sergiol
sergiol

Reputation: 4335

DBL_MAX integer part equality

In Visual C++ 2010, I tried this

double d= DBL_MAX;
double dblmaxintpart;
modf(DBL_MAX, &dblmaxintpart);

In the debugger window I put

d == dblmaxintpart

which gave true as result.

Can I assume that DBL_MAX is equal to its integer part as an always valid assertion?

Upvotes: 1

Views: 346

Answers (1)

Sneftel
Sneftel

Reputation: 41503

Yes, the integer part of a double which represents an integer will always be the double itself, even at DBL_MAX. In fact, any double greater than 2^52 will have itself as an integer part, because doubles of that size don't have enough mantissal bits to represent a fraction.

For similar reasons, not all integers above 2^53 are representable as doubles (though when converted to doubles, they will still be integers).

Finally, the fractional part of any double less than 1 will be exactly itself, and the fractional and integer parts of any double, when added, will produce exactly the original double.

Upvotes: 3

Related Questions