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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With