float - c data types

c, floating-point, floating-point-precision, types

Solution

Typically, a `float` has a precision of 24 bits. The number 20000001 = 0x1312d01 needs 25 bits to be represented exactly, so it must be rounded. The normal rounding mode for values exactly half-way between two representable values is rounding to last-bit-zero, hence 20000001 is rounded to 20000000 as a `float`.

2000001 = 0x1e8481 needs fewer than 24 bits to be represented (21), so there is no rounding needed for that.

Problem

``` int main() { float a = 20000000; float b = 1; float c = a+b; if (c==a) { printf("equal"); } else { printf("not equal");} return 0; } ``` when I run this it says "equal". but when I change the value of a to 2000000 (one zero less) the answer is no. why ?

Original source

Related problems