Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: Basic C++ Calculator Works only 99.5% of Time

  1. #11
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Basic C++ Calculator Works only 99.5% of Time

    If you want to definitely avoid rounding errors - don't read into a double at all - you technique of multiplying by 100 may solve the one test case you have but there might be others that you still get bitten by.

    I would read the input into a string buffer, and then parse that directly into an integer - and do validation at the same time if you want.

    That way your ammounts never go anywhere near a double, and no rounding errors will bite you.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  2. #12
    Join Date
    Apr 2008
    Location
    California Republic
    Beans
    2,657

    Re: Basic C++ Calculator Works only 99.5% of Time

    20.15, when converted to binary or hexadecimal, is a repeating fraction, and thus can't be represented by a floating point value with a finite number of bits. So paid, instead of being set to 20.15, is being set to a bit less than 20.15. When we multiply that by 100 without rounding, we get a bit less than 2015, and when we convert that to an int, it gets truncated from 2014.9999999... to 2014.
    The professor chuckled when I demonstrated this to him, and told him that everyone sitting around me with similar code was experiencing identical results. Hrmmm.

    Is that what you want it to do in your case?
    Not if it's going to lie to me about what I mean when I say to give me a zillion decimal points of accuracy!

    @Tony Flury:

    I like the idea of going from string to int.
    Semper Fi

    My Non-Ubuntu Blog.
    All posts by me are Public Domain.

  3. #13
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Basic C++ Calculator Works only 99.5% of Time

    Quote Originally Posted by earthpigg View Post
    Not if it's going to lie to me about what I mean when I say to give me a zillion decimal points of accuracy!
    A bit of pedantry here, but strictly speaking precission is not the same as accuracy.

    Precision is related to the number of significant figures that you work with - i.e. The sun is 93 million miles away - precision here is 2 significant figures. In general in a calculation you should never have a result which is more precise than any of your inputs.

    Accuracy : How close your answer is to the right answer. You could calculate a very precise answer (say 10 significant figures), but it may not be very accurate. This is effectively what is happening in your case - The double is trying to store your input in a very precise form 10.149999999999.... but the end result is not accurate.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  4. #14
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Basic C++ Calculator Works only 99.5% of Time

    Quote Originally Posted by tony flury View Post
    if you want to definitely avoid rounding errors - don't read into a double at all - you technique of multiplying by 100 may solve the one test case you have but there might be others that you still get bitten by.

    I would read the input into a string buffer, and then parse that directly into an integer - and do validation at the same time if you want.

    That way your ammounts never go anywhere near a double, and no rounding errors will bite you.
    +1
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  5. #15
    Join Date
    Nov 2011
    Beans
    56

    Re: Basic C++ Calculator Works only 99.5% of Time

    I'm going to work on this, it just seems like I ought to be able to solve this by using only integers.

    I don't think using literal constants such as 0.05, 0.10,0.25 should introduce the rounding errors.

    This is one of those problems that look so simple until one begins to analyze it.

  6. #16
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Basic C++ Calculator Works only 99.5% of Time

    Quote Originally Posted by xytron View Post
    I don't think using literal constants such as 0.05, 0.10,0.25 should introduce the rounding errors.
    Being a literal value or a calculated value does not matter. There is no way to represent an infinite amount of real numbers with a finite amount of bits. There will always be some values that cannot be represented and thus must be rounded.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  7. #17
    Join Date
    Nov 2011
    Beans
    56

    Re: Basic C++ Calculator Works only 99.5% of Time

    Quote Originally Posted by schauerlich View Post
    Being a literal value or a calculated value does not matter. There is no way to represent an infinite amount of real numbers with a finite amount of bits. There will always be some values that cannot be represented and thus must be rounded.
    Yes, but his solution almost worked. I think the problem is he mixed doubles and integers in his solution, he ought to have used only integers.

  8. #18
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: Basic C++ Calculator Works only 99.5% of Time

    I agree that "all ints" would be the way to go in order to avoid rounding problems. However, I'm not sufficiently proficient in C++ to suggest how to do this when converting an input string that might include decimals to integer.
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  9. #19
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Basic C++ Calculator Works only 99.5% of Time

    Actually, even a value seemingly very simple like 0.1 is rounded:

    Code:
    firas@dhcp-v041-181 ~ % gcc -std=c99 -pedantic -Wall -o test2 test2.c        
    firas@dhcp-v041-181 ~ % cat test2.c                                  
    #include <stdio.h>
    
    int main(void)
    {
        double d = 0.1;
        unsigned char *p = (unsigned char *) &d;
        for (int i=0; i<sizeof(double); i++) {
            printf("%02X ", *p++);
        }
        printf("\n");
    }
    firas@dhcp-v041-181 ~ % ./test2                                      
    9A 99 99 99 99 99 B9 3F
    So the stored value is 1.100110011001100110011001100110011001100110011001 1011*2^{—4}

    Notice how 1001 becomes 1011 at the end, which obviously means that rounding occurred (just like in decimal 0.666666... would be rounded to 0.666667). This is of course because computers operate in binary. Representing 1/10 in binary is like representing 2/3 in decimal: not possible.
    Last edited by Bachstelze; February 10th, 2012 at 01:37 AM.
    「明後日の夕方には帰ってるからね。」


  10. #20
    Join Date
    Apr 2008
    Location
    California Republic
    Beans
    2,657

    Re: Basic C++ Calculator Works only 99.5% of Time

    Someone suggested that I simply add a minuscule decimal value to the thing before converting it to an int and that seems to work.

    With what I actually know how to do, that seems to make it work well enough.
    Semper Fi

    My Non-Ubuntu Blog.
    All posts by me are Public Domain.

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •