Evgenii.Balai
Evgenii.Balai

Reputation: 949

On precision in multiplying integers stored as doubles

I have code like this:

double x = ...;
double y = ...;
double z = x * y;

Suppose x and y are assigned to integers (say, double x = 6, double y = 5), and x * y fits into 48 bits. Am I guaranteed that z will not loose precision? In other words, that z - floor(z) == 0.0 ?

Assume the compiler uses IEEE-754 standard, and double uses 64 bits.

Upvotes: 0

Views: 32

Answers (1)

Sneftel
Sneftel

Reputation: 41474

Yes, you are guaranteed not to lose precision. The basic rule of floating point is, the result is computed exactly, and then stored as closely as possible. Since all integers up to 2^53 are exactly representable, z will be exact.

Upvotes: 1

Related Questions