12

When a double has an 'exact' integer value, like so:

double x = 1.0;
double y = 123123;
double z = -4.000000;

Is it guaranteed that it will round properly to 1, 123123, and -4 when cast to an integer type via (int)x, (int)y, (int)z? (And not truncate to 0, 123122 or -5 b/c of floating point weirdness). I ask b/c according to this page (which is about fp's in lua, a language that only has doubles as its numeric type by default), talks about how integer operations with doubles are exact according to IEEE 754, but I'm not sure if, when calling C-functions with integer type parameters, I need to worry about rounding doubles manually, or it is taken care of when the doubles have exact integer values.

5
  • You should clarify a few items here a) by double do you mean 8 bytes or 4 bytes and b) by correct do you mean round goes towards zero?
    – JaredPar
    Jun 7, 2012 at 20:48
  • Possible duplicate: stackoverflow.com/questions/9154687/…
    – Ruben
    Jun 7, 2012 at 20:51
  • 1
    There is no "rounding" taking place. The cast performs truncation.
    – Ed S.
    Jun 7, 2012 at 20:51
  • @JaredPar: I had 8-byte floating points in mind, but I guess it doesn't matter. By rounding, I meant rounding to the nearest integer.
    – user1481
    Jun 7, 2012 at 20:54
  • It's fine, as long as your compiler's int type is not more than 32-bits and you assigned the double from a literal value (not computed). Jun 7, 2012 at 21:25

4 Answers 4

9

Yes, if the integer value fits in an int.

A double could represent integer values that are out of range for your int type. For example, 123123.0 cannot be converted to an int if your int type has only 16 bits.

It's also not guaranteed that a double can represent every value a particular type can represent. IEEE 754 uses something like 52 or 53 bits for the mantissa. If your long has 64 bits, then converting a very large long to double and back might not give the same value.

2
  • The standard does give that guarantee for values whose integral part can be represented in the target type in 6.3.1.4: "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined." Jun 7, 2012 at 23:26
  • @DanielFischer: Thanks for the reference. I've updated the answer. Jun 7, 2012 at 23:55
4

As Daniel Fischer stated, if the value of the integer part of the double (in your case, the double exactly) is representable in the type you are converting to, the result is exact. If the value is out of range of the destination type, the behavior is undefined. “Undefined” means the standard allows any behavior: You might get the closest representable number, you might get zero, you might get an exception, or the computer might explode. (Note: While the C standard permits your computer to explode, or even to destroy the universe, it is likely the manufacturer’s specifications impose a stricter limit on the behavior.)

2
  • 2
    In spite of my levity about undefined behavior, I want to point out it has serious consequences. Suppose you assume some results will be wrong because they are out of bounds, but you will detect and discard them after conversion. An unintended consequence of the compiler’s optimizer is that, if it can prove your code gets undefined behavior, it can optimize away your code and anything that calls it, replacing it with deliberate program exits or any other code. Jun 8, 2012 at 0:04
  • ... if the warding invoked by those manufacturer's specifications is insufficient: here be nasal daemons ....!
    – SlySven
    Oct 14, 2020 at 20:25
1

It will do it correctly, if it really is true integer, which you might be assured of in some contexts. But if the value is the result of previous floating point calculations, you could not easily know that.

Why not explicitly calculate the value with the floor() function, as in long value = floor(x + 0.5). Or, even better, use the modf() function to inspect for an integer value.

1
  • If you know the value is an integer then there is no advantage in using the floor() function vs. a cast (except when the language has no integer type, e.g. Lua, JS.)
    – finnw
    Jun 8, 2012 at 11:55
0

Yes it will hold the exact value you give it because you input it in code. Sometimes in calculations it would yield 0.99999999999 for example but that is due to the error in calculating with doubles not its storing capacity

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.