Finding the smallest integer that cannot be represented as an IEEE-754 32-bit float

Possible duplicate:
What is the first integer that the floating IEEE 754 cannot represent exactly?

First, it’s a matter of homework, just to make it clear right away. Of course, I'm not looking for false nutrition, maybe a little pointer to the right direction.

So, my task is to find the smallest positive integer that cannot be represented as an IEEE-754 float (32 bits). I know that equality testing for something like "5 == 5.00000000001" will fail, so I thought I would simply surpass all the numbers and experience it like this:

int main(int argc, char **argv)
{
    unsigned int i; /* Loop counter. No need to inizialize here. */

    /* Header output */
    printf("IEEE floating point rounding failure detection\n\n");

    /* Main program processing */
    /* Loop over every integer number */
    for (i = 0;; ++i)
    {
        float result = (float)i;

        /* TODO: Break condition for integer wrapping */

        /* Test integer representation against the IEEE-754 representation */
        if (result != i)
            break; /* Break the loop here */
    }

    /* Result output */
    printf("The smallest integer that can not be precisely represented as IEEE-754"
           " is:\n\t%d", i);


    return 0;
}

. "i" " ", "i" - "0.000000002", , .

- , , ?

-------------------- ---------------

! :

  • , (Solaris 10, 32 ), Linux- (64- 32- ).

  • , , , , , ,

!

+5
3

, - . float, , , float. float int, :

float result = (float)i;
int truncated = (int)result;
if (truncated != i) break;

16, . , .

+5

, (base, sign, signand, exponent)

Wikipedia, :

:

* Finite numbers, which may be either base 2 (binary) or base 10

(). : s = ( ), c = ( "" ), q = .         (-1) s Γ— c Γ— bq        b - (2 10). , 1 ( ), 12345, - -3, 10, -12.345.

+2

FLT_MAX+1. . float.h.

: . modf() math.h

0
source

All Articles