4

I was going through this answer and could not answer one thing. May be, because I have a mathematician's mind, so please forgive me if it is too simple question.

Why is this not resulting in Infinity.

double.MaxValue + double.Epsilon

However small value Epsilon have, whenever it is added to max value, it should increase the max value by a small precision. Why is that not increased in this case?

Here is the dotnetfiddle where I have been experimenting.

Community
  • 1
  • 1
Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
  • 2
    Not an answer: you do realize that to change value of `double` near MaxValue you need to modify it be really large (random guess 10^10?) increment, don't you? Or maybe it is an answer... [Double.PositiveInfinity](https://msdn.microsoft.com/en-us/library/system.double.positiveinfinity%28v=vs.110%29.aspx) returned when value is greater than MaxValue, but adding such small increment not going to change it at all. – Alexei Levenkov Aug 19 '15 at 04:24
  • 2
    Here is another answer which can help you: http://stackoverflow.com/questions/4251298/why-float-maxvalue-float-maxvalue-1-does-return-true – mybirthname Aug 19 '15 at 04:28
  • 2
    @mybirthname I think it is perfect duplicate... (and my guess of increment was *a bit* off - you need to add `1E292d` :)) – Alexei Levenkov Aug 19 '15 at 04:32

1 Answers1

2

In the default to-nearest rounding mode, the mathematical result of the addition would have to be above the middle point between double.MaxValue and the next floating-point value that could be represented if the exponent range was wider for the floating-point result to round up to Infinity.

Operations resulting in a mathematical result between double.MaxValue and this middle point are rounded down to double.MaxValue.

double.Epsilon is much smaller than the distance between double.MaxValue and this middle point, therefore the result of the floating-point addition is double.MaxValue.

This phenomenon is called “absorption” in general (not only when one of the summands is double.MaxValue, but as soon as the ratio between the summands is such that the result is the same as the largest summand).

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • Out of curiosity, I was testing for the limit before the value hits infinity. I found, consequently, that `var i = double.MaxValue - 100000000000000000000000000000000000000000000000000000000000000000000.0; (i == double.MaxValue).Dump();` returns `true`. My first tests were subtracting 1, and I figured it was due to precision. But how can this be..? – Rob Aug 19 '15 at 04:32
  • 2
    @Rob I think [answer](http://stackoverflow.com/questions/4251298/why-float-maxvalue-float-maxvalue-1-does-return-true) found by mybirthname explains it too. – Alexei Levenkov Aug 19 '15 at 04:33
  • 1
    @Rob `100000000000000000000000000000000000000000000000000000000000000000000.0` (about 70 zeroes) is **much** smaller than `double.MaxValue ` (~1e308) and even than the difference between `double.MaxValue ` and the next representable floating-point number if the exponent range was wider (take out about 16 zeroes, resulting in a number that must be around 1e292, give or take a factor of 100). – Pascal Cuoq Aug 19 '15 at 04:35
  • @AlexeiLevenkov - Thanks to you both, that makes sense now :) – Rob Aug 19 '15 at 04:37
  • @Rob Adding anything less than 179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792 will result in DBL_MAX. (That's 1/2 ULP above DBL_MAX) – Rick Regan Aug 19 '15 at 12:41
  • @Rob ...or subtracting as per your example. – Rick Regan Aug 19 '15 at 12:49