Class DoubleApfloatImpl
- java.lang.Object
-
- org.apfloat.internal.DoubleBaseMath
-
- org.apfloat.internal.DoubleApfloatImpl
-
- All Implemented Interfaces:
java.io.Serializable
,ApfloatImpl
public class DoubleApfloatImpl extends DoubleBaseMath implements ApfloatImpl
Immutable apfloat implementation class for thedouble
data element type.The associated
DataStorage
is assumed to be immutable also. This way performance can be improved by sharing the data storage between differentApfloatImpl
's and by only varying theApfloatImpl
specific fields, like sign, precision and exponent.This implementation doesn't necessarily store any extra digits for added precision, so the last digit of any operation may be inaccurate.
- Version:
- 1.11.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private DataStorage
dataStorage
private long
exponent
private int
hashCode
private int
initialDigits
private int
isOne
private long
leastZeros
private static int
MAX_DOUBLE_SIZE
private static int
MAX_LONG_SIZE
private long
precision
private int
radix
private static long
serialVersionUID
private int
sign
private long
size
private static int
UNDEFINED
private static DataStorage.Iterator
ZERO_ITERATOR
-
Constructor Summary
Constructors Modifier Constructor Description DoubleApfloatImpl(double value, long precision, int radix)
Create a newDoubleApfloatImpl
instance from adouble
.private
DoubleApfloatImpl(int sign, long precision, long exponent, DataStorage dataStorage, int radix)
DoubleApfloatImpl(long value, long precision, int radix)
Create a newDoubleApfloatImpl
instance from along
.DoubleApfloatImpl(java.io.PushbackReader in, long precision, int radix, boolean isInteger)
Create a newDoubleApfloatImpl
instance reading from a stream.DoubleApfloatImpl(java.lang.String value, long precision, int radix, boolean isInteger)
Create a newDoubleApfloatImpl
instance from a String.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ApfloatImpl
absCeil()
Returns thisApfloatImpl
rounded away from zero.ApfloatImpl
absFloor()
Returns thisApfloatImpl
rounded towards zero.ApfloatImpl
addOrSubtract(ApfloatImpl x, boolean subtract)
Add or subtract anApfloatImpl
to this object.private static int
checkRadix(int radix)
private int
compareMantissaTo(DoubleApfloatImpl that)
int
compareTo(ApfloatImpl x)
Compare thisApfloatImpl
and another number.private static DataStorage
createDataStorage(long size)
ApfloatImpl
divideShort(ApfloatImpl x)
Divide thisApfloatImpl
by anApfloatImpl
that is "short".double
doubleValue()
Returns the value of the this number as adouble
.long
equalDigits(ApfloatImpl x)
The number of equal digits in thisApfloatImpl
and another number.boolean
equals(java.lang.Object obj)
Compares this object to the specified object.private long
findMismatch(DataStorage.Iterator thisIterator, DataStorage.Iterator thatIterator, long size)
private void
formatWord(char[] buffer, double word)
ApfloatImpl
frac()
Returns the fractional part of thisApfloatImpl
.private long
getBasePrecision()
private long
getBasePrecision(long precision, int mswDigits)
private static int
getBlockSize()
private int
getDigits(double x)
private int
getInitialDigits()
private int
getInitialDigits(DataStorage dataStorage)
private static long
getLeadingZeros(DataStorage dataStorage, long index)
private double
getLeastSignificantWord(long index, double word)
private long
getLeastZeros()
private double
getMostSignificantWord()
private static double
getMostSignificantWord(DataStorage dataStorage)
private long
getSize()
private static long
getTrailingZeros(DataStorage dataStorage, long index)
private double
getWord(long index)
private DataStorage.Iterator
getZeroPaddedIterator(long start, long end)
int
hashCode()
Returns the hash code for thisApfloatImpl
.boolean
isOne()
Tests if this number is equal to 1.boolean
isShort()
Returns if thisApfloatImpl
is "short".long
longValue()
Returns the value of the this number as along
.ApfloatImpl
multiply(ApfloatImpl x)
Multiply this object by anApfloatImpl
.ApfloatImpl
negate()
Returns thisApfloatImpl
negated.long
precision()
Returns the precision of thisApfloatImpl
.ApfloatImpl
precision(long precision)
Returns thisApfloatImpl
with the specified precision.int
radix()
Returns the radix of thisApfloatImpl
.private static long
readExponent(java.io.PushbackReader in)
private void
readObject(java.io.ObjectInputStream in)
long
scale()
Returns the scale of thisApfloatImpl
.int
signum()
Returns the signum of thisApfloatImpl
.long
size()
Returns the size of the mantissa of thisApfloatImpl
.java.lang.String
toString(boolean pretty)
Convert thisApfloatImpl
toString
.void
writeTo(java.io.Writer out, boolean pretty)
Print thisApfloatImpl
to a stream.private static void
writeZeros(java.io.Writer out, long count)
private ApfloatImpl
zero()
-
Methods inherited from class org.apfloat.internal.DoubleBaseMath
baseAdd, baseDivide, baseMultiplyAdd, baseSubtract
-
-
-
-
Field Detail
-
ZERO_ITERATOR
private static final DataStorage.Iterator ZERO_ITERATOR
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
UNDEFINED
private static final int UNDEFINED
- See Also:
- Constant Field Values
-
MAX_LONG_SIZE
private static final int MAX_LONG_SIZE
- See Also:
- Constant Field Values
-
MAX_DOUBLE_SIZE
private static final int MAX_DOUBLE_SIZE
- See Also:
- Constant Field Values
-
sign
private int sign
-
precision
private long precision
-
exponent
private long exponent
-
dataStorage
private DataStorage dataStorage
-
radix
private int radix
-
hashCode
private int hashCode
-
initialDigits
private int initialDigits
-
isOne
private int isOne
-
leastZeros
private volatile long leastZeros
-
size
private volatile long size
-
-
Constructor Detail
-
DoubleApfloatImpl
private DoubleApfloatImpl(int sign, long precision, long exponent, DataStorage dataStorage, int radix)
-
DoubleApfloatImpl
public DoubleApfloatImpl(java.lang.String value, long precision, int radix, boolean isInteger) throws java.lang.NumberFormatException, ApfloatRuntimeException
Create a newDoubleApfloatImpl
instance from a String.- Parameters:
value
- The string to be parsed to a number.precision
- The precision of the number (in digits of the radix).radix
- The radix in which the number is created.isInteger
- Specifies if the number to be parsed from the string is to be treated as an integer or not.- Throws:
java.lang.NumberFormatException
- If the number is not valid.ApfloatRuntimeException
-
DoubleApfloatImpl
public DoubleApfloatImpl(long value, long precision, int radix) throws java.lang.NumberFormatException, ApfloatRuntimeException
Create a newDoubleApfloatImpl
instance from along
.- Parameters:
value
- The value of the number.precision
- The precision of the number (in digits of the radix).radix
- The radix in which the number is created.- Throws:
java.lang.NumberFormatException
- If the number is not valid.ApfloatRuntimeException
-
DoubleApfloatImpl
public DoubleApfloatImpl(double value, long precision, int radix) throws java.lang.NumberFormatException, ApfloatRuntimeException
Create a newDoubleApfloatImpl
instance from adouble
.- Parameters:
value
- The value of the number.precision
- The precision of the number (in digits of the radix).radix
- The radix in which the number is created.- Throws:
java.lang.NumberFormatException
- If the number is not valid.ApfloatRuntimeException
-
DoubleApfloatImpl
public DoubleApfloatImpl(java.io.PushbackReader in, long precision, int radix, boolean isInteger) throws java.io.IOException, java.lang.NumberFormatException, ApfloatRuntimeException
Create a newDoubleApfloatImpl
instance reading from a stream.Implementation note: this constructor calls the
in
stream's single-characterread()
method. If the underlying stream doesn't explicitly implement this method in some efficient way, but simply inherits it from theReader
base class, performance will suffer as the defaultReader
method creates anew char[1]
on every call toread()
.- Parameters:
in
- The stream to read from.precision
- The precision of the number (in digits of the radix).radix
- The radix in which the number is created.isInteger
- Specifies if the number to be parsed from the stream is to be treated as an integer or not.- Throws:
java.io.IOException
- If an I/O error occurs accessing the stream.java.lang.NumberFormatException
- If the number is not valid.ApfloatRuntimeException
-
-
Method Detail
-
readExponent
private static long readExponent(java.io.PushbackReader in) throws java.io.IOException, java.lang.NumberFormatException
- Throws:
java.io.IOException
java.lang.NumberFormatException
-
getTrailingZeros
private static long getTrailingZeros(DataStorage dataStorage, long index) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getLeadingZeros
private static long getLeadingZeros(DataStorage dataStorage, long index) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
addOrSubtract
public ApfloatImpl addOrSubtract(ApfloatImpl x, boolean subtract) throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Add or subtract anApfloatImpl
to this object.- Specified by:
addOrSubtract
in interfaceApfloatImpl
- Parameters:
x
- The number to be added or subtracted to thisApfloatImpl
.subtract
-true
if the numbers are to be subtracted,false
if added.- Returns:
this + x
orthis - x
depending on thesubtract
argument.- Throws:
ApfloatRuntimeException
-
multiply
public ApfloatImpl multiply(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Multiply this object by anApfloatImpl
.- Specified by:
multiply
in interfaceApfloatImpl
- Parameters:
x
- The number to be multiplied by thisApfloatImpl
.- Returns:
this * x
.- Throws:
ApfloatRuntimeException
-
isShort
public boolean isShort() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Returns if thisApfloatImpl
is "short". TypicallyApfloatImpl
is "short" if its mantissa fits in one machine word. If the apfloat is "short", some algorithms can be performed faster.The return value of this method is highly implementation dependent.
- Specified by:
isShort
in interfaceApfloatImpl
- Returns:
true
if theApfloatImpl
is "short",false
if not.- Throws:
ApfloatRuntimeException
- See Also:
Apfloat.isShort()
-
divideShort
public ApfloatImpl divideShort(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Divide thisApfloatImpl
by anApfloatImpl
that is "short".- Specified by:
divideShort
in interfaceApfloatImpl
- Parameters:
x
- The number by which thisApfloatImpl
is to be divided.- Returns:
this / x
.- Throws:
ApfloatRuntimeException
-
absFloor
public ApfloatImpl absFloor() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Returns thisApfloatImpl
rounded towards zero.- Specified by:
absFloor
in interfaceApfloatImpl
- Returns:
- This
ApfloatImpl
rounded towards zero. - Throws:
ApfloatRuntimeException
-
absCeil
public ApfloatImpl absCeil() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Returns thisApfloatImpl
rounded away from zero.- Specified by:
absCeil
in interfaceApfloatImpl
- Returns:
- This
ApfloatImpl
rounded away from zero. - Throws:
ApfloatRuntimeException
-
frac
public ApfloatImpl frac() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Returns the fractional part of thisApfloatImpl
.- Specified by:
frac
in interfaceApfloatImpl
- Returns:
- The fractional part of this
ApfloatImpl
. - Throws:
ApfloatRuntimeException
-
zero
private ApfloatImpl zero()
-
radix
public int radix()
Description copied from interface:ApfloatImpl
Returns the radix of thisApfloatImpl
.- Specified by:
radix
in interfaceApfloatImpl
- Returns:
- The radix of this
ApfloatImpl
.
-
precision
public long precision()
Description copied from interface:ApfloatImpl
Returns the precision of thisApfloatImpl
.- Specified by:
precision
in interfaceApfloatImpl
- Returns:
- The precision of this
ApfloatImpl
.
-
size
public long size() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Returns the size of the mantissa of thisApfloatImpl
.- Specified by:
size
in interfaceApfloatImpl
- Returns:
- The size of the mantissa of this
ApfloatImpl
. - Throws:
ApfloatRuntimeException
-
getLeastZeros
private long getLeastZeros() throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
precision
public ApfloatImpl precision(long precision)
Description copied from interface:ApfloatImpl
Returns thisApfloatImpl
with the specified precision.- Specified by:
precision
in interfaceApfloatImpl
- Parameters:
precision
- The precision.- Returns:
- This
ApfloatImpl
with the specified precision.
-
scale
public long scale() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Returns the scale of thisApfloatImpl
.- Specified by:
scale
in interfaceApfloatImpl
- Returns:
- The scale of this
ApfloatImpl
. - Throws:
ApfloatRuntimeException
- See Also:
Apfloat.scale()
-
signum
public int signum()
Description copied from interface:ApfloatImpl
Returns the signum of thisApfloatImpl
.- Specified by:
signum
in interfaceApfloatImpl
- Returns:
- The signum of this
ApfloatImpl
. - See Also:
Apfloat.signum()
-
negate
public ApfloatImpl negate() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Returns thisApfloatImpl
negated.- Specified by:
negate
in interfaceApfloatImpl
- Returns:
-this
.- Throws:
ApfloatRuntimeException
-
doubleValue
public double doubleValue()
Description copied from interface:ApfloatImpl
Returns the value of the this number as adouble
. If the number is greater thanDouble.MAX_VALUE
, thenDouble.POSITIVE_INFINITY
is returned.If the number is less than
Double.MIN_VALUE
, thenDouble.NEGATIVE_INFINITY
is returned.If the number is very small in magnitude, underflow may happen and zero is returned.
- Specified by:
doubleValue
in interfaceApfloatImpl
- Returns:
- The numeric value represented by this object after conversion to type
double
.
-
longValue
public long longValue()
Description copied from interface:ApfloatImpl
Returns the value of the this number as along
. The fractional part is truncated towards zero.If the number is greater than
Long.MAX_VALUE
, thenLong.MAX_VALUE
is returned.If the number is less than
Long.MIN_VALUE
, thenLong.MIN_VALUE
is returned.- Specified by:
longValue
in interfaceApfloatImpl
- Returns:
- The numeric value represented by this object after conversion to type
long
.
-
isOne
public boolean isOne() throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Tests if this number is equal to 1.- Specified by:
isOne
in interfaceApfloatImpl
- Returns:
true
if this number is equal to one, otherwisefalse
.- Throws:
ApfloatRuntimeException
-
equalDigits
public long equalDigits(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
The number of equal digits in thisApfloatImpl
and another number.- Specified by:
equalDigits
in interfaceApfloatImpl
- Parameters:
x
- The number to compare with.- Returns:
- The number of equal digits in this
ApfloatImpl
andx
. - Throws:
ApfloatRuntimeException
- See Also:
Apfloat.equalDigits(org.apfloat.Apfloat)
-
compareTo
public int compareTo(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Compare thisApfloatImpl
and another number.- Specified by:
compareTo
in interfaceApfloatImpl
- Parameters:
x
- The number to compare with.- Returns:
- Zero, less than zero or greater than zero depending on the result of the comparison.
- Throws:
ApfloatRuntimeException
- See Also:
Apfloat.compareTo(org.apfloat.Apfloat)
-
getZeroPaddedIterator
private DataStorage.Iterator getZeroPaddedIterator(long start, long end) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
compareMantissaTo
private int compareMantissaTo(DoubleApfloatImpl that) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
findMismatch
private long findMismatch(DataStorage.Iterator thisIterator, DataStorage.Iterator thatIterator, long size) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getLeastSignificantWord
private double getLeastSignificantWord(long index, double word) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
equals
public boolean equals(java.lang.Object obj)
Compares this object to the specified object.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- The object to compare with.- Returns:
true
if the objects are equal;false
otherwise.
-
hashCode
public int hashCode()
Description copied from interface:ApfloatImpl
Returns the hash code for thisApfloatImpl
.- Specified by:
hashCode
in interfaceApfloatImpl
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- The hash code for this
ApfloatImpl
.
-
toString
public java.lang.String toString(boolean pretty) throws ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Convert thisApfloatImpl
toString
.- Specified by:
toString
in interfaceApfloatImpl
- Parameters:
pretty
- Flag for formatting.- Returns:
- String representation of this
ApfloatImpl
. - Throws:
ApfloatRuntimeException
- See Also:
Apfloat.toString(boolean)
-
writeZeros
private static void writeZeros(java.io.Writer out, long count) throws java.io.IOException
- Throws:
java.io.IOException
-
writeTo
public void writeTo(java.io.Writer out, boolean pretty) throws java.io.IOException, ApfloatRuntimeException
Description copied from interface:ApfloatImpl
Print thisApfloatImpl
to a stream.- Specified by:
writeTo
in interfaceApfloatImpl
- Parameters:
out
- The stream to write to.pretty
- Flag for formatting.- Throws:
java.io.IOException
- In case of I/O error writing to the stream.ApfloatRuntimeException
- See Also:
Apfloat.writeTo(Writer,boolean)
-
formatWord
private void formatWord(char[] buffer, double word)
-
getSize
private long getSize() throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
checkRadix
private static int checkRadix(int radix) throws java.lang.NumberFormatException
- Throws:
java.lang.NumberFormatException
-
getMostSignificantWord
private double getMostSignificantWord() throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getMostSignificantWord
private static double getMostSignificantWord(DataStorage dataStorage) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getInitialDigits
private int getInitialDigits() throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getInitialDigits
private int getInitialDigits(DataStorage dataStorage) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getDigits
private int getDigits(double x)
-
getBasePrecision
private long getBasePrecision() throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getBasePrecision
private long getBasePrecision(long precision, int mswDigits)
-
getWord
private double getWord(long index)
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
createDataStorage
private static DataStorage createDataStorage(long size) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getBlockSize
private static int getBlockSize()
-
-