org.apfloat.internal
Class LongApfloatImpl

java.lang.Object
  extended byorg.apfloat.internal.LongBaseMath
      extended byorg.apfloat.internal.LongApfloatImpl
All Implemented Interfaces:
ApfloatImpl, LongRadixConstants, RadixConstants, java.io.Serializable

public final class LongApfloatImpl
extends LongBaseMath
implements ApfloatImpl, RadixConstants, LongRadixConstants

Immutable apfloat implementation class for the long data element type.

The associated DataStorage is assumed to be immutable also. This way performance can be improved by sharing the data storage between different ApfloatImpl's and by only varying the ApfloatImpl 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.0.2
Author:
Mikko Tommila
See Also:
Serialized Form

Field Summary
 
Fields inherited from interface org.apfloat.spi.RadixConstants
DOUBLE_PRECISION, FLOAT_PRECISION, LONG_PRECISION, RADIX_FACTORS
 
Fields inherited from interface org.apfloat.internal.LongRadixConstants
BASE, BASE_DIGITS, MAX_EXPONENT, MINIMUM_FOR_DIGITS
 
Constructor Summary
LongApfloatImpl(double value, long precision, int radix)
          Create a new LongApfloatImpl instance from a double.
LongApfloatImpl(long value, long precision, int radix)
          Create a new LongApfloatImpl instance from a long.
LongApfloatImpl(java.io.PushbackReader in, long precision, int radix, boolean isInteger)
          Create a new LongApfloatImpl instance reading from a stream.
LongApfloatImpl(java.lang.String value, long precision, int radix, boolean isInteger)
          Create a new LongApfloatImpl instance from a String.
 
Method Summary
 ApfloatImpl absCeil()
          Returns this ApfloatImpl rounded away from zero.
 ApfloatImpl absFloor()
          Returns this ApfloatImpl rounded towards zero.
 ApfloatImpl addOrSubtract(ApfloatImpl x, boolean subtract)
          Add or subtract an ApfloatImpl to this object.
 int compareTo(ApfloatImpl x)
          Compare this ApfloatImpl and another number.
 ApfloatImpl divideShort(ApfloatImpl x)
          Divide this ApfloatImpl by an ApfloatImpl that is "short".
 double doubleValue()
          Returns the value of the this number as a double.
 long equalDigits(ApfloatImpl x)
          The number of equal digits in this ApfloatImpl and another number.
 boolean equals(java.lang.Object obj)
          Compares this object to the specified object.
 int hashCode()
          Returns the hash code for this ApfloatImpl.
 boolean isOne()
          Tests if this number is equal to 1.
 boolean isShort()
          Returns if this ApfloatImpl is "short".
 long longValue()
          Returns the value of the this number as a long.
 ApfloatImpl multiply(ApfloatImpl x)
          Multiply this object by an ApfloatImpl.
 ApfloatImpl negate()
          Returns this ApfloatImpl negated.
 long precision()
          Returns the precision of this ApfloatImpl.
 ApfloatImpl precision(long precision)
          Returns this ApfloatImpl with the specified precision.
 int radix()
          Returns the radix of this ApfloatImpl.
 long scale()
          Returns the scale of this ApfloatImpl.
 int signum()
          Returns the signum of this ApfloatImpl.
 java.lang.String toString(boolean pretty)
          Convert this ApfloatImpl to String.
 void writeTo(java.io.Writer out, boolean pretty)
          Print this ApfloatImpl to a stream.
 
Methods inherited from class org.apfloat.internal.LongBaseMath
baseAdd, baseDivide, baseMultiplyAdd, baseSubtract
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LongApfloatImpl

public LongApfloatImpl(java.lang.String value,
                       long precision,
                       int radix,
                       boolean isInteger)
                throws java.lang.NumberFormatException,
                       ApfloatRuntimeException
Create a new LongApfloatImpl 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

LongApfloatImpl

public LongApfloatImpl(long value,
                       long precision,
                       int radix)
                throws java.lang.NumberFormatException,
                       ApfloatRuntimeException
Create a new LongApfloatImpl instance from a long.

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

LongApfloatImpl

public LongApfloatImpl(double value,
                       long precision,
                       int radix)
                throws java.lang.NumberFormatException,
                       ApfloatRuntimeException
Create a new LongApfloatImpl instance from a double.

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

LongApfloatImpl

public LongApfloatImpl(java.io.PushbackReader in,
                       long precision,
                       int radix,
                       boolean isInteger)
                throws java.io.IOException,
                       java.lang.NumberFormatException,
                       ApfloatRuntimeException
Create a new LongApfloatImpl instance reading from a stream.

Implementation note: this constructor calls the in stream's single-character read() method. If the underlying stream doesn't explicitly implement this method in some efficient way, but simply inherits it from the Reader base class, performance will suffer as the default Reader method creates a new char[1] on every call to read().

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

addOrSubtract

public ApfloatImpl addOrSubtract(ApfloatImpl x,
                                 boolean subtract)
                          throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Add or subtract an ApfloatImpl to this object.

