1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

This is precise math???

Discussion in 'C' started by danielakkerman, Jan 3, 2007.

  1. danielakkerman

    danielakkerman New Member

    Joined:
    Dec 25, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I am sorry if I am being too annoying with my questions, but here's a really puzzling one, especially, since I'd been running the same operation in Java, and it worked PERFECTLY!
    here's the code:
    Code:
    double i = 0;
    while(i<5){
    if(i*2 == 1.6)printf("%s", "Yay");
    i=i+0.1;
    }
    
    The point is, it dosen't find the answer(which is btw: 0.8, i should be equal to 0.8).
    I've tried, float, coverting double to float, and backwards...
    Please help,
    Thanks,
    Thanks again,
    Daniel
     
  2. ever_thus

    ever_thus New Member

    Joined:
    Jan 3, 2007
    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    using comparisons is tricky with floating points, which is what a double type is. As an analogy consider decimal numbers. The standard way to to represeent 1/3 is 0.33333333333 (for as long as you want). But multiply this by 3 and you don't have 1. The same problems occur with the binary numbers computers use. Different compilers have different ways of representing floating points, which might be why the program worked in Java.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Ever_thus gives a great example. You might also consider this: if a 32-bit float has more range than a 32-bit integer, where's the magic? There isn't any. Range is gained by a loss of precision and accuracy. If I give you only 3 decimal places, but an exponent to go with it, you can express a ton of numbers, but you can't express them all. One deals with this by measuring a result in terms of an error range, rather than by looking for an exact equality.
     
  4. danielakkerman

    danielakkerman New Member

    Joined:
    Dec 25, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Actually,
    Whenever I ask the program to print out the multiplication of i*2
    One of the answers that comes is 1.6(the correct one!).
    Thus, I was only able to reckon that something must wrong with the if statement rather then the precision.
    1.6 is a definite fraction. Not infinite like 1/3.
    Also, As I've mentioned earlier, the multiplication 'goes-on' just fine.
    Any other suggestions?
    Thanks,
    Daniel
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    1.6 is not a 'definite fraction' in base 2. Even so, that has nothing to do with the problem, per se. If i*2, however correctly or incorrectly it's represented, matches 1.6, however correctly or incorrectly it's represented, then the expression evaluation will be true.

    The value of .8, to 5 binary places, is .11001; multiplying that by 2 yields 1.10010. The value of 1.6, to 5 binary places, is 1.10011. Unequal. The value of .8, to 6 binary places, is .110011; multiplying by 2 yields 1.100110. The value of 1.6, to 6 binary places, is 1.10010. Equal. The outcome of your example, therefore, depends entirely upon your particular implementation.

    You have a couple of problems. You're taking an analogy expressed in base 10 as being an exact example for base 2. It was intended to show you that perfect accuracy is sometimes unattainable, this side of infinity. Your second problem is that you are looking for an exact explanation, such as I have given you above. It is better for you if you read your responses, then go sit on the corral fence and ponder the implications. You'll learn more and understand more. One unfortunate side effect of sitting on the fence and pondering is that you might get sidetracked into wondering why cow crap is like a frisbee and horse crap is like a tennis ball. I still highly recommend it.

    Despite what you may have heard, "precise math" is not a given. In ANY endeavor. That's why the term, "discarded because it's negligible", is seen in many solutions of complex equations. All you have to do to make your example work is define the bounds of negligibility and implement it.
     
    Last edited: Jan 4, 2007

Share This Page