APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.internal.math

public final Class FloatingDecimal

extends Object
Class Inheritance
Imports
jdk.internal.vm.annotation.Stable

A class for converting between ASCII and decimal representations of a single or double precision floating point number. Most conversions are provided via static convenience methods, although a BinaryToASCIIBuffer instance may be obtained.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class

Field Summary

Modifier and TypeField and Description
private static final FloatingDecimal.BinaryToASCIIBuffer
private static final FloatingDecimal.BinaryToASCIIBuffer
private static final int
private static final int
private static final int
private static final int[]
private static final int[]
private static final long
private static final int
private static final int
private static final long
private static final int[]
private static final String
private static final int
private static final int
private static final int
private static final int
private static final int
private static final String
private static final String
private static final int[]
P

private static final int[]
private static final int[]
private static final int[]
private static final int
private static final ThreadLocal<FloatingDecimal.BinaryToASCIIBuffer>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static long
appendDecDigit(long v, int ch)

private static double
buildDouble(int ssign, int q, long c)

private static float
buildFloat(int ssign, int q, long c)

private static void
check(String in, boolean expected)

private static int
checkMultiplePoints(int pt, int i)

private static void
copyDigits(String in, byte[] d, int len, int i)

private static int
digitFor(int ch)

private static FloatingDecimal.BinaryToASCIIBuffer
public static FloatingDecimal.BinaryToASCIIBuffer

Returns:

The converter.
getBinaryToASCIIConverter
(double
The double precision value to convert.
d
,
boolean
compatibility with releases < JDK 21
compat
)

Returns a BinaryToASCIIConverter for a double.

private static FloatingDecimal.BinaryToASCIIBuffer
private static FloatingDecimal.BinaryToASCIIBuffer
private static boolean
isDigit(int ch, boolean isDec)

public static double

Returns:

The double precision value.
parseDouble
(String
The String to convert.
s
)

Converts a String to a double precision floating point value.

public static double

Returns:

The double-precision value of the conversion
parseDoubleSignlessDigits
(int
The decimal exponent of the value to generate
decExp
,
byte[]
The digits of the significand.
d
,
int
Number of digits to use
length
)

Converts a sequence of digits ('0'-'9') as well as an exponent to a positive double value

public static float

Returns:

The single precision value.
parseFloat
(String
The String to convert.
s
)

Converts a String to a single precision floating point value.

private static double

Returns:

an appropriate binary converter
readJavaFormatString
(String
the non-null input
in
,
int
one of the BINARY_<S>_IX constants, where <S> is one of 16, 32, 64
ix
)

The input must match the rules described here, about leading and trailing whitespaces, and the grammar.

private static void
scanSymbolic(String in, int i, String sub)

private static int
private static int
toLowerCase(int ch)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

B2AC_NEGATIVE_ZEROback to summary
private static final FloatingDecimal.BinaryToASCIIBuffer B2AC_NEGATIVE_ZERO
B2AC_POSITIVE_ZEROback to summary
private static final FloatingDecimal.BinaryToASCIIBuffer B2AC_POSITIVE_ZERO
BINARY_16_IXback to summary
private static final int BINARY_16_IX
BINARY_32_IXback to summary
private static final int BINARY_32_IX
BINARY_64_IXback to summary
private static final int BINARY_64_IX
E_THR_Iback to summary
private static final int[] E_THR_I
Annotations
@Stable
E_THR_Zback to summary
private static final int[] E_THR_Z
Annotations
@Stable
EXP_ONEback to summary
private static final long EXP_ONE
EXP_SHIFTback to summary
private static final int EXP_SHIFT
FLOG_10_MAX_LONGback to summary
private static final int FLOG_10_MAX_LONG
FRACT_HOBback to summary
private static final long FRACT_HOB
HEX_COUNTback to summary
private static final int[] HEX_COUNT
Annotations
@Stable
INFINITY_REPback to summary
private static final String INFINITY_REP
L_HALFback to summary
private static final int L_HALF
MAX_DEC_DIGITSback to summary
private static final int MAX_DEC_DIGITS
MAX_OUTback to summary
private static final int MAX_OUT
MAX_SMALL_BIN_EXPback to summary
private static final int MAX_SMALL_BIN_EXP
MIN_SMALL_BIN_EXPback to summary
private static final int MIN_SMALL_BIN_EXP
NAN_REPback to summary
private static final String NAN_REP
OMITTEDback to summary
private static final String OMITTED
Pback to summary
private static final int[] P
Annotations
@Stable
Q_MINback to summary
private static final int[] Q_MIN
Annotations
@Stable
QP_MAXback to summary
private static final int[] QP_MAX
Annotations
@Stable
QP_MINback to summary
private static final int[] QP_MIN
Annotations
@Stable
R_HALFback to summary
private static final int R_HALF
threadLocalBinaryToASCIIBufferback to summary
private static final ThreadLocal<FloatingDecimal.BinaryToASCIIBuffer> threadLocalBinaryToASCIIBuffer

