Why is the double syntax not equal to the initialized double of supposedly one value?

When I execute this line:

double dParsed = double.Parse("0.00000002036");

dParsed actually gets the value: 0.000000020360000000000002

Compared to this line

double dInitialized = 0.00000002036;

and in this case, the dInitialized value is exactly 0.00000002036

Here they are in the debugger: Difference between double.Parse and an initializer

This inconsistency is a minor annoyance because I want to run tests line by line:

[Subject("parsing doubles")]
public class when_parsing_crazy_doubles
    {
    static double dInitialized = 0.00000002036;
    static double dParsed;
    Because of = () => dParsed = double.Parse("0.00000002036");
    It should_match = () =>  dParsed.ShouldBeLike(dInitialized);
    }

This of course fails:

Machine.Specifications.SpecificationException
"":
  Expected: [2.036E-08]
  But was: [2.036E-08]

"" , . , 4 5 . :

0.00000002036 0.90908165072 6256.77753019160

, 11 . ?

, , , , , double.Parse . 11 ?

+4
1

double dInitialized = 0.00000002036;

dInitialized 0,00000002036


- , dInitialized 0,00000002036. - , 10 0,00000002036 2.

, . . , , . , .

epsilon . , (, ), 1-15. (, ), .


: .

11 ?

,

static double epsilon = 1e-11;
if (Math.Abs(dParsed-dInitialized) > epsilon*Math.Abs(dInitialized)) {
    noteTestAsFailed();
}

epsilon 4e-16.


# 2: double.Parse ?

, ? double.Parse . , , 0,00000002036 , , , , ULP (0,00000002036). "" - , ULP . , .

0.00000002036 - . . , IEEE, 6153432421838462/2 ^ 78 6153432421838463/2 ^ 78. 12306864843676925/2 ^ 79, , 0,00000002036. . , , , double.Parse, , , .


# 3:

0.00000002036:

  • 2/1e8 + 3/1e10 + 6/1e11
  • 2 * 1e-8 + 3 * 1e-10 + 6 * 1e-11
  • 2.036 * 1e-8
  • 2.036/1e8
  • 2036 * 1e-11
  • 2036/1e11

. , , .

+4

Source: https://habr.com/ru/post/1524740/


All Articles