habakuk
habakuk

Reputation: 2761

Why evaluates "(Double.MinValue + 1) > Double.MinValue" to false?

I first tried this (in vb.net)

(Double.MinValue + Double.Epsilon) > Double.MinValue

but that evaluates to false. Then I tried this

(Double.MinValue + 999999999999999999) > Double.MinValue

that evaluates to false, too.

Why?

Upvotes: 11

Views: 246

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062745

Adding a very small (magnitude) value to a very large (magnitude) makes: virtually no difference. In this case, the difference is so small that it cannot be represented within the precision of double. Basically:

double.MinValue + (most things) === double.MinValue

It doesn't guarantee to be able to represent every single double between double.MinValue and double.MaxValue, and as you increase the magnitude, the absolute resolution of what can be represented decreases.

Don't forget: double.MinValue has 308 digits before the decimal place. You are altering very few of them. You are basically doing:

-179769313486232000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000 // yikes!
+ 999999999999999999

Keep in mind that double has roughly 17 digits of precision; so about 291 digits of that huge number can be largely ignored.

Upvotes: 15

Related Questions