Unsigned to double unsigned conversion without loss of precision

Will the integer value converted to a floating point value and vice versa be the same as the original integer value?

For example:

unsigned x = 42;
double y = x;
unsigned z = y;

Assuming the compiler is not optimizing for floating point conversion, will it x == zalways evaluate to true ?

I suspect that any representation error in a floating point conversion will always increase. Therefore, when a floating point value is converted back to an integer value, the value is truncated, which always leads to the original integer value.

Is my assumption correct?

+4
3

IEEE 754 double, x == z 1 x 2 53. , 32- unsigned int, , x.

, integer float. C FPU, -. float integer ( , float int ).

float , , , . , 2 53 +1 double, 2 53. , float , float integer .

float : 5555555555555555555 double 5555555555555555328, , , , . : , 5555555555555555855 5555555555555556352.

+10

2 53 , double IEEE-754 ( ). , int - 32-, , unsigned double .

+3

, - 64- IEEE754. ( C , , ).

unsigned int. 32-, , 64 , . ( 53- : 2 53 + 1 - IEEE double.).

32- .

On 64-bit platforms, this depends on the compiler. On LP64 and LLP64 unsigned int, 32 bits, but on ILP64, 64 bits. (Note that Win64 uses LLP64, which also sets longto 32 bits).

+1
source

All Articles