I have some C code performing high precision arithmetic compiled by gcc(gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)).The final result of the calculation is a double which has a value of 622.07999995861189.I am casting the double to a float.
frequency_value = (float)current_freq;
where current_freq is a double and frequency_value is a float.The value of frequency_value after the cast is 622.080017.I would expect the value to be 622.079956 as calculated in gdb by
(gdb) p (float)current_freq
$1 = 622.079956
Can anyone explain the large difference in values between that calculated by gcc and gdb.
A float has much less precision than a double; you lose about half the digits. So at best you’d be seeing the 622.0799 portion (rounded up to 622.0800). The difference you see is probably caused by the rounding mode in use.
Here are the actual numbers:
The internal representations are values generated using Java’s
Float.floatToIntBits. You can also useFloat.intBitsToFloatto get back a floating-point number.