-
Enhancement
-
Resolution: Won't Fix
-
P4
-
None
-
9.0.1
A DESCRIPTION OF THE REQUEST :
There is no integral type support in Java SE for integral decimal types. It previously was the ase in version 1.2 of Java that float and double, along with their object classes Float and Double, inside java.lang.*, defaulted to an accuracy mode of arithmetic, where infinite binary carry underflows and overflows absolutely did not happen.
types float and ouble need to have an accuracy arithmetic mode again, either by default or their own keyword. Note that the 'strictfp' keyword, to enforce floating point arithmetic mode, continues to remain part of the language.
JUSTIFICATION :
Because the basic arithmetic operators of +, - , * , / and % need to be available. Because accuracy mode on float and double are required, because using BigDecimal for everything involves too many subsequent round brackets ().() with re inclosing brackets, all at the same time. (()().(())).(). Because accuracy mode is required for programs that operate from a decimal type. Because this behaviour is in line with initial expectations.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
//The following should work for
//double primitive and Double classes, in Java 9 and beyond,
//withstanding auto boxing and auto unboxing.
double a = 0.1d;
double b = 0.1d;
double x = a*b;
System.out.println(x); // 0.01
System.out.println(x == 0.01) //true
//and
float a = 0.1f;
float b = 0.1f;
float x = a*b;
System.out.println(x); //0.01
System.out.println(x==0.01); //true
ACTUAL -
/*
Actual behaviour includes a mixture
of underflow and overflow of internal binary bits,
which are not logically consistant with the arithmetic desired results.
*/
Double a = new Double(0.1);
Double b = new Double(0.1);
Double x = a*b;
out.println(x); // 0.010000000000000002
---------- BEGIN SOURCE ----------
import java.lang.*;
import static java.lang.System.*;
public class Attempt
{
public static void main(String ... args)
{
Double a = new Double(0.1);
Double b = new Double(0.1);
Double x = a*b;
out.println(x);
float one = 0.1f;
float two = 0.1f;
float result = one*two;
out.println(result);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
There are workarounds for this issue, but only to a limited extent,
since there is no java.lang.StrictMath equivalent for
java.math.BigDecimal and java.math.BigInteger. If I wish to
do accuracy mode trigonometry, type exponents and type square roots,
via float and double.
There is no integral type support in Java SE for integral decimal types. It previously was the ase in version 1.2 of Java that float and double, along with their object classes Float and Double, inside java.lang.*, defaulted to an accuracy mode of arithmetic, where infinite binary carry underflows and overflows absolutely did not happen.
types float and ouble need to have an accuracy arithmetic mode again, either by default or their own keyword. Note that the 'strictfp' keyword, to enforce floating point arithmetic mode, continues to remain part of the language.
JUSTIFICATION :
Because the basic arithmetic operators of +, - , * , / and % need to be available. Because accuracy mode on float and double are required, because using BigDecimal for everything involves too many subsequent round brackets ().() with re inclosing brackets, all at the same time. (()().(())).(). Because accuracy mode is required for programs that operate from a decimal type. Because this behaviour is in line with initial expectations.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
//The following should work for
//double primitive and Double classes, in Java 9 and beyond,
//withstanding auto boxing and auto unboxing.
double a = 0.1d;
double b = 0.1d;
double x = a*b;
System.out.println(x); // 0.01
System.out.println(x == 0.01) //true
//and
float a = 0.1f;
float b = 0.1f;
float x = a*b;
System.out.println(x); //0.01
System.out.println(x==0.01); //true
ACTUAL -
/*
Actual behaviour includes a mixture
of underflow and overflow of internal binary bits,
which are not logically consistant with the arithmetic desired results.
*/
Double a = new Double(0.1);
Double b = new Double(0.1);
Double x = a*b;
out.println(x); // 0.010000000000000002
---------- BEGIN SOURCE ----------
import java.lang.*;
import static java.lang.System.*;
public class Attempt
{
public static void main(String ... args)
{
Double a = new Double(0.1);
Double b = new Double(0.1);
Double x = a*b;
out.println(x);
float one = 0.1f;
float two = 0.1f;
float result = one*two;
out.println(result);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
There are workarounds for this issue, but only to a limited extent,
since there is no java.lang.StrictMath equivalent for
java.math.BigDecimal and java.math.BigInteger. If I wish to
do accuracy mode trigonometry, type exponents and type square roots,
via float and double.
- relates to
-
JDK-4297957 Add algebraic and transcendental functions operating on BigDecimal
- Closed
-
JDK-8190946 There is no elementary functions support for BigDecimal and BigInteger.
- Closed