23

If I have an int, convert it to a double, then convert the double back to an int, am I guaranteed to get the same value back that I started with? In other words, given this function:

int passThroughDouble(int input)
{
  double d = input;
  return d;
}

Am I guaranteed that passThroughDouble(x) == x for all ints x?

8
  • It will depend on the size of int, e.g. for 64 bit ints this will definitely not work. For 32 bit ints it will probably work.
    – Paul R
    Dec 3, 2012 at 22:59
  • 1
    assert(std::numeric_limits<int>::digits <= std::numeric_limits<double>::digits); - en.cppreference.com/w/cpp/types/numeric_limits/digits
    – sehe
    Dec 3, 2012 at 23:03
  • 1
    @sehe: Surely you mean static_assert. :P
    – GManNickG
    Dec 3, 2012 at 23:04
  • 6
    @GManNickG darn. too late to fix :) static_assert(std::numeric_limits<int>::digits <= std::numeric_limits<double>::digits, "barf");
    – sehe
    Dec 3, 2012 at 23:08
  • 1
    of course any compiler worthy of the name will optimize out the fiddling about with the double and simply return input directly. Or maybe not - if the compiler writer knows that the result is not guaranteed :-)
    – pm100
    Dec 3, 2012 at 23:56

2 Answers 2

30

No it isn't. The standard says nothing about the relative sizes of int and double.

If int is a 64-bit integer and double is the standard IEEE double-precision, then it will already fail for numbers bigger than 2^53.


That said, int is still 32-bit on the majority of environments today. So it will still hold in many cases.

7
  • in what system does int have 64 bit though? windows and linux both have it at 32bit. Dec 3, 2012 at 23:04
  • I dont think C says IEEE double precision. It just says double precision; meaning twice the precision of float. So 64 bit int might fit in double exactly (clearly not guaranteed tho)
    – pm100
    Dec 3, 2012 at 23:04
  • @JohannesSchaub-litb I've heard of some linux distribution having 64-bit ints. But that was second-hand info I got from the lounge.
    – Mysticial
    Dec 3, 2012 at 23:08
  • @pm100: I don't think C says a double has twice the precision of float either, they could be the same size. However, Mysticial's post is right. "if... double is the standard IEEE double-precision..." Dec 3, 2012 at 23:41
  • Indeed, I've seen one compiler (LCC?) that implemented both float and double using double-precision.
    – dan04
    Dec 4, 2012 at 2:50
12

If we restrict consideration to the "traditional" IEEE-754-style representation of floating-point types, then you can expect this conversion to be value-preserving if and only if the mantissa of the type double has as many bits as there are non-sign bits in type int.

Mantissa of a classic IEEE-754 double type is 53-bit wide (including the "implied" leading bit), which means that you can represent integers in [-2^53, +2^53] range precisely. Everything out of this range will generally lose precision.

So, it all depends on how wide your int is compared to your double. The answer depends on the specific platform. With 32-bit int and IEEE-754 double the equality should hold.

1
  • Digital audio systems depend on integer to floating-point to integer conversions to work, or 'the world would stop spinning'. As someone who had to delve into the theory of the topic and to conduct some practical experiments, I can only say that this is the right answer.
    – Izhaki
    Dec 4, 2012 at 0:41

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.