0

When I add a small double to 1 the result is 1:

tmpResult[q] = 7.8879654121609884E-88  

tmpResult[q] = tmpResult[q] + 1.0;

tmpResult[q] = 1

I know that double is 18 precision only, but I am using Math.Net.Numerical.
And it can work only with double (int optional).

What is the problem?

J...
  • 30,968
  • 6
  • 66
  • 143
Vitalii
  • 13
  • 6
  • 2
    if you have to consider `7.8879654121609884E-88` anything else than zero you have a scaling problem in your logic. Adding two numbers that are apart by a factor of 10^88 smells pretty fishy to me. – InBetween Jan 04 '17 at 00:01
  • You might try incrementing (or decrementing) your double by the smallest possible amount. See [Get next smallest Double number](http://stackoverflow.com/q/15330644/3744182). – dbc Jan 04 '17 at 00:03
  • Are you sure that this discrepancy is significant? 1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000007887 is well close to 1. Are you sure that treating it as 1 will cause problems? Moving to decimal will slow things down massively. – spender Jan 04 '17 at 00:03
  • 2
    Using `decimal` won't help since it supports [28-29 significant digits](https://msdn.microsoft.com/en-us/library/364x0z75.aspx) and you need **at least 88 significant digits** for `1.0 + 7.8879654121609884E-88 == 1.0` to be false. You need to rethink your design if you think you need 88 digits of precision. – dbc Jan 04 '17 at 00:08
  • Besides, the language specs state: "For decimals with an absolute value less than 1.0m, the value is exact to the 28th decimal place, but no further.", so you'll butt into similar problems if you switch to decimal. – spender Jan 04 '17 at 00:11

1 Answers1

2

There is no problem.

The IEEE754 double precision representation of 1.0 is

0x3FF0000000000000

The next largest double is

0x3FF0000000000001

Which has a decimal value of

1.0000000000000002220446049250

The number you are trying to represent is

1.0000000000000000000000000000000000000000000000000000000000‌​00000000000000000000‌​00000000007887965....

The closest double precision number to this value is precisely 1.0, so that is the value that correctly returns from the operation.

J...
  • 30,968
  • 6
  • 66
  • 143