Constructor Detail

FloatingDecimalback to summary
public FloatingDecimal()

Method Detail

appendDecDigitback to summary
private static long appendDecDigit(long v, int ch)
buildDoubleback to summary
private static double buildDouble(int ssign, int q, long c)
buildFloatback to summary
private static float buildFloat(int ssign, int q, long c)
checkback to summary
private static void check(String in, boolean expected)
checkMultiplePointsback to summary
private static int checkMultiplePoints(int pt, int i)
copyDigitsback to summary
private static void copyDigits(String in, byte[] d, int len, int i)
digitForback to summary
private static int digitFor(int ch)
getBinaryToASCIIBufferback to summary
private static FloatingDecimal.BinaryToASCIIBuffer getBinaryToASCIIBuffer()
getBinaryToASCIIConverterback to summary
public static FloatingDecimal.BinaryToASCIIBuffer getBinaryToASCIIConverter(double d, boolean compat)

Returns a BinaryToASCIIConverter for a double. The returned object is a ThreadLocal variable of this class.

Parameters
d:double

The double precision value to convert.

compat:boolean

compatibility with releases < JDK 21

Returns:FloatingDecimal.BinaryToASCIIBuffer

The converter.

getBinaryToASCIIConverterback to summary
private static FloatingDecimal.BinaryToASCIIBuffer getBinaryToASCIIConverter(double d)
getCompatBinaryToASCIIConverterback to summary
private static FloatingDecimal.BinaryToASCIIBuffer getCompatBinaryToASCIIConverter(double d)
isDigitback to summary
private static boolean isDigit(int ch, boolean isDec)
parseDoubleback to summary
public static double parseDouble(String s) throws NumberFormatException

Converts a String to a double precision floating point value.

Parameters
s:String

The String to convert.

Returns:double

The double precision value.

Exceptions
NumberFormatException:
If the String does not represent a properly formatted double precision value.
parseDoubleSignlessDigitsback to summary
public static double parseDoubleSignlessDigits(int decExp, byte[] d, int length)

Converts a sequence of digits ('0'-'9') as well as an exponent to a positive double value

Parameters
decExp:int

The decimal exponent of the value to generate

d:byte[]

The digits of the significand.

length:int

Number of digits to use

Returns:double

The double-precision value of the conversion

parseFloatback to summary
public static float parseFloat(String s) throws NumberFormatException

Converts a String to a single precision floating point value.

Parameters
s:String

The String to convert.

Returns:float

The single precision value.

Exceptions
NumberFormatException:
If the String does not represent a properly formatted single precision value.
readJavaFormatStringback to summary
private static double readJavaFormatString(String in, int ix)

The input must match the rules described here, about leading and trailing whitespaces, and the grammar.

Parameters
in:String

the non-null input

ix:int

one of the BINARY_<S>_IX constants, where <S> is one of 16, 32, 64

Returns:double

an appropriate binary converter

Exceptions
NullPointerException:
if the input is null
NumberFormatException:
if the input is malformed
scanSymbolicback to summary
private static void scanSymbolic(String in, int i, String sub)
skipWhitespacesback to summary
private static int skipWhitespaces(String in, int i)
toLowerCaseback to summary
private static int toLowerCase(int ch)
jdk.internal.math back to summary

private final Class FloatingDecimal.ASCIIToBinaryBuffer

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final byte[]
d

private final int
e

private final boolean
private static final int
private final int
n

private static final int
private static final float[]
private static final double[]

Constructor Summary

AccessConstructor and Description
private
ASCIIToBinaryBuffer(boolean isNegative, int e, byte[] d, int n)

Method Summary

Modifier and TypeMethod and Description
private double
private float
private double
signed(double v)

private float
signed(float v)

