public final class CheckedScaleNfTruncatingArithmetic extends AbstractCheckedScaleNfArithmetic
Constructor and Description |
---|
CheckedScaleNfTruncatingArithmetic(ScaleMetrics scaleMetrics)
Constructor with scale metrics for this arithmetic.
|
Modifier and Type | Method and Description |
---|---|
long |
addUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal + unscaled * 10-scale).
|
long |
avg(long a,
long b)
Returns an unscaled decimal whose value is the average of
uDecimal1 and uDecimal2 . |
long |
divide(long uDecimalDividend,
long uDecimalDivisor)
Returns an unscaled decimal whose value is the quotient of the specified arguments:
(uDecimalDividend / uDecimalDivisor) . |
long |
divideByLong(long uDecimalDividend,
long lDivisor)
Returns an unscaled decimal whose value is
(uDecimalDividend / lDivisor) where the second argument is a
true long value instead of an unscaled decimal. |
long |
divideByPowerOf10(long uDecimal,
int positions)
Returns an unscaled decimal whose value is (uDecimal / 10n).
|
long |
divideByUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal / (unscaled * 10-scale)).
|
long |
fromBigDecimal(BigDecimal value)
Converts the specified
BigDecimal value to an unscaled decimal. |
long |
fromDouble(double value)
Converts the specified double value to an unscaled decimal.
|
long |
fromFloat(float value)
Converts the specified float value to an unscaled decimal.
|
long |
fromLong(long value)
Converts the specified long value to an unscaled decimal.
|
long |
fromUnscaled(long unscaledValue,
int scale)
Converts the specified unscaled decimal with the given scale to another unscaled decimal of the scale of this
arithmetic.
|
RoundingMode |
getRoundingMode()
Returns the rounding mode applied to operations of this
DecimalArithmetic object if rounding is
necessary. |
CheckedRounding |
getTruncationPolicy()
Returns the truncation policy defining how to handle truncation due to overflow or rounding.
|
long |
invert(long uDecimal)
Returns an unscaled decimal whose value is the inverse of the argument:
1/uDecimal . |
long |
multiply(long uDecimal1,
long uDecimal2)
Returns an unscaled decimal whose value is the product of the specified arguments:
(uDecimal1 * uDecimal2) . |
long |
multiplyByPowerOf10(long uDecimal,
int positions)
Returns an unscaled decimal whose value is (uDecimal * 10n).
|
long |
multiplyByUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal * unscaled * 10-scale).
|
long |
parse(CharSequence value,
int start,
int end)
Translates the string representation of a
Decimal into an unscaled Decimal. |
long |
parse(String value)
Translates the string representation of a
Decimal into an unscaled Decimal. |
long |
pow(long uDecimal,
int exponent)
Returns an unscaled decimal whose value is (uDecimalBaseexponent).
|
long |
round(long uDecimal,
int precision)
Returns an unscaled decimal whose value is rounded to the specified
precision using the
rounding mode of this arithmetic. |
long |
shiftLeft(long uDecimal,
int positions)
Returns an unscaled decimal whose value is
(uDecimal << n) . |
long |
shiftRight(long uDecimal,
int positions)
Returns an unscaled decimal whose value is
(uDecimal >> n) . |
long |
sqrt(long uDecimal)
Returns an unscaled decimal whose value is the square root of the specified argument: sqrt(uDecimal).
|
long |
square(long uDecimal)
Returns an unscaled decimal whose value is the square of the specified argument: uDecimal2.
|
long |
subtractUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal - unscaled * 10-scale).
|
double |
toDouble(long uDecimal)
Converts the specified unscaled decimal value into a double value and returns it.
|
float |
toFloat(long uDecimal)
Converts the specified unscaled decimal value into a float value and returns it.
|
long |
toLong(long uDecimal)
Converts the specified unscaled decimal value into a long value and returns it.
|
long |
toUnscaled(long uDecimal,
int scale)
Converts the specified unscaled decimal value into an unscaled value of the given scale.
|
addLong, getScale, getScaleMetrics, one, subtractLong, toString, toString
abs, add, getOverflowMode, multiplyByLong, negate, subtract
compare, compareToUnscaled, deriveArithmetic, deriveArithmetic, deriveArithmetic, deriveArithmetic, deriveArithmetic, fromBigInteger, signum, toBigDecimal, toBigDecimal
public CheckedScaleNfTruncatingArithmetic(ScaleMetrics scaleMetrics)
scaleMetrics
- the scale metricspublic final RoundingMode getRoundingMode()
DecimalArithmetic
DecimalArithmetic
object if rounding is
necessary.DecimalArithmetic
object if rounding is necessarypublic final CheckedRounding getTruncationPolicy()
DecimalArithmetic
TruncationPolicy
is defined by the overflow mode
and the
rounding mode
.getTruncationPolicy
in interface DecimalArithmetic
getTruncationPolicy
in class AbstractCheckedArithmetic
DecimalArithmetic
handles truncationpublic final long addUnscaled(long uDecimal, long unscaled, int scale)
DecimalArithmetic
rounding mode
is applied. Note that scale of the first
operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round(uDecimal + lValue * 10-scale + s) where s
refers to this arithetic's scale. The method avoids information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to be added tounscaled
- unscaled value to addscale
- scale associated with the unscaled
valuepublic final long subtractUnscaled(long uDecimal, long unscaled, int scale)
DecimalArithmetic
rounding mode
is applied. Note that scale of the first
operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round(uDecimal - lValue * 10-scale + s) where s
refers to this arithetic's scale. The method avoids information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to subtract fromunscaled
- unscaled value to subtractscale
- scale associated with the unscaled
valuepublic final long multiplyByUnscaled(long uDecimal, long unscaled, int scale)
DecimalArithmetic
rounding mode
is applied. Note that scale of the first
operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round((uDecimal * unscaled) * 10-scale) avoiding information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to be multipliedunscaled
- unscaled value to be multipliedscale
- scale associated with the unscaled
valuepublic final long divideByUnscaled(long uDecimal, long unscaled, int scale)
DecimalArithmetic
rounding mode
is applied. Note that scale of the
first operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round((uDecimal * 10scale) / unscaled) avoiding information loss due to overflow of intermediary results.
uDecimal
- value to be divided.unscaled
- unscaled value by which the dividend is to be divided.scale
- scale associated with the unscaled
valuepublic final long multiply(long uDecimal1, long uDecimal2)
DecimalArithmetic
(uDecimal1 * uDecimal2)
. If rounding must be performed, this arithmetic's rounding mode
is applied.
Mathematically the method calculates round((uDecimal1 * uDecimal2) * 10-scale) avoiding information loss due to overflow of intermediary results.
uDecimal1
- first unscaled decimal value to be multiplieduDecimal2
- second unscaled decimal value to be multipliedround(uDecimal1 * uDecimal2)
public final long square(long uDecimal)
DecimalArithmetic
rounding mode
is applied.
Mathematically the method calculates round((uDecimal * uDecimal) * 10-scale) avoiding information loss due to overflow of intermediary results.
uDecimal
- the unscaled decimal value to be squaredround(uDecimal * uDecimal)
public final long divide(long uDecimalDividend, long uDecimalDivisor)
DecimalArithmetic
(uDecimalDividend / uDecimalDivisor)
. If rounding must be performed, this arithmetic's
rounding mode
is applied.
Mathematically the method calculates round((uDecimalDividend * 10scale) / uDecimalDivisor) avoiding information loss due to overflow of intermediary results.
uDecimalDividend
- value to be divided.uDecimalDivisor
- value by which the dividend is to be divided.round(uDecimalDividend / uDecimalDivisor)
public final long pow(long uDecimal, int exponent)
DecimalArithmetic
exponent
is an integer rather than a decimal. If rounding must be performed, this arithmetic's rounding mode
is applied.
The current implementation uses the core algorithm defined in ANSI standard X3.274-1996. For exponent >= 0
, the
returned numerical value is within 1 ULP of the exact numerical value; the result is actually exact for all
rounding modes other than HALF_UP, HALF_EVEN and HALF_DOWN. No precision is guaranteed for exponent < 0
but the
result is usually exact up to 10-20 ULP.
Properties of the X3.274-1996 algorithm are:
IllegalArgumentException
is thrown if abs(n) > 999999999
exponent
is zero, one is returned even if uDecimalBase
is zero, otherwise
exponent
is positive, the result is calculated via the repeated squaring technique into a single
accumulatorexponent
is negative, the result is calculated as if exponent
were positive; this value is then divided
into onerounding mode
Note: this operation is not strictly garbage free since internally, two ThreadLocal
objects are
used to calculate the result. The ThreadLocal
values may become garbage if the thread becomes garbage.
uDecimal
- the unscaled decimal base valueexponent
- exponent to which uDecimalBase
is to be raised.public final long avg(long a, long b)
DecimalArithmetic
uDecimal1
and uDecimal2
. The method is
much more efficient than an addition and subsequent long division and is guaranteed not to overflow. If rounding
must be performed, this arithmetic's rounding mode
is applied.a
- the first unscaled decimal value to averageb
- the second unscaled decimal value to averageround((uDecimal1 + uDecimal2) / 2)
public final long sqrt(long uDecimal)
DecimalArithmetic
rounding mode
is applied.
Mathematically the method calculates round(sqrt(uDecimal * 10scale)) avoiding information loss due to overflow of intermediary results.
uDecimal
- the unscaled decimal valueround(sqrt(uDecimal))
public final long divideByLong(long uDecimalDividend, long lDivisor)
DecimalArithmetic
(uDecimalDividend / lDivisor)
where the second argument is a
true long value instead of an unscaled decimal. If rounding must be performed, this arithmetic's
rounding mode
is applied.uDecimalDividend
- value to be divided.lDivisor
- long value by which the dividend is to be divided.round(uDecimalDividend / lDivisor)
public final long divideByPowerOf10(long uDecimal, int positions)
DecimalArithmetic
rounding mode
is applied.
The power, n
, may be negative, in which case this method performs a multiplication by a power of ten.
uDecimal
- value to be divided.positions
- the power of tenpublic final long invert(long uDecimal)
DecimalArithmetic
1/uDecimal
. If rounding must be
performed, this arithmetic's rounding mode
is applied.
Mathematically the method calculates round((10scale * 10scale) / uDecimalDivisor) avoiding information loss due to overflow of intermediary results.
uDecimal
- the unscaled decimal value to invertround(1/uDecimal)
public final long multiplyByPowerOf10(long uDecimal, int positions)
DecimalArithmetic
The power, n
, may be negative, in which case this method performs a division by a power of ten. If
rounding must be performed (for negative n), this arithmetic's rounding mode
is
applied.
uDecimal
- value to be multipliedpositions
- the power of tenpublic final long shiftLeft(long uDecimal, int positions)
DecimalArithmetic
(uDecimal << n)
. The shift distance, n
, may be
negative, in which case this method performs a right shift. The result is equal to
round(uDecimal * 2n) using this arithmetic's rounding mode
if
rounding is necessary.uDecimal
- the unscaled decimal value to shiftpositions
- shift distance, in bits.round(uDecimal << n)
DecimalArithmetic.shiftRight(long, int)
public final long shiftRight(long uDecimal, int positions)
DecimalArithmetic
(uDecimal >> n)
. The shift distance, n
, may be
negative, in which case this method performs a left shift. The result is equal to
round(uDecimal / 2n) using this arithmetic's rounding mode
if
rounding is necessary.uDecimal
- the unscaled decimal value to shiftpositions
- shift distance, in bits.round(uDecimal >> n)
DecimalArithmetic.shiftLeft(long, int)
public final long round(long uDecimal, int precision)
DecimalArithmetic
precision
using the
rounding mode
of this arithmetic.
Note that this method does not change the scale of the value --- extra digits are simply zeroised.
Examples and special cases:
IllegalArgumentException
is thrownuDecimal
- the unscaled decimal value to roundprecision
- the precision to use for the rounding, for instance 2 to round to the second digit after the decimal
point; must be at least (scale - 18)
public final long fromLong(long value)
DecimalArithmetic
scale
.value
- the value to convertpublic final long fromFloat(float value)
DecimalArithmetic
scale
.value
- the value to convertpublic final long fromDouble(double value)
DecimalArithmetic
scale
.value
- the value to convertpublic final long fromUnscaled(long unscaledValue, int scale)
DecimalArithmetic
unscaledValue
- the unscaled decimal value to convertscale
- the scale associated with unscaledValue
public final long fromBigDecimal(BigDecimal value)
DecimalArithmetic
BigDecimal
value to an unscaled decimal. An exception is thrown if the specified
value is too large to be represented as a Decimal of this arithmetic's scale
.
Note: this operation is not garbage free, meaning that new temporary objects may be allocated during the conversion.
value
- the value to convertpublic final long toLong(long uDecimal)
DecimalArithmetic
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into a long valueuDecimal
value converted into a long value, possibly rounded or truncatedpublic final float toFloat(long uDecimal)
DecimalArithmetic
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into a float valueuDecimal
value converted into a float value, possibly rounded or truncatedpublic final double toDouble(long uDecimal)
DecimalArithmetic
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into a double valueuDecimal
value converted into a double value, possibly rounded or truncatedpublic final long toUnscaled(long uDecimal, int scale)
DecimalArithmetic
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into an unscaled value of the specified scalescale
- the target scale for the result valueuDecimal
value converted into an unscaled value of the specified scale, possibly rounded or
truncatedpublic final long parse(String value)
DecimalArithmetic
Decimal
into an unscaled Decimal. The string representation
consists of an optional sign, '+'
or '-'
, followed by a sequence of zero or more decimal digits
("the integer"), optionally followed by a fraction.
The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at
least one digit in either the integer or the fraction. If the fraction contains more digits than this
arithmetic's scale
, the value is rounded using the arithmetic's rounding mode
. An exception is thrown if the value is too large to be represented as a Decimal of this
arithmetic's scale.
value
- a String
containing the decimal value representation to be parsedlong
valuepublic final long parse(CharSequence value, int start, int end)
DecimalArithmetic
Decimal
into an unscaled Decimal. The string representation
consists of an optional sign, '+'
or '-'
, followed by a sequence of zero or more decimal digits
("the integer"), optionally followed by a fraction.
The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at
least one digit in either the integer or the fraction. If the fraction contains more digits than this
arithmetic's scale
, the value is rounded using the arithmetic's rounding mode
. An exception is thrown if the value is too large to be represented as a Decimal of this
arithmetic's scale.
value
- a character sequence such as a String
containing the decimal value representation to be parsedstart
- the start index to read characters in value
, inclusiveend
- the end index where to stop reading in characters in value
, exclusivelong
value