Java – Corretto Java float

Corretto Java float… here is a solution to the problem.

Corretto Java float

Java often has problems with including floats
Point arithmetic informal and original normal values contain
On such an operation:

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

Via float or double type.

Can anyone tell me how to tell Coretto Java
Going into a non-informal, precise floating-point pattern?

Solution

“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. This is a common Java problem. The simple answer is the same approach you use in other versions of OpenJDK.

You can’t use Java primitives like float and double. You have to resort to another math library. java.math. BigDecimal can 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

Short description:

The primitive types float and double are defined in the Java language specification. Java implements the IEEE 754 definition it represents.

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

Therefore, the following output is expected due to the 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

Citations:

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

Related Problems and Solutions