public class Dfp extends Object implements RealFieldElement<Dfp>
Another floating point class. This one is built using radix 10000 which is 104, so its almost decimal.
The design goals here are:
Trade offs:
Numbers are represented in the following form:
n = sign × mant × (radix)exp;where sign is ±1, mantissa represents a fractional number between zero and one. mant[0] is the least significant digit. exp is in the range of -32767 to 32768
IEEE 854-1987 Notes and differences
IEEE 854 requires the radix to be either 2 or 10. The radix here is 10000, so that requirement is not met, but it is possible that a subclassed can be made to make it behave as a radix 10 number. It is my opinion that if it looks and behaves as a radix 10 number then it is one and that requirement would be met.
The radix of 10000 was chosen because it should be faster to operate on 4 decimal digits at once instead of one at a time. Radix 10 behavior can be realized by adding an additional rounding step to ensure that the number of decimal digits represented is constant.
The IEEE standard specifically leaves out internal data encoding, so it is reasonable to conclude that such a subclass of this radix 10000 system is merely an encoding of a radix 10 system.
IEEE 854 also specifies the existence of "sub-normal" numbers. This class does not contain any such entities. The most significant radix 10000 digit is always non-zero. Instead, we support "gradual underflow" by raising the underflow flag for numbers less with exponent less than expMin, but don't flush to zero until the exponent reaches MIN_EXP-digits. Thus the smallest number we can represent would be: 1E(-(MIN_EXP-digits-1)*4), eg, for digits=5, MIN_EXP=-32767, that would be 1e-131092.
IEEE 854 defines that the implied radix point lies just to the right of the most significant digit and to the left of the remaining digits. This implementation puts the implied radix point to the left of all digits including the most significant one. The most significant digit here is the one just to the right of the radix point. This is a fine detail and is really only a matter of definition. Any side effects of this can be rendered invisible by a subclass.
DfpField
Modifier and Type | Field and Description |
---|---|
static int |
ERR_SCALE
The amount under/overflows are scaled by before going to trap handler
|
protected int |
exp
Exponent.
|
static byte |
FINITE
Indicator value for normal finite numbers.
|
static byte |
INFINITE
Indicator value for Infinity.
|
protected int[] |
mant
Mantissa.
|
static int |
MAX_EXP
The maximum exponent before overflow is signaled and results flushed
to infinity
|
static int |
MIN_EXP
The minimum exponent before underflow is signaled.
|
protected byte |
nans
Indicator for non-finite / non-number values.
|
static byte |
QNAN
Indicator value for quiet NaN.
|
static int |
RADIX
The radix, or base of this system.
|
protected byte |
sign
Sign bit: 1 for positive, -1 for negative.
|
static byte |
SNAN
Indicator value for signaling NaN.
|
DEG_TO_RAD, RAD_TO_DEG
Modifier | Constructor and Description |
---|---|
|
Dfp(Dfp d)
Copy constructor.
|
protected |
Dfp(DfpField field)
Makes an instance with a value of zero.
|
protected |
Dfp(DfpField field,
byte x)
Create an instance from a byte value.
|
protected |
Dfp(DfpField field,
byte sign,
byte nans)
Creates an instance with a non-finite value.
|
protected |
Dfp(DfpField field,
double x)
Create an instance from a double value.
|
protected |
Dfp(DfpField field,
int x)
Create an instance from an int value.
|
protected |
Dfp(DfpField field,
long x)
Create an instance from a long value.
|
protected |
Dfp(DfpField field,
String s)
Create an instance from a String representation.
|
Modifier and Type | Method and Description |
---|---|
Dfp |
abs()
Get the absolute value of instance.
|
Dfp |
acos()
Arc cosine operation.
|
Dfp |
acosh()
Inverse hyperbolic cosine operation.
|
Dfp |
add(Dfp x)
Add x to this.
|
Dfp |
add(double a)
'+' operator.
|
protected int |
align(int e)
Make our exp equal to the supplied one, this may cause rounding.
|
Dfp |
asin()
Arc sine operation.
|
Dfp |
asinh()
Inverse hyperbolic sine operation.
|
Dfp |
atan()
Arc tangent operation.
|
Dfp |
atan2(Dfp x)
Two arguments arc tangent operation.
|
Dfp |
atanh()
Inverse hyperbolic tangent operation.
|
Dfp |
cbrt()
Cubic root.
|
Dfp |
ceil()
Round to an integer using the round ceil mode.
|
int |
classify()
Returns the type - one of FINITE, INFINITE, SNAN, QNAN.
|
protected int |
complement(int extra)
Negate the mantissa of this by computing the complement.
|
Dfp |
copySign(Dfp s)
Returns the instance with the sign of the argument.
|
static Dfp |
copysign(Dfp x,
Dfp y)
Creates an instance that is the same as x except that it has the sign of y.
|
Dfp |
copySign(double s)
Returns the instance with the sign of the argument.
|
Dfp |
cos()
Cosine operation.
|
Dfp |
cosh()
Hyperbolic cosine operation.
|
protected String |
dfp2sci()
Convert an instance to a string using scientific notation.
|
protected String |
dfp2string()
Convert an instance to a string using normal notation.
|
Dfp |
divide(Dfp divisor)
Divide this by divisor.
|
Dfp |
divide(double a)
'÷' operator.
|
Dfp |
divide(int divisor)
Divide by a single digit less than radix.
|
Dfp |
dotrap(int type,
String what,
Dfp oper,
Dfp result)
Raises a trap.
|
boolean |
equals(Object other)
Check if instance is equal to x.
|
Dfp |
exp()
Exponential.
|
Dfp |
expm1()
Exponential minus 1.
|
Dfp |
floor()
Round to an integer using the round floor mode.
|
int |
getExponent()
Return the exponent of the instance, removing the bias.
|
DfpField |
getField()
|
Dfp |
getOne()
Get the constant 1.
|
int |
getRadixDigits()
Get the number of radix digits of the instance.
|
double |
getReal()
Get the real value of the number.
|
Dfp |
getTwo()
Get the constant 2.
|
Dfp |
getZero()
Get the constant 0.
|
boolean |
greaterThan(Dfp x)
Check if instance is greater than x.
|
int |
hashCode()
Gets a hashCode for the instance.
|
Dfp |
hypot(Dfp y)
Returns the hypotenuse of a triangle with sides
this and y
- sqrt(this2 +y2)
avoiding intermediate overflow or underflow. |
int |
intLog10()
Get the exponent of the greatest power of 10 that is less than or equal to abs(this).
|
int |
intValue()
Convert this to an integer.
|
boolean |
isInfinite()
Check if the instance is infinite.
|
boolean |
isNaN()
Check if the instance is Not a Number.
|
boolean |
isZero()
Check if instance is equal to zero.
|
boolean |
lessThan(Dfp x)
Check if instance is less than x.
|
Dfp |
linearCombination(Dfp[] a,
Dfp[] b)
Compute a linear combination.
|
Dfp |
linearCombination(Dfp a1,
Dfp b1,
Dfp a2,
Dfp b2)
Compute a linear combination.
|
Dfp |
linearCombination(Dfp a1,
Dfp b1,
Dfp a2,
Dfp b2,
Dfp a3,
Dfp b3)
Compute a linear combination.
|
Dfp |
linearCombination(Dfp a1,
Dfp b1,
Dfp a2,
Dfp b2,
Dfp a3,
Dfp b3,
Dfp a4,
Dfp b4)
Compute a linear combination.
|
Dfp |
linearCombination(double[] a,
Dfp[] b)
Compute a linear combination.
|
Dfp |
linearCombination(double a1,
Dfp b1,
double a2,
Dfp b2)
Compute a linear combination.
|
Dfp |
linearCombination(double a1,
Dfp b1,
double a2,
Dfp b2,
double a3,
Dfp b3)
Compute a linear combination.
|
Dfp |
linearCombination(double a1,
Dfp b1,
double a2,
Dfp b2,
double a3,
Dfp b3,
double a4,
Dfp b4)
Compute a linear combination.
|
Dfp |
log()
Natural logarithm.
|
Dfp |
log10()
Base 10 logarithm.
|
int |
log10K()
Get the exponent of the greatest power of 10000 that is
less than or equal to the absolute value of this.
|
Dfp |
log1p()
Shifted natural logarithm.
|
Dfp |
multiply(Dfp x)
Multiply this by x.
|
Dfp |
multiply(double a)
'×' operator.
|
Dfp |
multiply(int x)
Multiply this by a single digit x.
|
Dfp |
negate()
Returns a number that is this number with the sign bit reversed.
|
boolean |
negativeOrNull()
Check if instance is less than or equal to 0.
|
Dfp |
newInstance()
Create an instance with a value of 0.
|
Dfp |
newInstance(byte x)
Create an instance from a byte value.
|
Dfp |
newInstance(byte sig,
byte code)
Creates an instance with a non-finite value.
|
Dfp |
newInstance(Dfp d)
Create an instance by copying an existing one.
|
Dfp |
newInstance(DfpField targetField,
DfpField.RoundingMode rmode)
Creates an instance by converting the instance to a different field (i.e.
|
Dfp |
newInstance(double x)
Create an instance corresponding to a constant real value.
|
Dfp |
newInstance(int x)
Create an instance from an int value.
|
Dfp |
newInstance(long x)
Create an instance from a long value.
|
Dfp |
newInstance(String s)
Create an instance from a String representation.
|
Dfp |
nextAfter(Dfp x)
Returns the next number greater than this one in the direction of x.
|
boolean |
positiveOrNull()
Check if instance is greater than or equal to 0.
|
Dfp |
pow(Dfp e)
Power operation.
|
Dfp |
pow(double p)
Power operation.
|
Dfp |
pow(int n)
Integer power operation.
|
Dfp |
power10(int e)
Return the specified power of 10.
|
Dfp |
power10K(int e)
Get the specified power of 10000.
|
Dfp |
reciprocal()
Returns the multiplicative inverse of
this element. |
Dfp |
remainder(Dfp d)
Returns the IEEE remainder.
|
Dfp |
remainder(double a)
IEEE remainder operator.
|
Dfp |
rint()
Round to nearest integer using the round-half-even method.
|
Dfp |
rootN(int n)
Nth root.
|
protected int |
round(int n)
Round this given the next digit n using the current rounding mode.
|
Dfp |
scalb(int n)
Multiply the instance by a power of 2.
|
protected void |
shiftLeft()
Shift the mantissa left, and adjust the exponent to compensate.
|
protected void |
shiftRight()
Shift the mantissa right, and adjust the exponent to compensate.
|
Dfp |
signum()
Compute the signum of the instance.
|
Dfp |
sin()
Sine operation.
|
Dfp |
sinh()
Hyperbolic sine operation.
|
Dfp |
sqrt()
Compute the square root.
|
boolean |
strictlyNegative()
Check if instance is strictly less than 0.
|
boolean |
strictlyPositive()
Check if instance is strictly greater than 0.
|
Dfp |
subtract(Dfp x)
Subtract x from this.
|
Dfp |
subtract(double a)
'-' operator.
|
Dfp |
tan()
Tangent operation.
|
Dfp |
tanh()
Hyperbolic tangent operation.
|
Dfp |
toDegrees()
Convert radians to degrees, with error of less than 0.5 ULP
|
double |
toDouble()
Convert the instance into a double.
|
Dfp |
toRadians()
Convert degrees to radians, with error of less than 0.5 ULP
|
double[] |
toSplitDouble()
Convert the instance into a split double.
|
String |
toString()
Get a string representation of the instance.
|
protected Dfp |
trap(int type,
String what,
Dfp oper,
Dfp def,
Dfp result)
Trap handler.
|
protected Dfp |
trunc(DfpField.RoundingMode rmode)
Does the integer conversions with the specified rounding.
|
boolean |
unequal(Dfp x)
Check if instance is not equal to x.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
round
sinCos
public static final int RADIX
public static final int MIN_EXP
public static final int MAX_EXP
public static final int ERR_SCALE
public static final byte FINITE
public static final byte INFINITE
public static final byte SNAN
public static final byte QNAN
protected int[] mant
protected byte sign
protected int exp
protected byte nans
protected Dfp(DfpField field)
field
- field to which this instance belongsprotected Dfp(DfpField field, byte x)
field
- field to which this instance belongsx
- value to convert to an instanceprotected Dfp(DfpField field, int x)
field
- field to which this instance belongsx
- value to convert to an instanceprotected Dfp(DfpField field, long x)
field
- field to which this instance belongsx
- value to convert to an instanceprotected Dfp(DfpField field, double x)
field
- field to which this instance belongsx
- value to convert to an instancepublic Dfp(Dfp d)
d
- instance to copyprotected Dfp(DfpField field, String s)
field
- field to which this instance belongss
- string representation of the instancepublic Dfp newInstance()
public Dfp newInstance(byte x)
x
- value to convert to an instancepublic Dfp newInstance(int x)
x
- value to convert to an instancepublic Dfp newInstance(long x)
x
- value to convert to an instancepublic Dfp newInstance(double x)
The default implementation creates the instance by adding
the value to getField().getZero()
. This is not optimal
and does not work when called with a negative zero as the
sign of zero is lost with the addition. The default implementation
should therefore be overridden in concrete classes. The default
implementation will be removed at the next major version.
newInstance
in interface CalculusFieldElement<Dfp>
x
- constant real valuepublic Dfp newInstance(Dfp d)
d
- instance to copypublic Dfp newInstance(String s)
s
- string representation of the instancepublic Dfp newInstance(byte sig, byte code)
public Dfp newInstance(DfpField targetField, DfpField.RoundingMode rmode)
If the target field as a greater number of digits, the extra least significant digits will be set to zero.
targetField
- field to convert the instance tormode
- rounding mode to use if target field as less digits than the instance, can be null otherwiseDfpField.getExtendedField(int, boolean)
public DfpField getField()
Field
(really a DfpField
) to which the instance belongs.
The field is linked to the number of digits and acts as a factory
for Dfp
instances.
getField
in interface FieldElement<Dfp>
Field
(really a DfpField
) to which the instance belongspublic int getRadixDigits()
public Dfp getZero()
public Dfp getOne()
public Dfp getTwo()
protected void shiftLeft()
protected void shiftRight()
protected int align(int e)
e
- desired exponentpublic boolean lessThan(Dfp x)
x
- number to check instance againstpublic boolean greaterThan(Dfp x)
x
- number to check instance againstpublic boolean negativeOrNull()
public boolean strictlyNegative()
public boolean positiveOrNull()
public boolean strictlyPositive()
public Dfp abs()
abs
in interface RealFieldElement<Dfp>
public boolean isInfinite()
isInfinite
in interface CalculusFieldElement<Dfp>
public boolean isNaN()
isNaN
in interface CalculusFieldElement<Dfp>
public boolean isZero()
isZero
in interface FieldElement<Dfp>
public boolean equals(Object other)
public int hashCode()
public boolean unequal(Dfp x)
x
- number to check instance againstpublic Dfp rint()
rint
in interface CalculusFieldElement<Dfp>
public Dfp floor()
floor
in interface CalculusFieldElement<Dfp>
public Dfp ceil()
ceil
in interface CalculusFieldElement<Dfp>
public Dfp remainder(Dfp d)
remainder
in interface CalculusFieldElement<Dfp>
d
- divisorprotected Dfp trunc(DfpField.RoundingMode rmode)
rmode
- rounding mode to usepublic int intValue()
public int log10K()
public Dfp power10K(int e)
e
- desired powerpublic int intLog10()
public Dfp power10(int e)
e
- desired powerprotected int complement(int extra)
extra
- ???public Dfp add(Dfp x)
add
in interface FieldElement<Dfp>
x
- number to addpublic Dfp negate()
negate
in interface FieldElement<Dfp>
public Dfp subtract(Dfp x)
subtract
in interface FieldElement<Dfp>
x
- number to subtractprotected int round(int n)
n
- ???public Dfp multiply(Dfp x)
multiply
in interface FieldElement<Dfp>
x
- multiplicandpublic Dfp multiply(int x)
multiply
in interface FieldElement<Dfp>
x
- multiplicandpublic Dfp divide(Dfp divisor)
divide
in interface FieldElement<Dfp>
divisor
- divisorpublic Dfp divide(int divisor)
divisor
- divisorpublic Dfp reciprocal()
this
element.reciprocal
in interface CalculusFieldElement<Dfp>
reciprocal
in interface FieldElement<Dfp>
this
.public Dfp sqrt()
sqrt
in interface CalculusFieldElement<Dfp>
public String toString()
protected String dfp2sci()
protected String dfp2string()
public Dfp dotrap(int type, String what, Dfp oper, Dfp result)
type
- the trap typewhat
- - name of routine trap occurred inoper
- - input operator to functionresult
- - the result computed prior to the trapprotected Dfp trap(int type, String what, Dfp oper, Dfp def, Dfp result)
type
- The exception type - e.g. FLAG_OVERFLOWwhat
- The name of the routine we were in e.g. divide()oper
- An operand to this function if anydef
- The default return value if trap not enabledresult
- The result that is specified to be delivered per
IEEE 854, if anypublic int classify()
public static Dfp copysign(Dfp x, Dfp y)
x
- number to get the value fromy
- number to get the sign frompublic Dfp nextAfter(Dfp x)
x
- direction where to look atpublic double toDouble()
toSplitDouble()
public double[] toSplitDouble()
toDouble()
public double getReal()
getReal
in interface CalculusFieldElement<Dfp>
public Dfp add(double a)
add
in interface CalculusFieldElement<Dfp>
a
- right hand side parameter of the operatorpublic Dfp subtract(double a)
subtract
in interface CalculusFieldElement<Dfp>
a
- right hand side parameter of the operatorpublic Dfp multiply(double a)
multiply
in interface CalculusFieldElement<Dfp>
a
- right hand side parameter of the operatorpublic Dfp divide(double a)
divide
in interface CalculusFieldElement<Dfp>
a
- right hand side parameter of the operatorpublic Dfp remainder(double a)
remainder
in interface CalculusFieldElement<Dfp>
a
- right hand side parameter of the operatorpublic Dfp signum()
signum
in interface CalculusFieldElement<Dfp>
public Dfp copySign(Dfp s)
sign
argument is treated as positive.copySign
in interface CalculusFieldElement<Dfp>
s
- the sign for the returned valuesign
argumentpublic Dfp copySign(double s)
sign
argument is treated as positive.copySign
in interface CalculusFieldElement<Dfp>
s
- the sign for the returned valuesign
argumentpublic int getExponent()
For double numbers of the form 2x, the unbiased exponent is exactly x.
getExponent
in interface CalculusFieldElement<Dfp>
public Dfp scalb(int n)
scalb
in interface CalculusFieldElement<Dfp>
n
- power of 2public Dfp hypot(Dfp y)
this
and y
- sqrt(this2 +y2)
avoiding intermediate overflow or underflow.
hypot
in interface CalculusFieldElement<Dfp>
y
- a valuepublic Dfp cbrt()
cbrt
in interface CalculusFieldElement<Dfp>
public Dfp rootN(int n)
rootN
in interface CalculusFieldElement<Dfp>
n
- order of the rootpublic Dfp pow(double p)
pow
in interface CalculusFieldElement<Dfp>
p
- power to applypublic Dfp pow(int n)
pow
in interface CalculusFieldElement<Dfp>
n
- power to applypublic Dfp pow(Dfp e)
pow
in interface CalculusFieldElement<Dfp>
e
- exponentpublic Dfp exp()
exp
in interface CalculusFieldElement<Dfp>
public Dfp expm1()
expm1
in interface CalculusFieldElement<Dfp>
public Dfp log()
log
in interface CalculusFieldElement<Dfp>
public Dfp log1p()
log1p
in interface CalculusFieldElement<Dfp>
public Dfp log10()
log10
in interface CalculusFieldElement<Dfp>
public Dfp cos()
cos
in interface CalculusFieldElement<Dfp>
public Dfp sin()
sin
in interface CalculusFieldElement<Dfp>
public Dfp tan()
tan
in interface CalculusFieldElement<Dfp>
public Dfp acos()
acos
in interface CalculusFieldElement<Dfp>
public Dfp asin()
asin
in interface CalculusFieldElement<Dfp>
public Dfp atan()
atan
in interface CalculusFieldElement<Dfp>
public Dfp atan2(Dfp x) throws MathIllegalArgumentException
atan2
in interface CalculusFieldElement<Dfp>
x
- second argument of the arc tangentMathIllegalArgumentException
- if number of free parameters or orders are inconsistentpublic Dfp cosh()
cosh
in interface CalculusFieldElement<Dfp>
public Dfp sinh()
sinh
in interface CalculusFieldElement<Dfp>
public Dfp tanh()
tanh
in interface CalculusFieldElement<Dfp>
public Dfp acosh()
acosh
in interface CalculusFieldElement<Dfp>
public Dfp asinh()
asinh
in interface CalculusFieldElement<Dfp>
public Dfp atanh()
atanh
in interface CalculusFieldElement<Dfp>
public Dfp toDegrees()
toDegrees
in interface CalculusFieldElement<Dfp>
public Dfp toRadians()
toRadians
in interface CalculusFieldElement<Dfp>
public Dfp linearCombination(Dfp[] a, Dfp[] b) throws MathIllegalArgumentException
linearCombination
in interface CalculusFieldElement<Dfp>
a
- Factors.b
- Factors.Σi ai bi
.MathIllegalArgumentException
- if arrays dimensions don't matchpublic Dfp linearCombination(double[] a, Dfp[] b) throws MathIllegalArgumentException
linearCombination
in interface CalculusFieldElement<Dfp>
a
- Factors.b
- Factors.Σi ai bi
.MathIllegalArgumentException
- if arrays dimensions don't matchpublic Dfp linearCombination(Dfp a1, Dfp b1, Dfp a2, Dfp b2)
linearCombination
in interface CalculusFieldElement<Dfp>
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second termCalculusFieldElement.linearCombination(Object, Object, Object, Object, Object, Object)
,
CalculusFieldElement.linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)
public Dfp linearCombination(double a1, Dfp b1, double a2, Dfp b2)
linearCombination
in interface CalculusFieldElement<Dfp>
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second termCalculusFieldElement.linearCombination(double, Object, double, Object, double, Object)
,
CalculusFieldElement.linearCombination(double, Object, double, Object, double, Object, double, Object)
public Dfp linearCombination(Dfp a1, Dfp b1, Dfp a2, Dfp b2, Dfp a3, Dfp b3)
linearCombination
in interface CalculusFieldElement<Dfp>
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second terma3
- first factor of the third termb3
- second factor of the third termCalculusFieldElement.linearCombination(Object, Object, Object, Object)
,
CalculusFieldElement.linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)
public Dfp linearCombination(double a1, Dfp b1, double a2, Dfp b2, double a3, Dfp b3)
linearCombination
in interface CalculusFieldElement<Dfp>
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second terma3
- first factor of the third termb3
- second factor of the third termCalculusFieldElement.linearCombination(double, Object, double, Object)
,
CalculusFieldElement.linearCombination(double, Object, double, Object, double, Object, double, Object)
public Dfp linearCombination(Dfp a1, Dfp b1, Dfp a2, Dfp b2, Dfp a3, Dfp b3, Dfp a4, Dfp b4)
linearCombination
in interface CalculusFieldElement<Dfp>
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second terma3
- first factor of the third termb3
- second factor of the third terma4
- first factor of the fourth termb4
- second factor of the fourth termCalculusFieldElement.linearCombination(Object, Object, Object, Object)
,
CalculusFieldElement.linearCombination(Object, Object, Object, Object, Object, Object)
public Dfp linearCombination(double a1, Dfp b1, double a2, Dfp b2, double a3, Dfp b3, double a4, Dfp b4)
linearCombination
in interface CalculusFieldElement<Dfp>
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second terma3
- first factor of the third termb3
- second factor of the third terma4
- first factor of the fourth termb4
- second factor of the fourth termCalculusFieldElement.linearCombination(double, Object, double, Object)
,
CalculusFieldElement.linearCombination(double, Object, double, Object, double, Object)
Copyright © 2016–2020 Hipparchus.org. All rights reserved.