Is there a case where an integer loses its accuracy in double casting?

Let's pretend that

int i=25; double j=(double)i; 

Is it likely that j will have values ​​of 24.9999999..upto_allowed or 25.00000000..._upto_allowed_minus_one_and_then_1 . I remember reading such things, but I couldn’t remember correctly.

In other words:

Is there a case where an integer loses its accuracy with double lithium?

+6
source share
3 answers

For small numbers like 25 , you are good. For very large (absolute) int values ​​in an architecture where int is 64-bit (having a value not representable in 53 bits) or more, you will lose accuracy.

A double-precision floating-point number has 53 bits of precision, of which the most significant bit (implicitly) is usually 1 .

On platforms where the floating point representation is not IEEE-754, the answer may be slightly different. For more information, you can refer to chapter 5.2.4.2.2 of the specifications C99 / C11

+5
source

The IEEE-754 double has a significant accuracy of 53 bits. This means that it can store all signed integers in the range of 2 ^ 53 and -2 ^ 53.

IEEE 754 Floating Point

Since int usually has 32 bits in most compilers / architectures, double will usually be able to handle int .

+2
source

@Mohit Jain is suitable for practical coding.

By specification C, DBL_DIG or FLT_RADIX/DBL_MANT_DIG and INT_MAX/INT_MIN are important values.

DBL_DIG in maximum decimal digits, the number of which may have, which, when converted to double and vice versa, will certainly have the same value. It is not less than 10. Thus, an integer, for example 9999999999, of course, can be converted to double and vice versa without loss of precision. Possible larger values ​​can also be successfully rounded.

The real circular motion problem starts with integer values ​​greater than +/-power(FLT_RADIX, DBL_MANT_DIG) . FLT_RADIX is the floating point base (and the vast majority 2 ), and DBL_MANT_DIG is the number of “base- FLT_RADIX digits in the floating point value,” for example 53 with the IEEE-754 binary code.

Of course, int has a range of [INT_MIN ... INT_MAX] . The range must be at least [-32767 ... + 32.767].

When, mathematically, power(FLT_RADIX, DBL_MANT_DIG) >= INT_MAX , there are no problems with the conversion. This applies to all relevant C. compilers.

0
source

All Articles