JDK-8262930 : Double.scalb fails for higher exponents.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 8,11,16,17
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • Submitted: 2021-03-02
  • Updated: 2021-05-25
  • Resolved: 2021-03-03
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
This bug occurs accross all versions i tried including 11 

A DESCRIPTION OF THE PROBLEM :
Comparing
	System.out.println("pow 2^max: "+Math.pow  (2, 127));
	System.out.println("sca 2^max: "+Math.scalb(1, 127));
yields 
pow 2^max: 1.7014118346046923E38
sca 2^max: 1.7014118E38

but even worse, 
	System.out.println("pow 2^max: "+Math.pow  (2, 128));
	System.out.println("sca 2^max: "+Math.scalb(1, 128));
yields 
pow 2^max: 3.4028236692093846E38
sca 2^max: Infinity
Wheras pow works until Double.MAX_EXPONENT, scalb continues to fail. 


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just write a class with main method containing the two lines in the description. 

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
2^127 shall be the same with pow and with scalb and i have good reasons to suspect, 
that scalb is wrong. 
2^128 until 2^Double.MAX_EXPONENT shall be finite as it is for pow, but not for scalb. 

Note: scalb for float seems ok. 
ACTUAL -
lower exponents until 127 inaccurate result, 128 to Double.MAX_EXPONENT infinite result. 

---------- BEGIN SOURCE ----------
see above. 
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
well, use the cumbersome pow. but this is not as exact. 
I need in particular scalb(1,...) i.e. powers of 2 which shall be exact using scalb but not for pow. 
So, for high precision computing: no workaround. 
Well long to double. 

FREQUENCY : always



Comments
Note there are two scalb methods in java.lang.Math, one with a float first argument, the other with a double. The expression "Math.scalb(1, 127)" will call the *float* method, which has much less range than double. As shown in the partial jshell session below, calling the double scalb method shows the expected range: jshell> Math.pow (2, 127) $1 ==> 1.7014118346046923E38 jshell> Math.scalb(1, 127) $2 ==> 1.7014118E38 jshell> Math.scalb(1.0, 127) $3 ==> 1.7014118346046923E38 jshell> Math.scalb(1.0, 128) $4 ==> 3.4028236692093846E38 jshell> Math.scalb(1.0, 1023) $5 ==> 8.98846567431158E307 jshell> Math.scalb(1.0, 1024) $6 ==> Infinity Closing as not a bug.
03-03-2021

The observations on Windows 10: JDK 8: Failed, result of sca 2^max is Infinity JDK 11: Failed. JDK 16: Failed. JDK 17ea+6: Failed.
03-03-2021