Ben Aston
Ben Aston

Reputation: 55729

IEEE 754 floating point - why does this work?

Given the following:

.1 + .2 === .3 // false
1 * .3 === .3 // true

...the former does not result in 0.3 because one or more of the operands cannot be exactly represented in IEEE 754 double-precision floating point, so the result is almost but not quite 0.3.

Why does the latter return true. Is it because 0.3 happens to be exactly representable in IEEE 754 double-precision floating point?

Upvotes: 1

Views: 79

Answers (1)

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

0.3 is not exactly representable, but 1.0 is. Multiplication of any number by 1, under IEEE round-to-nearest rules, must return the original value, which is equal to itself.

Upvotes: 1

Related Questions