private long
toLong(int n)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dback to summary
private final byte[] d
eback to summary
private final int e
isNegativeback to summary
private final boolean isNegative
MAX_SMALL_TENback to summary
private static final int MAX_SMALL_TEN
nback to summary
private final int n
SINGLE_MAX_SMALL_TENback to summary
private static final int SINGLE_MAX_SMALL_TEN
SINGLE_SMALL_10_POWback to summary
private static final float[] SINGLE_SMALL_10_POW
Annotations
@Stable
SMALL_10_POWback to summary
private static final double[] SMALL_10_POW
Annotations
@Stable

Constructor Detail

ASCIIToBinaryBufferback to summary
private ASCIIToBinaryBuffer(boolean isNegative, int e, byte[] d, int n)

Method Detail

doubleValueback to summary
private double doubleValue()
floatValueback to summary
private float floatValue()
signedback to summary
private double signed(double v)
signedback to summary
private float signed(float v)
toLongback to summary
private long toLong(int n)
jdk.internal.math back to summary

public final Class FloatingDecimal.BinaryToASCIIBuffer

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private boolean
private final byte[]
private boolean
private int
private static final int[]
insignificantDigitsNumber

If insignificant==(1L << ixd) i = insignificantDigitsNumber[idx] is the same as: int i; for ( i = 0; insignificant >= 10L; i++ ) insignificant /= 10L;

private static final int[]
private int

Constructor Summary

AccessConstructor and Description
private
BinaryToASCIIBuffer()

Default constructor; used for non-zero values, BinaryToASCIIBuffer may be thread-local and reused

private
BinaryToASCIIBuffer(byte[] digits)

Creates a specialized value (positive and negative zeros).

Method Summary

Modifier and TypeMethod and Description
public boolean
private void
developLongDigits(long lvalue, int insignificantDigits)

This is the easy subcase -- all the significant bits, after scaling, are held in lvalue.

public boolean
private void
dtoa(int binExp, long fractBits, int nSignificantBits)

private static int
estimateDecExp(long fractBits, int binExp)

Estimate decimal exponent.

public int
public int
getDigits(byte[] digits)

private static int
insignificantDigitsForPow2(int p2)

Calculates

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

decExponentback to summary
private int decExponent
decimalDigitsRoundedUpback to summary
private boolean decimalDigitsRoundedUp
digitsback to summary
private final byte[] digits
exactDecimalConversionback to summary
private boolean exactDecimalConversion
firstDigitIndexback to summary
private int firstDigitIndex
insignificantDigitsNumberback to summary
private static final int[] insignificantDigitsNumber

If insignificant==(1L << ixd) i = insignificantDigitsNumber[idx] is the same as: int i; for ( i = 0; insignificant >= 10L; i++ ) insignificant /= 10L;

Annotations
@Stable
N_5_BITSback to summary
private static final int[] N_5_BITS
Annotations
@Stable
nDigitsback to summary
private int nDigits

Constructor Detail

BinaryToASCIIBufferback to summary
private BinaryToASCIIBuffer()

Default constructor; used for non-zero values, BinaryToASCIIBuffer may be thread-local and reused

BinaryToASCIIBufferback to summary
private BinaryToASCIIBuffer(byte[] digits)

Creates a specialized value (positive and negative zeros).

Method Detail

decimalDigitsExactback to summary
public boolean decimalDigitsExact()
developLongDigitsback to summary
private void developLongDigits(long lvalue, int insignificantDigits)

This is the easy subcase -- all the significant bits, after scaling, are held in lvalue. negSign and decExponent tell us what processing and scaling has already been done. Exceptional cases have already been stripped out. In particular: lvalue is a finite number (not Inf, nor NaN) lvalue > 0L (not zero, nor negative).

The only reason that we develop the digits here, rather than calling on Long.toString() is that we can do it a little faster, and besides want to treat trailing 0s specially. If Long.toString changes, we should re-evaluate this strategy!

digitsRoundedUpback to summary
public boolean digitsRoundedUp()
dtoaback to summary
private void dtoa(int binExp, long fractBits, int nSignificantBits)
estimateDecExpback to summary
private static int estimateDecExp(long fractBits, int binExp)

Estimate decimal exponent. (If it is small-ish, we could double-check.)

First, scale the mantissa bits such that 1 <= d2 < 2. We are then going to estimate log10(d2) ~=~ (d2-1.5)/1.5 + log(1.5) and so we can estimate log10(d) ~=~ log10(d2) + binExp * log10(2) take the floor and call it decExp.

getDecimalExponentback to summary
public int getDecimalExponent()
getDigitsback to summary
public int getDigits(byte[] digits)
insignificantDigitsForPow2back to summary
private static int insignificantDigitsForPow2(int p2)

Calculates

roundupback to summary
private void roundup()