Specified by:
addOrSubtract in interface ApfloatImpl
Parameters:
x - The number to be added or subtracted to this ApfloatImpl.
subtract - true if the numbers are to be subtracted, false if added.
Returns:
this + x or this - x depending on the subtract argument.
Throws:
ApfloatRuntimeException

multiply

public ApfloatImpl multiply(ApfloatImpl x)
                     throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Multiply this object by an ApfloatImpl.

Specified by:
multiply in interface ApfloatImpl
Parameters:
x - The number to be multiplied by this ApfloatImpl.
Returns:
this * x.
Throws:
ApfloatRuntimeException

isShort

public boolean isShort()
                throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Returns if this ApfloatImpl is "short". Typically ApfloatImpl 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 interface ApfloatImpl
Returns:
true if the ApfloatImpl 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 this ApfloatImpl by an ApfloatImpl that is "short".

Specified by:
divideShort in interface ApfloatImpl
Parameters:
x - The number by which this ApfloatImpl is to be divided.
Returns:
this / x.
Throws:
ApfloatRuntimeException

absFloor

public ApfloatImpl absFloor()
                     throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Returns this ApfloatImpl rounded towards zero.

Specified by:
absFloor in interface ApfloatImpl
Returns:
This ApfloatImpl rounded towards zero.
Throws:
ApfloatRuntimeException

absCeil

public ApfloatImpl absCeil()
                    throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Returns this ApfloatImpl rounded away from zero.

Specified by:
absCeil in interface ApfloatImpl
Returns:
This ApfloatImpl rounded away from zero.
Throws:
ApfloatRuntimeException

radix

public int radix()
Description copied from interface: ApfloatImpl
Returns the radix of this ApfloatImpl.

Specified by:
radix in interface ApfloatImpl
Returns:
The radix of this ApfloatImpl.

precision

public long precision()
Description copied from interface: ApfloatImpl
Returns the precision of this ApfloatImpl.

Specified by:
precision in interface ApfloatImpl
Returns:
The precision of this ApfloatImpl.

precision

public ApfloatImpl precision(long precision)
Description copied from interface: ApfloatImpl
Returns this ApfloatImpl with the specified precision.

Specified by:
precision in interface ApfloatImpl
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 this ApfloatImpl.

Specified by:
scale in interface ApfloatImpl
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 this ApfloatImpl.

Specified by:
signum in interface ApfloatImpl
Returns:
The signum of this ApfloatImpl.
See Also:
Apfloat.signum()

negate

public ApfloatImpl negate()
                   throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Returns this ApfloatImpl negated.

Specified by:
negate in interface ApfloatImpl
Returns:
-this.
Throws:
ApfloatRuntimeException

doubleValue

public double doubleValue()
Description copied from interface: ApfloatImpl
Returns the value of the this number as a double. If the number is greater than Double.MAX_VALUE, then Double.POSITIVE_INFINITY is returned.

If the number is less than Double.MIN_VALUE, then Double.NEGATIVE_INFINITY is returned.

If the number is very small in magnitude, underflow may happen and zero is returned.

Specified by:
doubleValue in interface ApfloatImpl
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 a long. The fractional part is truncated towards zero.

If the number is greater than Long.MAX_VALUE, then Long.MAX_VALUE is returned.

If the number is less than Long.MIN_VALUE, then Long.MIN_VALUE is returned.

Specified by:
longValue in interface ApfloatImpl
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 interface ApfloatImpl
Returns:
true if this number is equal to one, otherwise false.
Throws:
ApfloatRuntimeException

equalDigits

public long equalDigits(ApfloatImpl x)
                 throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
The number of equal digits in this ApfloatImpl and another number.

Specified by:
equalDigits in interface ApfloatImpl
Parameters:
x - The number to compare with.
Returns:
The number of equal digits in this ApfloatImpl and x.
Throws:
ApfloatRuntimeException
See Also:
Apfloat.equalDigits(Apfloat)

compareTo

public int compareTo(ApfloatImpl x)
              throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Compare this ApfloatImpl and another number.

Specified by:
compareTo in interface ApfloatImpl
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(Apfloat)

equals

public boolean equals(java.lang.Object obj)
Compares this object to the specified 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 this ApfloatImpl.

Specified by:
hashCode in interface ApfloatImpl

toString

public java.lang.String toString(boolean pretty)
                          throws ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Convert this ApfloatImpl to String.

Specified by:
toString in interface ApfloatImpl
Parameters:
pretty - Flag for formatting.
Returns:
String representation of this ApfloatImpl.
Throws:
ApfloatRuntimeException
See Also:
Apfloat.toString(boolean)

writeTo

public void writeTo(java.io.Writer out,
                    boolean pretty)
             throws java.io.IOException,
                    ApfloatRuntimeException
Description copied from interface: ApfloatImpl
Print this ApfloatImpl to a stream.

Specified by:
writeTo in interface ApfloatImpl
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)