Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't adding a number to Double.MaxValue makes it Double.PositiveInfinity?

Tags:

c#

In Double.PositiveInfinity docs it's written that:

This constant is returned when the result of an operation is greater than MaxValue.

However, when I try to add a number to maximum value of double, it doesn't return infinity. I've tried running this:

double maxVal = Double.MaxValue;
maxVal = maxVal + 10000000000000000000;
Console.WriteLine(maxVal + " " + Double.IsInfinity(maxVal)); //prints 1.79769313486232E+308 False

Why is it happening? Why isn't it showing maxVal as infinity?

Here is a working fiddle.

like image 949
Dumbledore Avatar asked Mar 25 '15 17:03

Dumbledore


1 Answers

That's because the number that you are adding is way too small to make a dent on the Double.MaxValue value.

The precision of a double is about 15 digits, so you need a number that is at least 1e292 for it to be large enough to make a difference.

That would be 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 written out.

like image 69
Guffa Avatar answered Sep 19 '22 07:09

Guffa