problems in floating point comparison

c, floating-point, floating-point-conversion

Solution

`f` is using `float` precision, but 0.98 is in `double` precision by default, so the statement `f <= 0.98` is compared using `double` precision.

The `f` is therefore converted to a `double` in the comparison, but may make the result slightly larger than 0.98.

Use

if(f <= 0.98f)

or use a `double` for `f` instead.

In detail... assuming `float` is IEEE single-precision and `double` is IEEE double-precision.

These kinds of floating point numbers are stored with base-2 representation. In base-2 this number needs an infinite precision to represent as it is a repeated decimal:

0.98 = 0.1111101011100001010001111010111000010100011110101110000101000...

A `float` can only store 24 bits of significant figures, i.e.

       0.111110101110000101000111_101...
                                 ^ round off here
   =   0.111110101110000101001000

   =   16441672 / 2^24

   =   0.98000001907...

A `double` can store 53 bits of signficant figures, so

       0.11111010111000010100011110101110000101000111101011100_00101000...
                                                              ^ round off here
   =   0.11111010111000010100011110101110000101000111101011100

   =   8827055269646172 / 2^53

   =   0.97999999999999998224...

So the 0.98 will become slightly larger in `float` and smaller in `double`.

Problem

``` void main() { float f = 0.98; if(f <= 0.98) printf("hi"); else printf("hello"); getch(); } ``` I am getting this problem here.On using different floating point values of f i am getting different results. Why this is happening?

Original source

Related problems