3

Java typically has had a problem with inclusion of floating point arithmetic denormal and pronormal value inclusion upon operations such as this:

double a = 0.1;
double b = 0.1;
double x = a*b;
out.println();
out.println(x);

by means of the float or double types.

Can someone please tell me how to tell Coretto Java to enter non denormal, accurate floating point mode?

Andreas
  • 2,455
  • 10
  • 21
  • 24
user1544
  • 155
  • 4
  • 1
    We have removed the sentences in your Question aimed at the Coretto developers. If you wish to contact them (e.g. to request new features or report bugs) the FAQ says how you can do this: https://aws.amazon.com/corretto/faqs/ – Stephen C Feb 14 '19 at 06:01

1 Answers1

3

“Can someone please tell me how to tell Coretto Java to enter non denormal, accurate floating point mode?”

This is not a Corretto-specific problem. It’s a general Java problem. The simple answer is in the same way you do it in other versions of OpenJDK.

You can’t use Java primitive types like float and double. You have to resort to another math library. java.math.BigDecimal might be a good start.

jshell> import static java.math.BigDecimal.*;
jshell> var a = ONE.divide(TEN)
a ==> 0.1
jshell> var b = ONE.divide(TEN) 
b ==> 0.1
jshell> a.add(b)
$16 ==> 0.2

TLDR:

Primitive types float and double are defined in Java Language Spec. Java implements the IEEE 754 definition of their representation.

4.2.3. Floating-Point Types, Formats, and Values The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).

https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.2.3

As a result, the following output is expected because of IEEE 754 binary representation.

jshell> double a = 0.1;
a ==> 0.1
jshell> double b = 0.1;
b ==> 0.1
jshell> double x = a*b;
x ==> 0.010000000000000002

References:

Is there any IEEE 754 standard implementations for Java floating point primitives?

Xin Liu
  • 41
  • 2
  • Withstanding compatability, is there any possibility of Corretto getting a simultaneous runtime switch, or a secondary mode, allowing for range accurate floating point beaviour instead? – user1544 Feb 14 '19 at 23:34
  • Corretto doesn't support it. IIRC, I don't know any Java implementation supports it. Java is a general-purpose programming language. As I said before, you can resort to library to support it. This is same as C/C++. If numeric applications are your primary consideration, I think Python is a better option. – Xin Liu Feb 15 '19 at 22:19