Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

double

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?

like image 615
habakuk Avatar asked Sep 24 '14 08:09

habakuk


1 Answers

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.

like image 92
Marc Gravell Avatar answered Oct 22 '22 05:10

Marc Gravell