Class MPF
java.lang.Object
java.lang.Number
it.unich.jgmp.MPF
- All Implemented Interfaces:
Serializable, Comparable<MPF>
Multi-precision floating point numbers. This class encapsulates the
mpf_t data type, see the
Floating-point Functions page of the GMP manual. In determining
the names and signatures of the methods of the MPF class, we adopt
the rules described in the documentation of the it.unich.jgmp
package, enriched with the following ones:
- the function
mpf_set_prec_rawis not exposed by theMPFclass; - the functions in the "I/O of Floats" category are not exposed by
the
MPFclass.
Every method which returns a new MPF, use the default precision set
by the setDefaultPrec(long) method, with the excpetion of the init2(long)
method where precision is specificied explicitly.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classCleaning action for theMPFclass. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate MpfTThe pointer to the nativempf_tobject.private static final longVersion for serializability. -
Constructor Summary
ConstructorsModifierConstructorDescriptionMPF()Build anMPFwhose value is zero.MPF(double op) Build anMPFwhose value isop, possibly truncated to the default precision.MPF(long op) Build anMPFwhose value isop, possibly truncated to the default precision.Build anMPFwhose value isop, possibly truncated to the default precision.Build anMPFwhose value isop, possibly truncated to the default precision.Build anMPFwhose value isop, possibly truncated to the default precision.privateA private constructor which build anMPFstarting from a pointer to its native data object.Build anMPFwhose value is the number represented by the stringstrin decimal base, possibly truncated to the default precision.Build anMPFwhose value is the number represented by the stringstrin the specifiedbase, possibly truncated to the default precision.MPF(BigDecimal op) Builds anMPFwhose value is the same asop. -
Method Summary
Modifier and TypeMethodDescriptionabs()Return anMPFwhose value is the absolute value ofthis.Set thisMPFto its absolute value.Set thisMPFto the absolute value ofop.Return anMPFwhose value is(this + op).Set thisMPFto(this + op)Set thisMPFto(op1 + op2).addUi(long op) Return anMPFwhose value is(this + op).addUiAssign(long op) Set thisMPFto(this + op)addUiAssign(MPF op1, long op2) Set thisMPFto(op1 + op2).ceil()Return anMPFwhose value isthisrounded to the next higher integer.Set thisMPFto its value rounded to the next higher integer.ceilAssign(MPF op) Set thisMPFto the value ofoprounded to the next higher integer.intcmp(double op) Comparethiswithop.intcmp(long op) Comparethiswithop.intComparethiswithop.intComparethiswithop.intcmpUi(long op) Comparethiswithop.intCompare thisMPFwithop.Return anMPFwhose value is(this / op).div2Exp(long b) Return anMPFwhose value is(this * 2b).div2ExpAssign(long b) Set thisMPFto(this / 2b).div2ExpAssign(MPF op, long b) Set thisMPFto(op / 2b).Set thisMPFto(this / op)Set thisMPFto(op1 / op2).divUi(long op) Return anMPFwhose value is(this / op).divUiAssign(long op) Set thisMPFto(this / op)divUiAssign(MPF op1, long op2) Set thisMPFto(op1 / op2).doubleConvert thisMPFto a double, truncating if necessary.booleanCompare thisMPFwith the objectopfor equality.booleanfitsSint()Returntrueif and only if thisMPFfits into a native signed int.booleanReturntrueif and only if thisMPFfits into a native signed long.booleanReturntrueif and only if thisMPFfits into a native signed short.booleanfitsUint()Returntrueif and only if thisMPFfits into a native unsigned int.booleanReturntrueif and only if thisMPFfits into a native unsigned long.booleanReturntrueif and only if thisMPFfits into a native unsigned short.floatConvert thisMPFto a float, truncating if necessary.floor()Return anMPFwhose value isthisrounded to the next lower integer.Set thisMPFto its value rounded to the next lower integer.floorAssign(MPF op) Set thisMPFto the value ofoprounded to the next lower integer.Convertthisto aBigDecimal.doublegetD()Convert thisMPFto a double, truncating if necessary.getD2Exp()Convert thisMPFto a pair made of mantissa and exponent, truncating if necessary.static longReturn the default precision actually used.Return the native pointer to the GMP object.longgetPrec()Return the current precision of thisMPF, in bits.longgetSi()Convert thisMPFto a long, truncating any fraction part.getStr(int base, long nDigits) Return the String representation of thisMPFin the specifiedbase, ornullif the base is not valid.longgetUi()Convert thisMPFto an unsigned long, truncating any fraction part.inthashCode()Return a hash code value for thisMPF.static MPFinit()Return anMPFwhose value is zero.static MPFinit2(long prec) Return anMPFwhose value is zero, and set its precision to be at leastprecbits.static MPFinitSet(double op) Return anMPFwhose value isop, possibly truncated to the default precision.static MPFinitSet(long op) Return anMPFwhose value isop, possibly truncated to the default precision.static MPFReturn anMPFwhose value isop, possibly truncated to the default precision.Return anMPFwhose value is the number represented by the stringstrin the specifiedbase, possibly truncated to the default precision.static MPFinitSetUi(long op) Return anMPFwhose value isop, possibly truncated to the default precision.intintValue()Convert thisMPFto anint.booleanReturn whether thisMPFis an integer.booleanisZero()Return true if and only if thisthisMPF is zero.longConvert thisMPFto along, truncating any fraction part.Return anMPFwhose value is(this * op).mul2Exp(long b) Return anMPFwhose value is(this * 2b).mul2ExpAssign(long b) Set thisMPFto(this * 2b).mul2ExpAssign(MPF op, long b) Set thisMPFto(op * 2b).Set thisMPFto(this * op)Set thisMPFto(op1 * op2).mulUi(long op) Return anMPFwhose value is(this * op).mulUiAssign(long op) Set thisMPFto(this * op)mulUiAssign(MPF op1, long op2) Set thisMPFto(op1 * op2).neg()Return anMPFwhose value is(- this).Set thisMPFto its opposite.Set thisMPFto(- op).powUi(long exp) Return anMPFwhose value is(thisexp).powUiAssign(long exp) Set thisMPFto(thisexp).powUiAssign(MPF base, long exp) Set thisMPFto(baseexp).static MPFrandom2(long maxSize, long exp) Return anMPFwhose value is a random integer of at mostmaxSizelimbs, with long strings of zeros and ones in the binary representation.random2Assign(long maxSize, long exp) Set thisMPFto a random integer of at mostmaxSizelimbs, with long strings of zeros and ones in the binary representation.private voidprivate voidReturn the relative difference betweenthisandthis, i.e.,(abs(this-op)/this).reldiffAssign(MPF op) Set thisMPFto the relative difference betweenthisandop, i.e.,(abs(this-op)/this).reldiffAssign(MPF op1, MPF op2) Set thisMPFto the relative difference betweenop1andop2, i.e.,(abs(op1-op2)/op1).set(double op) Set thisMPFtoop, possibly truncated according to precision.set(long op) Set thisMPFtoop, possibly truncated according to precision.Set thisMPFtoop, possibly truncated according to precision.Set thisMPFtoop, possibly truncated according to precision.Set thisMPFtoop, possibly truncated according to precision.intSet thisMPFto the number represented by the stringstrin the specifiedbase, possibly truncated according to precision.set(BigDecimal op) Set thisMPFto the big decimalop.static voidsetDefaultPrec(long prec) Set the default precision to be at leastprecbits.setPrec(long prec) Set the precision of thisMPFto be at leastprecbits.setUi(long op) Set thisMPFtoop, possibly truncated according to precision.setValue(double op) Set thisMPFtoop, possibly truncated according to precision.setValue(long op) Set thisMPFtoop, possibly truncated according to precision.Set thisMPFtoop, possibly truncated according to precision.Set thisMPFtoop, possibly truncated according to precision.Set thisMPFtoop, possibly truncated according to precision.Set thisMPFto the value represented by the stringstrin decimal base, possibly truncated according to precision.Set thisMPFto the number represented by the stringstrin the specifiedbase, possibly truncated according to precision.setValue(BigDecimal op) Set thisMPFto the big decimalop.intsgn()Return+1if(this > 0),0if(this = 0)and-1if(this < 0).sqrt()Return anMPFwhose value is the square root ofthis.Set thisMPFto its square root.sqrtAssign(MPF op) Set thisMPFto the the square root ofop.static MPFsqrtUi(long op) Return anMPFwhose value is the square root ofthis.sqrtUiAssign(long op) Set thisMPFto the the square root ofop.Return anMPFwhose value is(this - op).Set thisMPFto(this - op)Set thisMPFto(op1 - op2).subUi(long op) Return anMPFwhose value is(this - op).subUiAssign(long op) Set thisMPFto(this - op)subUiAssign(MPF op1, long op2) Set thisMPFto(op1 - op2).Swap the value of thisMPFwith the value ofop.toString()Convert thisMPFto its decimal string representation.toString(int base) Convert thisMPFto its string representation in the specifiedbase, ornullif the base is not valid.toString(int base, long nDigits) Convert thisMPFto its string representation in the specifiedbase, ornullif the base is not valid.trunc()Return anMPFwhose value isthisrounded towards zero.Set thisMPFto its value rounded towards zero.truncAssign(MPF op) Set thisMPFto the value ofoprounded towards zero.uiDiv(long op) Return anMPFwhose value is(op / this).uiDivAssign(long op) Set thisMPFto(op / this)uiDivAssign(long op1, MPF op2) Set thisMPFto(op1 / op2).uiSub(long op) Return anMPFwhose value is(op - this).uiSubAssign(long op) Set thisMPFto(op - this)uiSubAssign(long op1, MPF op2) Set thisMPFto(op1 - op2).static MPFReturn anMPFwhose value is an uniformly distributed random float in the range from0included to1excluded.urandombAssign(RandState s, long nbits) Set thisMPFto a uniformly distributed random float in the range from0included to1excluded.private voidMethods inherited from class Number
byteValue, shortValue
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDVersion for serializability.- See Also:
-
mpfNative
The pointer to the nativempf_tobject.
-
-
Constructor Details
-
MPF
A private constructor which build anMPFstarting from a pointer to its native data object. The native object needs to be already initialized. -
MPF
public MPF()Build anMPFwhose value is zero. -
MPF
Build anMPFwhose value isop, possibly truncated to the default precision. -
MPF
public MPF(long op) Build anMPFwhose value isop, possibly truncated to the default precision. -
MPF
public MPF(double op) Build anMPFwhose value isop, possibly truncated to the default precision.- Throws:
ArithmeticException- ifopis not a finite number.
-
MPF
Build anMPFwhose value isop, possibly truncated to the default precision. -
MPF
Build anMPFwhose value isop, possibly truncated to the default precision. -
MPF
Build anMPFwhose value is the number represented by the stringstrin the specifiedbase, possibly truncated to the default precision. See the GMP functionmpf_init_set_str.- Throws:
NumberFormatException- if eitherbaseis not valid orstris not a valid string in the specifiedbase.
-
MPF
Build anMPFwhose value is the number represented by the stringstrin decimal base, possibly truncated to the default precision. See the GMP functionmpf_init_set_str.- Throws:
NumberFormatException- ifstris not a valid number representation in decimal base.
-
MPF
Builds anMPFwhose value is the same asop. Note that, sinceBigDecimalrepresents number in thedecimal base whileMPFuse the binary base, rounding is possible.
-
-
Method Details
-
getNative
Return the native pointer to the GMP object. -
setDefaultPrec
public static void setDefaultPrec(long prec) Set the default precision to be at leastprecbits. Previously initialized variables are unaffected. -
getDefaultPrec
public static long getDefaultPrec()Return the default precision actually used. -
init
Return anMPFwhose value is zero. The precision of the result will be taken from the active default precision, as set bysetDefaultPrec(long). -
init2
Return anMPFwhose value is zero, and set its precision to be at leastprecbits. -
getPrec
public long getPrec()Return the current precision of thisMPF, in bits. -
setPrec
Set the precision of thisMPFto be at leastprecbits. The value will be truncated to the new precision. This function requires a reallocation, and should not be used in a tight loop.- Returns:
- this
MPF
-
set
-
set
Set thisMPFtoop, possibly truncated according to precision.- Returns:
- this
MPF.
-
setUi
Set thisMPFtoop, possibly truncated according to precision.- Returns:
- this
MPF.
-
set
Set thisMPFtoop, possibly truncated according to precision.- Returns:
- this
MPF. - Throws:
ArithmeticException- ifopis not a finite number. In this case,thisis not altered.
-
set
-
set
-
set
Set thisMPFto the number represented by the stringstrin the specifiedbase, possibly truncated according to precision. See the GMP functionmpf_set_str. The decimal point character is taken from the current system locale, which may be different from the Java locale.- Returns:
- 0 if the operation succeeded, -1 otherwise. In the latter case,
thisis not altered.
-
set
Set thisMPFto the big decimalop. Note that, sinceBigDecimalrepresents number in thedecimal base whileMPFuse the binary base, rounding is possible.- Returns:
- this
MPF.
-
swap
-
initSet
Return anMPFwhose value isop, possibly truncated to the default precision.- Throws:
ArithmeticException- ifopis not a finite number. In this case,thisis not altered.
-
initSetUi
Return anMPFwhose value isop, possibly truncated to the default precision. -
initSet
Return anMPFwhose value isop, possibly truncated to the default precision. -
initSet
Return anMPFwhose value isop, possibly truncated to the default precision.- Throws:
ArithmeticException- ifopis not a finite number.
-
initSet
Return anMPFwhose value is the number represented by the stringstrin the specifiedbase, possibly truncated to the default precision. See the GMP functionmpf_init_set_str. The decimal point character is taken from the current system locale, which may be different from the Java locale.- Returns:
- a pair whose first component is
0if the operation succeeded, and-1if eitherbaseis not valid, orstris not a valid numeric representation in the specified base. The second component of the pair is the number represented instrif the operation succeeded,0otherwise.
-
getD
public double getD()Convert thisMPFto a double, truncating if necessary. If the exponent from the conversion is too big or too small, the result is system dependent. For too big an infinity is returned when available. For too small 0.0 is normally returned. Hardware overflow, underflow and denorm traps may or may not occur. -
getD2Exp
Convert thisMPFto a pair made of mantissa and exponent, truncating if necessary. See the GMP functionmpf_get_d_2exp. -
getUi
public long getUi()Convert thisMPFto an unsigned long, truncating any fraction part. If this number is too big to fit an unsigned long, the result is undefined. -
getSi
public long getSi()Convert thisMPFto a long, truncating any fraction part. If this number is too big to fit a long, the result is undefined. -
getStr
Return the String representation of thisMPFin the specifiedbase, ornullif the base is not valid. See the GMP functionmpf_get_str. -
getBigDecimal
Convertthisto aBigDecimal. -
addAssign
-
addAssign
-
add
-
addUiAssign
-
addUiAssign
-
addUi
Return anMPFwhose value is(this + op). -
subAssign
-
subAssign
-
sub
-
subUiAssign
-
subUiAssign
-
subUi
Return anMPFwhose value is(this - op). -
uiSubAssign
-
uiSubAssign
-
uiSub
Return anMPFwhose value is(op - this). -
mulAssign
-
mulAssign
-
mul
-
mulUiAssign
-
mulUiAssign
-
mulUi
Return anMPFwhose value is(this * op). -
divAssign
Set thisMPFto(op1 / op2).- Returns:
- this
MPF. - Throws:
ArithmeticException- ifop2is zero.
-
divAssign
Set thisMPFto(this / op)- Returns:
- this
MPF - Throws:
ArithmeticException- ifopis zero.
-
div
Return anMPFwhose value is(this / op).- Throws:
ArithmeticException- ifopis zero.
-
divUiAssign
Set thisMPFto(op1 / op2).- Returns:
- this
MPF. - Throws:
ArithmeticException- ifop2is zero.
-
divUiAssign
Set thisMPFto(this / op)- Returns:
- this
MPF - Throws:
ArithmeticException- ifopis zero.
-
divUi
Return anMPFwhose value is(this / op).- Throws:
ArithmeticException- ifopis zero.
-
uiDivAssign
Set thisMPFto(op1 / op2).- Returns:
- this
MPF. - Throws:
ArithmeticException- ifop2is zero.
-
uiDivAssign
Set thisMPFto(op / this)- Returns:
- this
MPF - Throws:
ArithmeticException- ifthisis zero.
-
uiDiv
Return anMPFwhose value is(op / this).- Throws:
ArithmeticException- ifthisis zero.
-
sqrtAssign
-
sqrtAssign
-
sqrt
Return anMPFwhose value is the square root ofthis. -
sqrtUiAssign
-
sqrtUi
Return anMPFwhose value is the square root ofthis. -
powUiAssign
-
powUiAssign
Set thisMPFto(thisexp). The case00yields1.- Returns:
- this
MPF.
-
powUi
Return anMPFwhose value is(thisexp). The case00yields1. -
negAssign
-
negAssign
-
neg
Return anMPFwhose value is(- this). -
absAssign
-
absAssign
-
abs
Return anMPFwhose value is the absolute value ofthis. -
mul2ExpAssign
-
mul2ExpAssign
-
mul2Exp
Return anMPFwhose value is(this * 2b). -
div2ExpAssign
-
div2ExpAssign
-
div2Exp
Return anMPFwhose value is(this * 2b). -
cmp
Comparethiswithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op). -
cmp
Comparethiswithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op). The value ofopmay be infinite, but the result is undefined on NaNs. -
cmp
public int cmp(double op) Comparethiswithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op). The value ofopmay be infinite, but the result is undefined on NaNs.- Throws:
ArithmeticException- ifopis a NaN.
-
cmp
public int cmp(long op) Comparethiswithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op). -
cmpUi
public int cmpUi(long op) Comparethiswithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op). -
reldiffAssign
-
reldiffAssign
-
reldiff
-
sgn
public int sgn()Return+1if(this > 0),0if(this = 0)and-1if(this < 0). -
ceilAssign
-
ceilAssign
Set thisMPFto its value rounded to the next higher integer.- Returns:
- this
MPF.
-
ceil
Return anMPFwhose value isthisrounded to the next higher integer. -
floorAssign
-
floorAssign
Set thisMPFto its value rounded to the next lower integer.- Returns:
- this
MPF.
-
floor
Return anMPFwhose value isthisrounded to the next lower integer. -
truncAssign
-
truncAssign
-
trunc
Return anMPFwhose value isthisrounded towards zero. -
isInteger
public boolean isInteger()Return whether thisMPFis an integer. -
fitsUlong
public boolean fitsUlong()Returntrueif and only if thisMPFfits into a native unsigned long. -
fitsSlong
public boolean fitsSlong()Returntrueif and only if thisMPFfits into a native signed long. -
fitsUint
public boolean fitsUint()Returntrueif and only if thisMPFfits into a native unsigned int. -
fitsSint
public boolean fitsSint()Returntrueif and only if thisMPFfits into a native signed int. -
fitsUshort
public boolean fitsUshort()Returntrueif and only if thisMPFfits into a native unsigned short. -
fitsSshort
public boolean fitsSshort()Returntrueif and only if thisMPFfits into a native signed short. -
isZero
public boolean isZero()Return true if and only if thisthisMPF is zero. -
urandombAssign
-
urandomb
-
random2Assign
Set thisMPFto a random integer of at mostmaxSizelimbs, with long strings of zeros and ones in the binary representation. The exponent of the number is in the interval-exptoexp(in limbs). This function is useful for testing functions and algorithms, since these kind of random numbers have proven to be more likely to trigger corner-case bugs. Negative random numbers are generated whenmaxSizeis negative.- Returns:
- this
MPF.
-
random2
Return anMPFwhose value is a random integer of at mostmaxSizelimbs, with long strings of zeros and ones in the binary representation. The exponent of the number is in the interval-exptoexp(in limbs). This function is useful for testing functions and algorithms, since these kind of random numbers have proven to be more likely to trigger corner-case bugs. Negative random numbers are generated whenmaxSizeis negative. -
setValue
-
setValue
Set thisMPFtoop, possibly truncated according to precision.- Returns:
- this
MPF.
-
setValue
Set thisMPFtoop, possibly truncated according to precision.- Returns:
- this
MPF. - Throws:
ArithmeticException- ifopis not a finite number. In this case,thisis not altered.
-
setValue
-
setValue
-
setValue
Set thisMPFto the number represented by the stringstrin the specifiedbase, possibly truncated according to precision. See the GMP functionmpf_set_str. The decimal point character is taken from the current system locale, which may be different from the Java locale.- Throws:
ArithmeticException- if eitherbaseis not valid orstris not a valid number representation in the specified base. In this case,thisis not altered.
-
setValue
Set thisMPFto the value represented by the stringstrin decimal base, possibly truncated according to precision. The decimal point character is taken from the current system locale, which may be different from the Java locale.- Throws:
ArithmeticException- ifstris not a valid number representation in decimal base.- See Also:
-
setValue
Set thisMPFto the big decimalop. Note that, sinceBigDecimalrepresents number in thedecimal base whileMPFuse the binary base, rounding is possible.- Returns:
- this
MPF.
-
compareTo
Compare thisMPFwithop. Return a positive value if(this > op), zero if(this = op), or a negative value if(this < op). This order is compatible with equality.- Specified by:
compareToin interfaceComparable<MPF>
-
equals
-
hashCode
-
longValue
-
intValue
-
doubleValue
public double doubleValue()Convert thisMPFto a double, truncating if necessary. If the exponent from the conversion is too big or too small to fit a double then the result is system dependent. For too big an infinity is returned when available. For too small 0.0 is normally returned. Hardware overflow, underflow and denorm traps may or may not occur.- Specified by:
doubleValuein classNumber
-
floatValue
public float floatValue()Convert thisMPFto a float, truncating if necessary.- Specified by:
floatValuein classNumber
-
toString
Convert thisMPFto its string representation in the specifiedbase, ornullif the base is not valid. See the GMP functionmpf_get_str.- Throws:
IllegalArgumentException- if the base is not valid.
-
toString
Convert thisMPFto its string representation in the specifiedbase, ornullif the base is not valid. See the GMP functionmpf_get_str. -
toString
-
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOExceptionClassNotFoundException
-
readObjectNoData
- Throws:
ObjectStreamException
-