# False Boolean Value?

• 10-08-2007
kwikness
False Boolean Value?
When the following code is executed, the loop does not run when f == 2.0
Why wouldn't it?

Code:

```int main() {         printf("\n\n\n"); //spacer         float f = .1;         while (f <= 2.0)         {                 printf("&#37;.1f*Pi:  sin() = %f  cos() = %f  tan() = %f\n",                                                         f,                                                         sin(PI*f),                                                         cos(PI*f),                                                         tan(PI*f));                 f += .1;                 printf("\nf now equals %f",f);         }         printf("\n\n\n"); //spacer         return 0; }```
last line of output from program:
Code:

```1.9*Pi:  sin() = -0.309016  cos() = 0.951057  tan() = -0.324919 f now equals 2.000000```
I also tried to see if this statement returned a true value:
Code:

`(2.0 == 2.00)`
..and it did, so I have no idea why my loop only runs until f == 1.9

I know I could eliminate the problem by changing my loop to:
Code:

`while (f < 2.1)`
But i'm curious as to why it isn't working as is.

Thank you for any help.
• 10-08-2007
brewbuck
Sigh...

The value 0.1 cannot be exactly represented in a binary format. There is no way to repeatedly add the value 0.1 to itself to achieve the value 2.0. Read any basic introduction to floating point arithmetic.
• 10-08-2007
kwikness

I'm aware that certain values cannot be expressed in binary, but when I did a printf for float f, it printed:

2.000000

I was under the impression that floats stored 6 significant digits, so I thought that this was the entire value of f.

*shrug*

Quote:

Originally Posted by brewbuck
Read any basic introduction to floating point arithmetic.

• 10-08-2007
ssharish2005
Yes by default, the float stores 6 digits after the decimal point. And you know it could be customized to 2 digits by &#37;.2f or something. That won't really make any different on comparing. Comparing floating point number is not going to give you the right answer, because of precision.

Google "Comparing Floating point number". You will understand the concept of comparing decimal variables.

ssharish
• 10-08-2007
kwikness
www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

Found the section on epsilon comparisons especially helpful. Thanks.
• 10-08-2007
brewbuck
Quote:

Originally Posted by kwikness
I was under the impression that floats stored 6 significant digits, so I thought that this was the entire value of f.

No, that's just what got printed. The actual value may have been something like 2.00000000001.

A float doesn't store a certain number of digits. The decimal point can "float" back and forth, thus the name. Since it is intrinsically a binary, not decimal, object, it is impossible to characterize the number of decimal digits it can represent -- unless you want to refer to a fractional number of digits.

For instance if you had 16 bits available to represent a fractional value, that's equivalent to 16 * log(2) / log(10) = 4.816... decimal digits.
• 10-08-2007
ssharish2005
I was wrong then if it the float doesnt have const no of decimal places after the point.

ssharish
• 10-09-2007
matsp
Quote:

Originally Posted by brewbuck
For instance if you had 16 bits available to represent a fractional value, that's equivalent to 16 * log(2) / log(10) = 4.816... decimal digits.

Note also that if you do something like this:
Code:

```int main() {   float f = 1.4f;   if (f == 1.4) {       printf("A\n");   }   if (f == 1.4f) {       printf("B\n");   }   return 0; }```
You may find that it only prints "B", becasue the first comparison extends f to a temporary double value before comparing with 1.4 - and since 1.4 is one of many values that can't be described EXACTLY as a float, it's failing to compare the exact value of the two variables.

The second compare compares the float value with a 1.4 float value, so it matches.
[But this may not happen if you calculate the value by for example adding 1.0 to 0.4 to make 1.4].

--
Mats