Class FloatApfloatImpl
- java.lang.Object
-
- org.apfloat.internal.FloatBaseMath
-
- org.apfloat.internal.FloatApfloatImpl
-
- All Implemented Interfaces:
java.io.Serializable,ApfloatImpl
public class FloatApfloatImpl extends FloatBaseMath implements ApfloatImpl
Immutable apfloat implementation class for thefloatdata element type.The associated
DataStorageis assumed to be immutable also. This way performance can be improved by sharing the data storage between differentApfloatImpl's and by only varying theApfloatImplspecific 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 DataStoragedataStorageprivate longexponentprivate inthashCodeprivate intinitialDigitsprivate intisOneprivate longleastZerosprivate static intMAX_DOUBLE_SIZEprivate static intMAX_LONG_SIZEprivate longprecisionprivate intradixprivate static longserialVersionUIDprivate intsignprivate longsizeprivate static intUNDEFINEDprivate static DataStorage.IteratorZERO_ITERATOR
-
Constructor Summary
Constructors Modifier Constructor Description FloatApfloatImpl(double value, long precision, int radix)Create a newFloatApfloatImplinstance from adouble.privateFloatApfloatImpl(int sign, long precision, long exponent, DataStorage dataStorage, int radix)FloatApfloatImpl(long value, long precision, int radix)Create a newFloatApfloatImplinstance from along.FloatApfloatImpl(java.io.PushbackReader in, long precision, int radix, boolean isInteger)Create a newFloatApfloatImplinstance reading from a stream.FloatApfloatImpl(java.lang.String value, long precision, int radix, boolean isInteger)Create a newFloatApfloatImplinstance from a String.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ApfloatImplabsCeil()Returns thisApfloatImplrounded away from zero.ApfloatImplabsFloor()Returns thisApfloatImplrounded towards zero.ApfloatImpladdOrSubtract(ApfloatImpl x, boolean subtract)Add or subtract anApfloatImplto this object.private static intcheckRadix(int radix)private intcompareMantissaTo(FloatApfloatImpl that)intcompareTo(ApfloatImpl x)Compare thisApfloatImpland another number.private static DataStoragecreateDataStorage(long size)ApfloatImpldivideShort(ApfloatImpl x)Divide thisApfloatImplby anApfloatImplthat is "short".doubledoubleValue()Returns the value of the this number as adouble.longequalDigits(ApfloatImpl x)The number of equal digits in thisApfloatImpland another number.booleanequals(java.lang.Object obj)Compares this object to the specified object.private longfindMismatch(DataStorage.Iterator thisIterator, DataStorage.Iterator thatIterator, long size)private voidformatWord(char[] buffer, float word)ApfloatImplfrac()Returns the fractional part of thisApfloatImpl.private longgetBasePrecision()private longgetBasePrecision(long precision, int mswDigits)private static intgetBlockSize()private intgetDigits(float x)private intgetInitialDigits()private intgetInitialDigits(DataStorage dataStorage)private static longgetLeadingZeros(DataStorage dataStorage, long index)private floatgetLeastSignificantWord(long index, float word)private longgetLeastZeros()private floatgetMostSignificantWord()private static floatgetMostSignificantWord(DataStorage dataStorage)private longgetSize()private static longgetTrailingZeros(DataStorage dataStorage, long index)private floatgetWord(long index)private DataStorage.IteratorgetZeroPaddedIterator(long start, long end)inthashCode()Returns the hash code for thisApfloatImpl.booleanisOne()Tests if this number is equal to 1.booleanisShort()Returns if thisApfloatImplis "short".longlongValue()Returns the value of the this number as along.ApfloatImplmultiply(ApfloatImpl x)Multiply this object by anApfloatImpl.ApfloatImplnegate()Returns thisApfloatImplnegated.longprecision()Returns the precision of thisApfloatImpl.ApfloatImplprecision(long precision)Returns thisApfloatImplwith the specified precision.intradix()Returns the radix of thisApfloatImpl.private static longreadExponent(java.io.PushbackReader in)private voidreadObject(java.io.ObjectInputStream in)longscale()Returns the scale of thisApfloatImpl.intsignum()Returns the signum of thisApfloatImpl.longsize()Returns the size of the mantissa of thisApfloatImpl.java.lang.StringtoString(boolean pretty)Convert thisApfloatImpltoString.voidwriteTo(java.io.Writer out, boolean pretty)Print thisApfloatImplto a stream.private static voidwriteZeros(java.io.Writer out, long count)private ApfloatImplzero()-
Methods inherited from class org.apfloat.internal.FloatBaseMath
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
-
FloatApfloatImpl
private FloatApfloatImpl(int sign, long precision, long exponent, DataStorage dataStorage, int radix)
-
FloatApfloatImpl
public FloatApfloatImpl(java.lang.String value, long precision, int radix, boolean isInteger) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionCreate a newFloatApfloatImplinstance 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
-
FloatApfloatImpl
public FloatApfloatImpl(long value, long precision, int radix) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionCreate a newFloatApfloatImplinstance 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
-
FloatApfloatImpl
public FloatApfloatImpl(double value, long precision, int radix) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionCreate a newFloatApfloatImplinstance 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
-
FloatApfloatImpl
public FloatApfloatImpl(java.io.PushbackReader in, long precision, int radix, boolean isInteger) throws java.io.IOException, java.lang.NumberFormatException, ApfloatRuntimeExceptionCreate a newFloatApfloatImplinstance reading from a stream.Implementation note: this constructor calls the
instream's single-characterread()method. If the underlying stream doesn't explicitly implement this method in some efficient way, but simply inherits it from theReaderbase class, performance will suffer as the defaultReadermethod 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.IOExceptionjava.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:ApfloatImplAdd or subtract anApfloatImplto this object.- Specified by:
addOrSubtractin interfaceApfloatImpl- Parameters:
x- The number to be added or subtracted to thisApfloatImpl.subtract-trueif the numbers are to be subtracted,falseif added.- Returns:
this + xorthis - xdepending on thesubtractargument.- Throws:
ApfloatRuntimeException
-
multiply
public ApfloatImpl multiply(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImplMultiply this object by anApfloatImpl.- Specified by:
multiplyin interfaceApfloatImpl- Parameters:
x- The number to be multiplied by thisApfloatImpl.- Returns:
this * x.- Throws:
ApfloatRuntimeException
-
isShort
public boolean isShort() throws ApfloatRuntimeExceptionDescription copied from interface:ApfloatImplReturns if thisApfloatImplis "short". TypicallyApfloatImplis "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:
isShortin interfaceApfloatImpl- Returns:
trueif theApfloatImplis "short",falseif not.- Throws:
ApfloatRuntimeException- See Also:
Apfloat.isShort()
-
divideShort
public ApfloatImpl divideShort(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImplDivide thisApfloatImplby anApfloatImplthat is "short".- Specified by:
divideShortin interfaceApfloatImpl- Parameters:
x- The number by which thisApfloatImplis to be divided.- Returns:
this / x.- Throws:
ApfloatRuntimeException
-
absFloor
public ApfloatImpl absFloor() throws ApfloatRuntimeException
Description copied from interface:ApfloatImplReturns thisApfloatImplrounded towards zero.- Specified by:
absFloorin interfaceApfloatImpl- Returns:
- This
ApfloatImplrounded towards zero. - Throws:
ApfloatRuntimeException
-
absCeil
public ApfloatImpl absCeil() throws ApfloatRuntimeException
Description copied from interface:ApfloatImplReturns thisApfloatImplrounded away from zero.- Specified by:
absCeilin interfaceApfloatImpl- Returns:
- This
ApfloatImplrounded away from zero. - Throws:
ApfloatRuntimeException
-
frac
public ApfloatImpl frac() throws ApfloatRuntimeException
Description copied from interface:ApfloatImplReturns the fractional part of thisApfloatImpl.- Specified by:
fracin interfaceApfloatImpl- Returns:
- The fractional part of this
ApfloatImpl. - Throws:
ApfloatRuntimeException
-
zero
private ApfloatImpl zero()
-
radix
public int radix()
Description copied from interface:ApfloatImplReturns the radix of thisApfloatImpl.- Specified by:
radixin interfaceApfloatImpl- Returns:
- The radix of this
ApfloatImpl.
-
precision
public long precision()
Description copied from interface:ApfloatImplReturns the precision of thisApfloatImpl.- Specified by:
precisionin interfaceApfloatImpl- Returns:
- The precision of this
ApfloatImpl.
-
size
public long size() throws ApfloatRuntimeExceptionDescription copied from interface:ApfloatImplReturns the size of the mantissa of thisApfloatImpl.- Specified by:
sizein 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:ApfloatImplReturns thisApfloatImplwith the specified precision.- Specified by:
precisionin interfaceApfloatImpl- Parameters:
precision- The precision.- Returns:
- This
ApfloatImplwith the specified precision.
-
scale
public long scale() throws ApfloatRuntimeExceptionDescription copied from interface:ApfloatImplReturns the scale of thisApfloatImpl.- Specified by:
scalein interfaceApfloatImpl- Returns:
- The scale of this
ApfloatImpl. - Throws:
ApfloatRuntimeException- See Also:
Apfloat.scale()
-
signum
public int signum()
Description copied from interface:ApfloatImplReturns the signum of thisApfloatImpl.- Specified by:
signumin interfaceApfloatImpl- Returns:
- The signum of this
ApfloatImpl. - See Also:
Apfloat.signum()
-
negate
public ApfloatImpl negate() throws ApfloatRuntimeException
Description copied from interface:ApfloatImplReturns thisApfloatImplnegated.- Specified by:
negatein interfaceApfloatImpl- Returns:
-this.- Throws:
ApfloatRuntimeException
-
doubleValue
public double doubleValue()
Description copied from interface:ApfloatImplReturns the value of the this number as adouble. If the number is greater thanDouble.MAX_VALUE, thenDouble.POSITIVE_INFINITYis returned.If the number is less than
Double.MIN_VALUE, thenDouble.NEGATIVE_INFINITYis returned.If the number is very small in magnitude, underflow may happen and zero is returned.
- Specified by:
doubleValuein interfaceApfloatImpl- Returns:
- The numeric value represented by this object after conversion to type
double.
-
longValue
public long longValue()
Description copied from interface:ApfloatImplReturns 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_VALUEis returned.If the number is less than
Long.MIN_VALUE, thenLong.MIN_VALUEis returned.- Specified by:
longValuein interfaceApfloatImpl- Returns:
- The numeric value represented by this object after conversion to type
long.
-
isOne
public boolean isOne() throws ApfloatRuntimeExceptionDescription copied from interface:ApfloatImplTests if this number is equal to 1.- Specified by:
isOnein interfaceApfloatImpl- Returns:
trueif this number is equal to one, otherwisefalse.- Throws:
ApfloatRuntimeException
-
equalDigits
public long equalDigits(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImplThe number of equal digits in thisApfloatImpland another number.- Specified by:
equalDigitsin interfaceApfloatImpl- Parameters:
x- The number to compare with.- Returns:
- The number of equal digits in this
ApfloatImplandx. - Throws:
ApfloatRuntimeException- See Also:
Apfloat.equalDigits(org.apfloat.Apfloat)
-
compareTo
public int compareTo(ApfloatImpl x) throws ApfloatRuntimeException
Description copied from interface:ApfloatImplCompare thisApfloatImpland another number.- Specified by:
compareToin 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(FloatApfloatImpl that) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
findMismatch
private long findMismatch(DataStorage.Iterator thisIterator, DataStorage.Iterator thatIterator, long size) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getLeastSignificantWord
private float getLeastSignificantWord(long index, float word) throws ApfloatRuntimeException- Throws:
ApfloatRuntimeException
-
equals
public boolean equals(java.lang.Object obj)
Compares this object to the specified object.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- The object to compare with.- Returns:
trueif the objects are equal;falseotherwise.
-
hashCode
public int hashCode()
Description copied from interface:ApfloatImplReturns the hash code for thisApfloatImpl.- Specified by:
hashCodein interfaceApfloatImpl- Overrides:
hashCodein classjava.lang.Object- Returns:
- The hash code for this
ApfloatImpl.
-
toString
public java.lang.String toString(boolean pretty) throws ApfloatRuntimeExceptionDescription copied from interface:ApfloatImplConvert thisApfloatImpltoString.- Specified by:
toStringin 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, ApfloatRuntimeExceptionDescription copied from interface:ApfloatImplPrint thisApfloatImplto a stream.- Specified by:
writeToin 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, float 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 float getMostSignificantWord() throws ApfloatRuntimeException- Throws:
ApfloatRuntimeException
-
getMostSignificantWord
private static float 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(float x)
-
getBasePrecision
private long getBasePrecision() throws ApfloatRuntimeException- Throws:
ApfloatRuntimeException
-
getBasePrecision
private long getBasePrecision(long precision, int mswDigits)
-
getWord
private float getWord(long index)
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
createDataStorage
private static DataStorage createDataStorage(long size) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getBlockSize
private static int getBlockSize()
-
-