3 ms·
` As long as they are small enough, floating point numbers can represent integers exactly. ` Is this true? For example, if I want to do integer + floating poin
by frozenport 12y ago
` As long as they are small enough, floating point numbers can represent integers exactly. `
Is this true? For example, if I want to do integer + floating point addition the CPU might dump the floating point into a 80bit register resulting in a form which does not play as expected with integers.
- colanderman 12y agoThe CPU doesn't make that decision, the compiler does. And AFAIK, GCC doesn't use the 80-bit 8087 floating point registers in 64-bit code (it sticks to the 64-bit SSE2 registers.) Even if 80-bit registers were used, they are still exponent+mantissa format, which represents small integers exactly.
- nsxwolf 12y agoI had no idea the Intel 8087 was so related to the IEEE standard until I saw your comment. I had to look it up. Very cool.
- recursive 12y agoAs long as the mantissa has at least 32 bits in it, then any 32 bit integer can make the round trip lossless-ly. That's true for 64 bit floats and for 80 bit floats.
- wtallis 12y agoIf your machine uses 80-bit floating point, then those extended-precision floats can exactly represent any 64-bit integer, provided that your language's standard library/runtime correctly converts without an intermediate step using 64-bit floats (53-bit mantissa).
- jevinskie 12y agoSometimes called 'long double' by the compiler.
- saitohm 12y agoYes. You can see the compatibility between 80-bit floating points (long double in C) and 64-bit integer (long long int) with a code like this: long double x0,x1,y0,y1; x0 = 18446744073709551615.0L; /* 2^64 - 1 */ x1 = 18446744073709551616.0L; /* 2^64 */ y0 = x0 + 1.0L; y1 = x1 + 1.0L; printf("%d\n", x0 == y0); /* false (with enough precision) */ printf("%d\n", x1 == y1); /* true (precision being lost ) */