-2

I am comparing one double from a text file with a hard-coded double, the value read from a text file is put into a double variable using Double.parseDouble() in Java. Then, I'm using the following line to assert their equality. Is it possible that this assert fails? Does it depend on the value?

assertEquals(17353820.450663020000, Double.parseDouble(input.next()), 0.0);

Note that 0.0 is the value of epsilon used for comparing doubles.

Text file contents:

17353820.450663020000
A. Mashreghi
  • 1,729
  • 2
  • 20
  • 33
  • Possible duplicate of [How to compare two double values in Java?](https://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java) – Kaan Aug 26 '19 at 22:57
  • What this reduces to is asking whether the compiler's string-to-double conversion produces the same result as the runtime system's string-to-double conversion. My guess would be "maybe". But why does successful operation of your code require it? Tests should test things that matter, not what just happens to be true today. –  Aug 26 '19 at 22:59
  • @another-dave No need to guess, it is *specified* to be exactly the same. See [my answer](https://stackoverflow.com/a/57665881/5221149). – Andreas Aug 26 '19 at 23:07
  • @Andreas - thanks! –  Aug 26 '19 at 23:14

2 Answers2

2

Is it possible that this assert fails?

No.

Does it depend on the value?

No.


Javadoc of parseDouble says:

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

The Java Language Specification, section 3.10.2. Floating-Point Literals says:

The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf of class Float and class Double of the package java.lang.

As you can see, the compiler uses he same logic for parsing doubles as the parseDouble method, so the result will always be the same.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • So, it's not possible that there might be rounding issues on special variables? For example, is it possible that 3.9999999999999 (with enough 9's) becomes 4? – A. Mashreghi Aug 26 '19 at 23:31
  • 1
    @A.Mashreghi If it does become 4, then both the compiler and `parseDouble` would cause that, so the result is still `double` values that are equal. If the characters of the number literal in the Java source code are the same characters as the string given to `parseDouble`, then the `double` value will be the same, because the Java Language Specification says it must be so. – Andreas Aug 26 '19 at 23:33
-1

Floats and doubles in any language have a limit on precision, so can be approximations to the string format. See https://floating-point-gui.de for some information. That's what the epsilon argument is for, it says how close the value being tested has to be to be considered "correct" when it can't be an exact match.

John Bayko
  • 746
  • 4
  • 7