Alex Rivera | Logout

Representing integers in doubles

Asked 2009-04-17T06:00:44.010
25

Can a double (of a given number of bytes, with a reasonable mantissa/exponent balance) always fully precisely hold the range of an unsigned integer of half that number of bytes?

E.g. can an eight byte double fully precisely hold the range of numbers of a four byte unsigned int?

What this will boil down to is if a two byte float can hold the range of a one byte unsigned int.

A one byte unsigned int will of course be 0 -> 255.

Edit
Report

1 Answer

-5

I wouldn't use the words "fully precisely" when talking about floating-point numbers. But yes, a double can represent a 32-bit integer.

I do not know which other combinations of floats and ints that this is also true for.

Practically speaking, you don't want to bother using floating point above what your machine supports, so just switch to rational arithmetic with bignums. That way, you're guaranteed precision.

answered 2009-04-17T06:08:55.237

Your Answer