code
stringlengths 11
173k
| docstring
stringlengths 2
593k
| func_name
stringlengths 2
189
| language
stringclasses 1
value | repo
stringclasses 833
values | path
stringlengths 11
294
| url
stringlengths 60
339
| license
stringclasses 4
values |
---|---|---|---|---|---|---|---|
public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
BigDecimal divrem[] = this.divideAndRemainder(divisor, mc);
return divrem[1];
} |
Returns a {@code BigDecimal} whose value is {@code (this %
divisor)}, with rounding according to the context settings.
The {@code MathContext} settings affect the implicit divide
used to compute the remainder. The remainder computation
itself is by definition exact. Therefore, the remainder may
contain more than {@code mc.getPrecision()} digits.
<p>The remainder is given by
{@code this.subtract(this.divideToIntegralValue(divisor,
mc).multiply(divisor))}. Note that this is not the modulo
operation (the result can be negative).
@param divisor value by which this {@code BigDecimal} is to be divided.
@param mc the context to use.
@return {@code this % divisor}, rounded as necessary.
@throws ArithmeticException if {@code divisor==0}
@throws ArithmeticException if the result is inexact but the
rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
{@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
require a precision of more than {@code mc.precision} digits.
@see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
@since 1.5
| BigDecimal::remainder | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal[] divideAndRemainder(BigDecimal divisor) {
// we use the identity x = i * y + r to determine r
BigDecimal[] result = new BigDecimal[2];
result[0] = this.divideToIntegralValue(divisor);
result[1] = this.subtract(result[0].multiply(divisor));
return result;
} |
Returns a two-element {@code BigDecimal} array containing the
result of {@code divideToIntegralValue} followed by the result of
{@code remainder} on the two operands.
<p>Note that if both the integer quotient and remainder are
needed, this method is faster than using the
{@code divideToIntegralValue} and {@code remainder} methods
separately because the division need only be carried out once.
@param divisor value by which this {@code BigDecimal} is to be divided,
and the remainder computed.
@return a two element {@code BigDecimal} array: the quotient
(the result of {@code divideToIntegralValue}) is the initial element
and the remainder is the final element.
@throws ArithmeticException if {@code divisor==0}
@see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
@see #remainder(java.math.BigDecimal, java.math.MathContext)
@since 1.5
| BigDecimal::divideAndRemainder | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) {
if (mc.precision == 0)
return divideAndRemainder(divisor);
BigDecimal[] result = new BigDecimal[2];
BigDecimal lhs = this;
result[0] = lhs.divideToIntegralValue(divisor, mc);
result[1] = lhs.subtract(result[0].multiply(divisor));
return result;
} |
Returns a two-element {@code BigDecimal} array containing the
result of {@code divideToIntegralValue} followed by the result of
{@code remainder} on the two operands calculated with rounding
according to the context settings.
<p>Note that if both the integer quotient and remainder are
needed, this method is faster than using the
{@code divideToIntegralValue} and {@code remainder} methods
separately because the division need only be carried out once.
@param divisor value by which this {@code BigDecimal} is to be divided,
and the remainder computed.
@param mc the context to use.
@return a two element {@code BigDecimal} array: the quotient
(the result of {@code divideToIntegralValue}) is the
initial element and the remainder is the final element.
@throws ArithmeticException if {@code divisor==0}
@throws ArithmeticException if the result is inexact but the
rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
{@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
require a precision of more than {@code mc.precision} digits.
@see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
@see #remainder(java.math.BigDecimal, java.math.MathContext)
@since 1.5
| BigDecimal::divideAndRemainder | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal pow(int n) {
if (n < 0 || n > 999999999)
throw new ArithmeticException("Invalid operation");
// No need to calculate pow(n) if result will over/underflow.
// Don't attempt to support "supernormal" numbers.
int newScale = checkScale((long)scale * n);
return new BigDecimal(this.inflated().pow(n), newScale);
} |
Returns a {@code BigDecimal} whose value is
<tt>(this<sup>n</sup>)</tt>, The power is computed exactly, to
unlimited precision.
<p>The parameter {@code n} must be in the range 0 through
999999999, inclusive. {@code ZERO.pow(0)} returns {@link
#ONE}.
Note that future releases may expand the allowable exponent
range of this method.
@param n power to raise this {@code BigDecimal} to.
@return <tt>this<sup>n</sup></tt>
@throws ArithmeticException if {@code n} is out of range.
@since 1.5
| BigDecimal::pow | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal pow(int n, MathContext mc) {
if (mc.precision == 0)
return pow(n);
if (n < -999999999 || n > 999999999)
throw new ArithmeticException("Invalid operation");
if (n == 0)
return ONE; // x**0 == 1 in X3.274
BigDecimal lhs = this;
MathContext workmc = mc; // working settings
int mag = Math.abs(n); // magnitude of n
if (mc.precision > 0) {
int elength = longDigitLength(mag); // length of n in digits
if (elength > mc.precision) // X3.274 rule
throw new ArithmeticException("Invalid operation");
workmc = new MathContext(mc.precision + elength + 1,
mc.roundingMode);
}
// ready to carry out power calculation...
BigDecimal acc = ONE; // accumulator
boolean seenbit = false; // set once we've seen a 1-bit
for (int i=1;;i++) { // for each bit [top bit ignored]
mag += mag; // shift left 1 bit
if (mag < 0) { // top bit is set
seenbit = true; // OK, we're off
acc = acc.multiply(lhs, workmc); // acc=acc*x
}
if (i == 31)
break; // that was the last bit
if (seenbit)
acc=acc.multiply(acc, workmc); // acc=acc*acc [square]
// else (!seenbit) no point in squaring ONE
}
// if negative n, calculate the reciprocal using working precision
if (n < 0) // [hence mc.precision>0]
acc=ONE.divide(acc, workmc);
// round to final precision and strip zeros
return doRound(acc, mc);
} |
Returns a {@code BigDecimal} whose value is
<tt>(this<sup>n</sup>)</tt>. The current implementation uses
the core algorithm defined in ANSI standard X3.274-1996 with
rounding according to the context settings. In general, the
returned numerical value is within two ulps of the exact
numerical value for the chosen precision. Note that future
releases may use a different algorithm with a decreased
allowable error bound and increased allowable exponent range.
<p>The X3.274-1996 algorithm is:
<ul>
<li> An {@code ArithmeticException} exception is thrown if
<ul>
<li>{@code abs(n) > 999999999}
<li>{@code mc.precision == 0} and {@code n < 0}
<li>{@code mc.precision > 0} and {@code n} has more than
{@code mc.precision} decimal digits
</ul>
<li> if {@code n} is zero, {@link #ONE} is returned even if
{@code this} is zero, otherwise
<ul>
<li> if {@code n} is positive, the result is calculated via
the repeated squaring technique into a single accumulator.
The individual multiplications with the accumulator use the
same math context settings as in {@code mc} except for a
precision increased to {@code mc.precision + elength + 1}
where {@code elength} is the number of decimal digits in
{@code n}.
<li> if {@code n} is negative, the result is calculated as if
{@code n} were positive; this value is then divided into one
using the working precision specified above.
<li> The final value from either the positive or negative case
is then rounded to the destination precision.
</ul>
</ul>
@param n power to raise this {@code BigDecimal} to.
@param mc the context to use.
@return <tt>this<sup>n</sup></tt> using the ANSI standard X3.274-1996
algorithm
@throws ArithmeticException if the result is inexact but the
rounding mode is {@code UNNECESSARY}, or {@code n} is out
of range.
@since 1.5
| BigDecimal::pow | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal abs() {
return (signum() < 0 ? negate() : this);
} |
Returns a {@code BigDecimal} whose value is the absolute value
of this {@code BigDecimal}, and whose scale is
{@code this.scale()}.
@return {@code abs(this)}
| BigDecimal::abs | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal abs(MathContext mc) {
return (signum() < 0 ? negate(mc) : plus(mc));
} |
Returns a {@code BigDecimal} whose value is the absolute value
of this {@code BigDecimal}, with rounding according to the
context settings.
@param mc the context to use.
@return {@code abs(this)}, rounded as necessary.
@throws ArithmeticException if the result is inexact but the
rounding mode is {@code UNNECESSARY}.
@since 1.5
| BigDecimal::abs | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal negate() {
if (intCompact == INFLATED) {
return new BigDecimal(intVal.negate(), INFLATED, scale, precision);
} else {
return valueOf(-intCompact, scale, precision);
}
} |
Returns a {@code BigDecimal} whose value is {@code (-this)},
and whose scale is {@code this.scale()}.
@return {@code -this}.
| BigDecimal::negate | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal negate(MathContext mc) {
return negate().plus(mc);
} |
Returns a {@code BigDecimal} whose value is {@code (-this)},
with rounding according to the context settings.
@param mc the context to use.
@return {@code -this}, rounded as necessary.
@throws ArithmeticException if the result is inexact but the
rounding mode is {@code UNNECESSARY}.
@since 1.5
| BigDecimal::negate | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal plus() {
return this;
} |
Returns a {@code BigDecimal} whose value is {@code (+this)}, and whose
scale is {@code this.scale()}.
<p>This method, which simply returns this {@code BigDecimal}
is included for symmetry with the unary minus method {@link
#negate()}.
@return {@code this}.
@see #negate()
@since 1.5
| BigDecimal::plus | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal plus(MathContext mc) {
if (mc.precision == 0) // no rounding please
return this;
return doRound(this, mc);
} |
Returns a {@code BigDecimal} whose value is {@code (+this)},
with rounding according to the context settings.
<p>The effect of this method is identical to that of the {@link
#round(MathContext)} method.
@param mc the context to use.
@return {@code this}, rounded as necessary. A zero result will
have a scale of 0.
@throws ArithmeticException if the result is inexact but the
rounding mode is {@code UNNECESSARY}.
@see #round(MathContext)
@since 1.5
| BigDecimal::plus | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public int signum() {
return (intCompact != INFLATED)?
Long.signum(intCompact):
intVal.signum();
} |
Returns the signum function of this {@code BigDecimal}.
@return -1, 0, or 1 as the value of this {@code BigDecimal}
is negative, zero, or positive.
| BigDecimal::signum | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public int precision() {
int result = precision;
if (result == 0) {
long s = intCompact;
if (s != INFLATED)
result = longDigitLength(s);
else
result = bigDigitLength(intVal);
precision = result;
}
return result;
} |
Returns the <i>precision</i> of this {@code BigDecimal}. (The
precision is the number of digits in the unscaled value.)
<p>The precision of a zero value is 1.
@return the precision of this {@code BigDecimal}.
@since 1.5
| BigDecimal::precision | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigInteger unscaledValue() {
return this.inflated();
} |
Returns a {@code BigInteger} whose value is the <i>unscaled
value</i> of this {@code BigDecimal}. (Computes <tt>(this *
10<sup>this.scale()</sup>)</tt>.)
@return the unscaled value of this {@code BigDecimal}.
@since 1.2
| BigDecimal::unscaledValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal round(MathContext mc) {
return plus(mc);
} |
Returns a {@code BigDecimal} rounded according to the
{@code MathContext} settings. If the precision setting is 0 then
no rounding takes place.
<p>The effect of this method is identical to that of the
{@link #plus(MathContext)} method.
@param mc the context to use.
@return a {@code BigDecimal} rounded according to the
{@code MathContext} settings.
@throws ArithmeticException if the rounding mode is
{@code UNNECESSARY} and the
{@code BigDecimal} operation would require rounding.
@see #plus(MathContext)
@since 1.5
| BigDecimal::round | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal setScale(int newScale, RoundingMode roundingMode) {
return setScale(newScale, roundingMode.oldMode);
} |
Returns a {@code BigDecimal} whose scale is the specified
value, and whose unscaled value is determined by multiplying or
dividing this {@code BigDecimal}'s unscaled value by the
appropriate power of ten to maintain its overall value. If the
scale is reduced by the operation, the unscaled value must be
divided (rather than multiplied), and the value may be changed;
in this case, the specified rounding mode is applied to the
division.
<p>Note that since BigDecimal objects are immutable, calls of
this method do <i>not</i> result in the original object being
modified, contrary to the usual convention of having methods
named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>.
Instead, {@code setScale} returns an object with the proper
scale; the returned object may or may not be newly allocated.
@param newScale scale of the {@code BigDecimal} value to be returned.
@param roundingMode The rounding mode to apply.
@return a {@code BigDecimal} whose scale is the specified value,
and whose unscaled value is determined by multiplying or
dividing this {@code BigDecimal}'s unscaled value by the
appropriate power of ten to maintain its overall value.
@throws ArithmeticException if {@code roundingMode==UNNECESSARY}
and the specified scaling operation would require
rounding.
@see RoundingMode
@since 1.5
| BigDecimal::setScale | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal setScale(int newScale, int roundingMode) {
if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
throw new IllegalArgumentException("Invalid rounding mode");
int oldScale = this.scale;
if (newScale == oldScale) // easy case
return this;
if (this.signum() == 0) // zero can have any scale
return zeroValueOf(newScale);
if(this.intCompact!=INFLATED) {
long rs = this.intCompact;
if (newScale > oldScale) {
int raise = checkScale((long) newScale - oldScale);
if ((rs = longMultiplyPowerTen(rs, raise)) != INFLATED) {
return valueOf(rs,newScale);
}
BigInteger rb = bigMultiplyPowerTen(raise);
return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
} else {
// newScale < oldScale -- drop some digits
// Can't predict the precision due to the effect of rounding.
int drop = checkScale((long) oldScale - newScale);
if (drop < LONG_TEN_POWERS_TABLE.length) {
return divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode, newScale);
} else {
return divideAndRound(this.inflated(), bigTenToThe(drop), newScale, roundingMode, newScale);
}
}
} else {
if (newScale > oldScale) {
int raise = checkScale((long) newScale - oldScale);
BigInteger rb = bigMultiplyPowerTen(this.intVal,raise);
return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
} else {
// newScale < oldScale -- drop some digits
// Can't predict the precision due to the effect of rounding.
int drop = checkScale((long) oldScale - newScale);
if (drop < LONG_TEN_POWERS_TABLE.length)
return divideAndRound(this.intVal, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode,
newScale);
else
return divideAndRound(this.intVal, bigTenToThe(drop), newScale, roundingMode, newScale);
}
}
} |
Returns a {@code BigDecimal} whose scale is the specified
value, and whose unscaled value is determined by multiplying or
dividing this {@code BigDecimal}'s unscaled value by the
appropriate power of ten to maintain its overall value. If the
scale is reduced by the operation, the unscaled value must be
divided (rather than multiplied), and the value may be changed;
in this case, the specified rounding mode is applied to the
division.
<p>Note that since BigDecimal objects are immutable, calls of
this method do <i>not</i> result in the original object being
modified, contrary to the usual convention of having methods
named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>.
Instead, {@code setScale} returns an object with the proper
scale; the returned object may or may not be newly allocated.
<p>The new {@link #setScale(int, RoundingMode)} method should
be used in preference to this legacy method.
@param newScale scale of the {@code BigDecimal} value to be returned.
@param roundingMode The rounding mode to apply.
@return a {@code BigDecimal} whose scale is the specified value,
and whose unscaled value is determined by multiplying or
dividing this {@code BigDecimal}'s unscaled value by the
appropriate power of ten to maintain its overall value.
@throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY}
and the specified scaling operation would require
rounding.
@throws IllegalArgumentException if {@code roundingMode} does not
represent a valid rounding mode.
@see #ROUND_UP
@see #ROUND_DOWN
@see #ROUND_CEILING
@see #ROUND_FLOOR
@see #ROUND_HALF_UP
@see #ROUND_HALF_DOWN
@see #ROUND_HALF_EVEN
@see #ROUND_UNNECESSARY
| BigDecimal::setScale | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal setScale(int newScale) {
return setScale(newScale, ROUND_UNNECESSARY);
} |
Returns a {@code BigDecimal} whose scale is the specified
value, and whose value is numerically equal to this
{@code BigDecimal}'s. Throws an {@code ArithmeticException}
if this is not possible.
<p>This call is typically used to increase the scale, in which
case it is guaranteed that there exists a {@code BigDecimal}
of the specified scale and the correct value. The call can
also be used to reduce the scale if the caller knows that the
{@code BigDecimal} has sufficiently many zeros at the end of
its fractional part (i.e., factors of ten in its integer value)
to allow for the rescaling without changing its value.
<p>This method returns the same result as the two-argument
versions of {@code setScale}, but saves the caller the trouble
of specifying a rounding mode in cases where it is irrelevant.
<p>Note that since {@code BigDecimal} objects are immutable,
calls of this method do <i>not</i> result in the original
object being modified, contrary to the usual convention of
having methods named <tt>set<i>X</i></tt> mutate field
<i>{@code X}</i>. Instead, {@code setScale} returns an
object with the proper scale; the returned object may or may
not be newly allocated.
@param newScale scale of the {@code BigDecimal} value to be returned.
@return a {@code BigDecimal} whose scale is the specified value, and
whose unscaled value is determined by multiplying or dividing
this {@code BigDecimal}'s unscaled value by the appropriate
power of ten to maintain its overall value.
@throws ArithmeticException if the specified scaling operation would
require rounding.
@see #setScale(int, int)
@see #setScale(int, RoundingMode)
| BigDecimal::setScale | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal movePointLeft(int n) {
// Cannot use movePointRight(-n) in case of n==Integer.MIN_VALUE
int newScale = checkScale((long)scale + n);
BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
} |
Returns a {@code BigDecimal} which is equivalent to this one
with the decimal point moved {@code n} places to the left. If
{@code n} is non-negative, the call merely adds {@code n} to
the scale. If {@code n} is negative, the call is equivalent
to {@code movePointRight(-n)}. The {@code BigDecimal}
returned by this call has value <tt>(this ×
10<sup>-n</sup>)</tt> and scale {@code max(this.scale()+n,
0)}.
@param n number of places to move the decimal point to the left.
@return a {@code BigDecimal} which is equivalent to this one with the
decimal point moved {@code n} places to the left.
@throws ArithmeticException if scale overflows.
| BigDecimal::movePointLeft | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal movePointRight(int n) {
// Cannot use movePointLeft(-n) in case of n==Integer.MIN_VALUE
int newScale = checkScale((long)scale - n);
BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
} |
Returns a {@code BigDecimal} which is equivalent to this one
with the decimal point moved {@code n} places to the right.
If {@code n} is non-negative, the call merely subtracts
{@code n} from the scale. If {@code n} is negative, the call
is equivalent to {@code movePointLeft(-n)}. The
{@code BigDecimal} returned by this call has value <tt>(this
× 10<sup>n</sup>)</tt> and scale {@code max(this.scale()-n,
0)}.
@param n number of places to move the decimal point to the right.
@return a {@code BigDecimal} which is equivalent to this one
with the decimal point moved {@code n} places to the right.
@throws ArithmeticException if scale overflows.
| BigDecimal::movePointRight | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal scaleByPowerOfTen(int n) {
return new BigDecimal(intVal, intCompact,
checkScale((long)scale - n), precision);
} |
Returns a BigDecimal whose numerical value is equal to
({@code this} * 10<sup>n</sup>). The scale of
the result is {@code (this.scale() - n)}.
@param n the exponent power of ten to scale by
@return a BigDecimal whose numerical value is equal to
({@code this} * 10<sup>n</sup>)
@throws ArithmeticException if the scale would be
outside the range of a 32-bit integer.
@since 1.5
| BigDecimal::scaleByPowerOfTen | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal stripTrailingZeros() {
if (intCompact == 0 || (intVal != null && intVal.signum() == 0)) {
return BigDecimal.ZERO;
} else if (intCompact != INFLATED) {
return createAndStripZerosToMatchScale(intCompact, scale, Long.MIN_VALUE);
} else {
return createAndStripZerosToMatchScale(intVal, scale, Long.MIN_VALUE);
}
} |
Returns a {@code BigDecimal} which is numerically equal to
this one but with any trailing zeros removed from the
representation. For example, stripping the trailing zeros from
the {@code BigDecimal} value {@code 600.0}, which has
[{@code BigInteger}, {@code scale}] components equals to
[6000, 1], yields {@code 6E2} with [{@code BigInteger},
{@code scale}] components equals to [6, -2]. If
this BigDecimal is numerically equal to zero, then
{@code BigDecimal.ZERO} is returned.
@return a numerically equal {@code BigDecimal} with any
trailing zeros removed.
@since 1.5
| BigDecimal::stripTrailingZeros | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public int compareTo(BigDecimal val) {
// Quick path for equal scale and non-inflated case.
if (scale == val.scale) {
long xs = intCompact;
long ys = val.intCompact;
if (xs != INFLATED && ys != INFLATED)
return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
}
int xsign = this.signum();
int ysign = val.signum();
if (xsign != ysign)
return (xsign > ysign) ? 1 : -1;
if (xsign == 0)
return 0;
int cmp = compareMagnitude(val);
return (xsign > 0) ? cmp : -cmp;
} |
Compares this {@code BigDecimal} with the specified
{@code BigDecimal}. Two {@code BigDecimal} objects that are
equal in value but have a different scale (like 2.0 and 2.00)
are considered equal by this method. This method is provided
in preference to individual methods for each of the six boolean
comparison operators ({@literal <}, ==,
{@literal >}, {@literal >=}, !=, {@literal <=}). The
suggested idiom for performing these comparisons is:
{@code (x.compareTo(y)} <<i>op</i>> {@code 0)}, where
<<i>op</i>> is one of the six comparison operators.
@param val {@code BigDecimal} to which this {@code BigDecimal} is
to be compared.
@return -1, 0, or 1 as this {@code BigDecimal} is numerically
less than, equal to, or greater than {@code val}.
| BigDecimal::compareTo | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private int compareMagnitude(BigDecimal val) {
// Match scales, avoid unnecessary inflation
long ys = val.intCompact;
long xs = this.intCompact;
if (xs == 0)
return (ys == 0) ? 0 : -1;
if (ys == 0)
return 1;
long sdiff = (long)this.scale - val.scale;
if (sdiff != 0) {
// Avoid matching scales if the (adjusted) exponents differ
long xae = (long)this.precision() - this.scale; // [-1]
long yae = (long)val.precision() - val.scale; // [-1]
if (xae < yae)
return -1;
if (xae > yae)
return 1;
BigInteger rb = null;
if (sdiff < 0) {
// The cases sdiff <= Integer.MIN_VALUE intentionally fall through.
if ( sdiff > Integer.MIN_VALUE &&
(xs == INFLATED ||
(xs = longMultiplyPowerTen(xs, (int)-sdiff)) == INFLATED) &&
ys == INFLATED) {
rb = bigMultiplyPowerTen((int)-sdiff);
return rb.compareMagnitude(val.intVal);
}
} else { // sdiff > 0
// The cases sdiff > Integer.MAX_VALUE intentionally fall through.
if ( sdiff <= Integer.MAX_VALUE &&
(ys == INFLATED ||
(ys = longMultiplyPowerTen(ys, (int)sdiff)) == INFLATED) &&
xs == INFLATED) {
rb = val.bigMultiplyPowerTen((int)sdiff);
return this.intVal.compareMagnitude(rb);
}
}
}
if (xs != INFLATED)
return (ys != INFLATED) ? longCompareMagnitude(xs, ys) : -1;
else if (ys != INFLATED)
return 1;
else
return this.intVal.compareMagnitude(val.intVal);
} |
Version of compareTo that ignores sign.
| BigDecimal::compareMagnitude | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal min(BigDecimal val) {
return (compareTo(val) <= 0 ? this : val);
} |
Returns the minimum of this {@code BigDecimal} and
{@code val}.
@param val value with which the minimum is to be computed.
@return the {@code BigDecimal} whose value is the lesser of this
{@code BigDecimal} and {@code val}. If they are equal,
as defined by the {@link #compareTo(BigDecimal) compareTo}
method, {@code this} is returned.
@see #compareTo(java.math.BigDecimal)
| BigDecimal::min | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal max(BigDecimal val) {
return (compareTo(val) >= 0 ? this : val);
} |
Returns the maximum of this {@code BigDecimal} and {@code val}.
@param val value with which the maximum is to be computed.
@return the {@code BigDecimal} whose value is the greater of this
{@code BigDecimal} and {@code val}. If they are equal,
as defined by the {@link #compareTo(BigDecimal) compareTo}
method, {@code this} is returned.
@see #compareTo(java.math.BigDecimal)
| BigDecimal::max | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public String toEngineeringString() {
return layoutChars(false);
} |
Returns a string representation of this {@code BigDecimal},
using engineering notation if an exponent is needed.
<p>Returns a string that represents the {@code BigDecimal} as
described in the {@link #toString()} method, except that if
exponential notation is used, the power of ten is adjusted to
be a multiple of three (engineering notation) such that the
integer part of nonzero values will be in the range 1 through
999. If exponential notation is used for zero values, a
decimal point and one or two fractional zero digits are used so
that the scale of the zero value is preserved. Note that
unlike the output of {@link #toString()}, the output of this
method is <em>not</em> guaranteed to recover the same [integer,
scale] pair of this {@code BigDecimal} if the output string is
converting back to a {@code BigDecimal} using the {@linkplain
#BigDecimal(String) string constructor}. The result of this method meets
the weaker constraint of always producing a numerically equal
result from applying the string constructor to the method's output.
@return string representation of this {@code BigDecimal}, using
engineering notation if an exponent is needed.
@since 1.5
| BigDecimal::toEngineeringString | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public String toPlainString() {
if(scale==0) {
if(intCompact!=INFLATED) {
return Long.toString(intCompact);
} else {
return intVal.toString();
}
}
if(this.scale<0) { // No decimal point
if(signum()==0) {
return "0";
}
int tailingZeros = checkScaleNonZero((-(long)scale));
StringBuilder buf;
if(intCompact!=INFLATED) {
buf = new StringBuilder(20+tailingZeros);
buf.append(intCompact);
} else {
String str = intVal.toString();
buf = new StringBuilder(str.length()+tailingZeros);
buf.append(str);
}
for (int i = 0; i < tailingZeros; i++)
buf.append('0');
return buf.toString();
}
String str ;
if(intCompact!=INFLATED) {
str = Long.toString(Math.abs(intCompact));
} else {
str = intVal.abs().toString();
}
return getValueString(signum(), str, scale);
} |
Returns a string representation of this {@code BigDecimal}
without an exponent field. For values with a positive scale,
the number of digits to the right of the decimal point is used
to indicate scale. For values with a zero or negative scale,
the resulting string is generated as if the value were
converted to a numerically equal value with zero scale and as
if all the trailing zeros of the zero scale value were present
in the result.
The entire string is prefixed by a minus sign character '-'
(<tt>'\u002D'</tt>) if the unscaled value is less than
zero. No sign character is prefixed if the unscaled value is
zero or positive.
Note that if the result of this method is passed to the
{@linkplain #BigDecimal(String) string constructor}, only the
numerical value of this {@code BigDecimal} will necessarily be
recovered; the representation of the new {@code BigDecimal}
may have a different scale. In particular, if this
{@code BigDecimal} has a negative scale, the string resulting
from this method will have a scale of zero when processed by
the string constructor.
(This method behaves analogously to the {@code toString}
method in 1.4 and earlier releases.)
@return a string representation of this {@code BigDecimal}
without an exponent field.
@since 1.5
@see #toString()
@see #toEngineeringString()
| BigDecimal::toPlainString | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private String getValueString(int signum, String intString, int scale) {
/* Insert decimal point */
StringBuilder buf;
int insertionPoint = intString.length() - scale;
if (insertionPoint == 0) { /* Point goes right before intVal */
return (signum<0 ? "-0." : "0.") + intString;
} else if (insertionPoint > 0) { /* Point goes inside intVal */
buf = new StringBuilder(intString);
buf.insert(insertionPoint, '.');
if (signum < 0)
buf.insert(0, '-');
} else { /* We must insert zeros between point and intVal */
buf = new StringBuilder(3-insertionPoint + intString.length());
buf.append(signum<0 ? "-0." : "0.");
for (int i=0; i<-insertionPoint; i++)
buf.append('0');
buf.append(intString);
}
return buf.toString();
} |
Returns a string representation of this {@code BigDecimal}
without an exponent field. For values with a positive scale,
the number of digits to the right of the decimal point is used
to indicate scale. For values with a zero or negative scale,
the resulting string is generated as if the value were
converted to a numerically equal value with zero scale and as
if all the trailing zeros of the zero scale value were present
in the result.
The entire string is prefixed by a minus sign character '-'
(<tt>'\u002D'</tt>) if the unscaled value is less than
zero. No sign character is prefixed if the unscaled value is
zero or positive.
Note that if the result of this method is passed to the
{@linkplain #BigDecimal(String) string constructor}, only the
numerical value of this {@code BigDecimal} will necessarily be
recovered; the representation of the new {@code BigDecimal}
may have a different scale. In particular, if this
{@code BigDecimal} has a negative scale, the string resulting
from this method will have a scale of zero when processed by
the string constructor.
(This method behaves analogously to the {@code toString}
method in 1.4 and earlier releases.)
@return a string representation of this {@code BigDecimal}
without an exponent field.
@since 1.5
@see #toString()
@see #toEngineeringString()
public String toPlainString() {
if(scale==0) {
if(intCompact!=INFLATED) {
return Long.toString(intCompact);
} else {
return intVal.toString();
}
}
if(this.scale<0) { // No decimal point
if(signum()==0) {
return "0";
}
int tailingZeros = checkScaleNonZero((-(long)scale));
StringBuilder buf;
if(intCompact!=INFLATED) {
buf = new StringBuilder(20+tailingZeros);
buf.append(intCompact);
} else {
String str = intVal.toString();
buf = new StringBuilder(str.length()+tailingZeros);
buf.append(str);
}
for (int i = 0; i < tailingZeros; i++)
buf.append('0');
return buf.toString();
}
String str ;
if(intCompact!=INFLATED) {
str = Long.toString(Math.abs(intCompact));
} else {
str = intVal.abs().toString();
}
return getValueString(signum(), str, scale);
}
/* Returns a digit.digit string | BigDecimal::getValueString | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigInteger toBigInteger() {
// force to an integer, quietly
return this.setScale(0, ROUND_DOWN).inflated();
} |
Converts this {@code BigDecimal} to a {@code BigInteger}.
This conversion is analogous to the
<i>narrowing primitive conversion</i> from {@code double} to
{@code long} as defined in section 5.1.3 of
<cite>The Java™ Language Specification</cite>:
any fractional part of this
{@code BigDecimal} will be discarded. Note that this
conversion can lose information about the precision of the
{@code BigDecimal} value.
<p>
To have an exception thrown if the conversion is inexact (in
other words if a nonzero fractional part is discarded), use the
{@link #toBigIntegerExact()} method.
@return this {@code BigDecimal} converted to a {@code BigInteger}.
| BigDecimal::toBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigInteger toBigIntegerExact() {
// round to an integer, with Exception if decimal part non-0
return this.setScale(0, ROUND_UNNECESSARY).inflated();
} |
Converts this {@code BigDecimal} to a {@code BigInteger},
checking for lost information. An exception is thrown if this
{@code BigDecimal} has a nonzero fractional part.
@return this {@code BigDecimal} converted to a {@code BigInteger}.
@throws ArithmeticException if {@code this} has a nonzero
fractional part.
@since 1.5
| BigDecimal::toBigIntegerExact | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public long longValue(){
return (intCompact != INFLATED && scale == 0) ?
intCompact:
toBigInteger().longValue();
} |
Converts this {@code BigDecimal} to a {@code long}.
This conversion is analogous to the
<i>narrowing primitive conversion</i> from {@code double} to
{@code short} as defined in section 5.1.3 of
<cite>The Java™ Language Specification</cite>:
any fractional part of this
{@code BigDecimal} will be discarded, and if the resulting
"{@code BigInteger}" is too big to fit in a
{@code long}, only the low-order 64 bits are returned.
Note that this conversion can lose information about the
overall magnitude and precision of this {@code BigDecimal} value as well
as return a result with the opposite sign.
@return this {@code BigDecimal} converted to a {@code long}.
| BigDecimal::longValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public long longValueExact() {
if (intCompact != INFLATED && scale == 0)
return intCompact;
// If more than 19 digits in integer part it cannot possibly fit
if ((precision() - scale) > 19) // [OK for negative scale too]
throw new java.lang.ArithmeticException("Overflow");
// Fastpath zero and < 1.0 numbers (the latter can be very slow
// to round if very small)
if (this.signum() == 0)
return 0;
if ((this.precision() - this.scale) <= 0)
throw new ArithmeticException("Rounding necessary");
// round to an integer, with Exception if decimal part non-0
BigDecimal num = this.setScale(0, ROUND_UNNECESSARY);
if (num.precision() >= 19) // need to check carefully
LongOverflow.check(num);
return num.inflated().longValue();
} |
Converts this {@code BigDecimal} to a {@code long}, checking
for lost information. If this {@code BigDecimal} has a
nonzero fractional part or is out of the possible range for a
{@code long} result then an {@code ArithmeticException} is
thrown.
@return this {@code BigDecimal} converted to a {@code long}.
@throws ArithmeticException if {@code this} has a nonzero
fractional part, or will not fit in a {@code long}.
@since 1.5
| BigDecimal::longValueExact | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public int intValue() {
return (intCompact != INFLATED && scale == 0) ?
(int)intCompact :
toBigInteger().intValue();
} |
Converts this {@code BigDecimal} to an {@code int}.
This conversion is analogous to the
<i>narrowing primitive conversion</i> from {@code double} to
{@code short} as defined in section 5.1.3 of
<cite>The Java™ Language Specification</cite>:
any fractional part of this
{@code BigDecimal} will be discarded, and if the resulting
"{@code BigInteger}" is too big to fit in an
{@code int}, only the low-order 32 bits are returned.
Note that this conversion can lose information about the
overall magnitude and precision of this {@code BigDecimal}
value as well as return a result with the opposite sign.
@return this {@code BigDecimal} converted to an {@code int}.
| LongOverflow::intValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public int intValueExact() {
long num;
num = this.longValueExact(); // will check decimal part
if ((int)num != num)
throw new java.lang.ArithmeticException("Overflow");
return (int)num;
} |
Converts this {@code BigDecimal} to an {@code int}, checking
for lost information. If this {@code BigDecimal} has a
nonzero fractional part or is out of the possible range for an
{@code int} result then an {@code ArithmeticException} is
thrown.
@return this {@code BigDecimal} converted to an {@code int}.
@throws ArithmeticException if {@code this} has a nonzero
fractional part, or will not fit in an {@code int}.
@since 1.5
| LongOverflow::intValueExact | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public short shortValueExact() {
long num;
num = this.longValueExact(); // will check decimal part
if ((short)num != num)
throw new java.lang.ArithmeticException("Overflow");
return (short)num;
} |
Converts this {@code BigDecimal} to a {@code short}, checking
for lost information. If this {@code BigDecimal} has a
nonzero fractional part or is out of the possible range for a
{@code short} result then an {@code ArithmeticException} is
thrown.
@return this {@code BigDecimal} converted to a {@code short}.
@throws ArithmeticException if {@code this} has a nonzero
fractional part, or will not fit in a {@code short}.
@since 1.5
| LongOverflow::shortValueExact | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public byte byteValueExact() {
long num;
num = this.longValueExact(); // will check decimal part
if ((byte)num != num)
throw new java.lang.ArithmeticException("Overflow");
return (byte)num;
} |
Converts this {@code BigDecimal} to a {@code byte}, checking
for lost information. If this {@code BigDecimal} has a
nonzero fractional part or is out of the possible range for a
{@code byte} result then an {@code ArithmeticException} is
thrown.
@return this {@code BigDecimal} converted to a {@code byte}.
@throws ArithmeticException if {@code this} has a nonzero
fractional part, or will not fit in a {@code byte}.
@since 1.5
| LongOverflow::byteValueExact | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public float floatValue(){
if(intCompact != INFLATED) {
if (scale == 0) {
return (float)intCompact;
} else {
/*
* If both intCompact and the scale can be exactly
* represented as float values, perform a single float
* multiply or divide to compute the (properly
* rounded) result.
*/
if (Math.abs(intCompact) < 1L<<22 ) {
// Don't have too guard against
// Math.abs(MIN_VALUE) because of outer check
// against INFLATED.
if (scale > 0 && scale < float10pow.length) {
return (float)intCompact / float10pow[scale];
} else if (scale < 0 && scale > -float10pow.length) {
return (float)intCompact * float10pow[-scale];
}
}
}
}
// Somewhat inefficient, but guaranteed to work.
return Float.parseFloat(this.toString());
} |
Converts this {@code BigDecimal} to a {@code float}.
This conversion is similar to the
<i>narrowing primitive conversion</i> from {@code double} to
{@code float} as defined in section 5.1.3 of
<cite>The Java™ Language Specification</cite>:
if this {@code BigDecimal} has too great a
magnitude to represent as a {@code float}, it will be
converted to {@link Float#NEGATIVE_INFINITY} or {@link
Float#POSITIVE_INFINITY} as appropriate. Note that even when
the return value is finite, this conversion can lose
information about the precision of the {@code BigDecimal}
value.
@return this {@code BigDecimal} converted to a {@code float}.
| LongOverflow::floatValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public double doubleValue(){
if(intCompact != INFLATED) {
if (scale == 0) {
return (double)intCompact;
} else {
/*
* If both intCompact and the scale can be exactly
* represented as double values, perform a single
* double multiply or divide to compute the (properly
* rounded) result.
*/
if (Math.abs(intCompact) < 1L<<52 ) {
// Don't have too guard against
// Math.abs(MIN_VALUE) because of outer check
// against INFLATED.
if (scale > 0 && scale < double10pow.length) {
return (double)intCompact / double10pow[scale];
} else if (scale < 0 && scale > -double10pow.length) {
return (double)intCompact * double10pow[-scale];
}
}
}
}
// Somewhat inefficient, but guaranteed to work.
return Double.parseDouble(this.toString());
} |
Converts this {@code BigDecimal} to a {@code double}.
This conversion is similar to the
<i>narrowing primitive conversion</i> from {@code double} to
{@code float} as defined in section 5.1.3 of
<cite>The Java™ Language Specification</cite>:
if this {@code BigDecimal} has too great a
magnitude represent as a {@code double}, it will be
converted to {@link Double#NEGATIVE_INFINITY} or {@link
Double#POSITIVE_INFINITY} as appropriate. Note that even when
the return value is finite, this conversion can lose
information about the precision of the {@code BigDecimal}
value.
@return this {@code BigDecimal} converted to a {@code double}.
| LongOverflow::doubleValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
public BigDecimal ulp() {
return BigDecimal.valueOf(1, this.scale(), 1);
} |
Returns the size of an ulp, a unit in the last place, of this
{@code BigDecimal}. An ulp of a nonzero {@code BigDecimal}
value is the positive distance between this value and the
{@code BigDecimal} value next larger in magnitude with the
same number of digits. An ulp of a zero value is numerically
equal to 1 with the scale of {@code this}. The result is
stored with the same scale as {@code this} so the result
for zero and nonzero values is equal to {@code [1,
this.scale()]}.
@return the size of an ulp of {@code this}
@since 1.5
| LongOverflow::ulp | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private String layoutChars(boolean sci) {
if (scale == 0) // zero scale is trivial
return (intCompact != INFLATED) ?
Long.toString(intCompact):
intVal.toString();
if (scale == 2 &&
intCompact >= 0 && intCompact < Integer.MAX_VALUE) {
// currency fast path
int lowInt = (int)intCompact % 100;
int highInt = (int)intCompact / 100;
return (Integer.toString(highInt) + '.' +
StringBuilderHelper.DIGIT_TENS[lowInt] +
StringBuilderHelper.DIGIT_ONES[lowInt]) ;
}
StringBuilderHelper sbHelper = threadLocalStringBuilderHelper.get();
char[] coeff;
int offset; // offset is the starting index for coeff array
// Get the significand as an absolute value
if (intCompact != INFLATED) {
offset = sbHelper.putIntCompact(Math.abs(intCompact));
coeff = sbHelper.getCompactCharArray();
} else {
offset = 0;
coeff = intVal.abs().toString().toCharArray();
}
// Construct a buffer, with sufficient capacity for all cases.
// If E-notation is needed, length will be: +1 if negative, +1
// if '.' needed, +2 for "E+", + up to 10 for adjusted exponent.
// Otherwise it could have +1 if negative, plus leading "0.00000"
StringBuilder buf = sbHelper.getStringBuilder();
if (signum() < 0) // prefix '-' if negative
buf.append('-');
int coeffLen = coeff.length - offset;
long adjusted = -(long)scale + (coeffLen -1);
if ((scale >= 0) && (adjusted >= -6)) { // plain number
int pad = scale - coeffLen; // count of padding zeros
if (pad >= 0) { // 0.xxx form
buf.append('0');
buf.append('.');
for (; pad>0; pad--) {
buf.append('0');
}
buf.append(coeff, offset, coeffLen);
} else { // xx.xx form
buf.append(coeff, offset, -pad);
buf.append('.');
buf.append(coeff, -pad + offset, scale);
}
} else { // E-notation is needed
if (sci) { // Scientific notation
buf.append(coeff[offset]); // first character
if (coeffLen > 1) { // more to come
buf.append('.');
buf.append(coeff, offset + 1, coeffLen - 1);
}
} else { // Engineering notation
int sig = (int)(adjusted % 3);
if (sig < 0)
sig += 3; // [adjusted was negative]
adjusted -= sig; // now a multiple of 3
sig++;
if (signum() == 0) {
switch (sig) {
case 1:
buf.append('0'); // exponent is a multiple of three
break;
case 2:
buf.append("0.00");
adjusted += 3;
break;
case 3:
buf.append("0.0");
adjusted += 3;
break;
default:
throw new AssertionError("Unexpected sig value " + sig);
}
} else if (sig >= coeffLen) { // significand all in integer
buf.append(coeff, offset, coeffLen);
// may need some zeros, too
for (int i = sig - coeffLen; i > 0; i--)
buf.append('0');
} else { // xx.xxE form
buf.append(coeff, offset, sig);
buf.append('.');
buf.append(coeff, offset + sig, coeffLen - sig);
}
}
if (adjusted != 0) { // [!sci could have made 0]
buf.append('E');
if (adjusted > 0) // force sign for positive
buf.append('+');
buf.append(adjusted);
}
}
return buf.toString();
} |
Lay out this {@code BigDecimal} into a {@code char[]} array.
The Java 1.2 equivalent to this was called {@code getValueString}.
@param sci {@code true} for Scientific exponential notation;
{@code false} for Engineering
@return string with canonical string representation of this
{@code BigDecimal}
| StringBuilderHelper::layoutChars | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigInteger bigTenToThe(int n) {
if (n < 0)
return BigInteger.ZERO;
if (n < BIG_TEN_POWERS_TABLE_MAX) {
BigInteger[] pows = BIG_TEN_POWERS_TABLE;
if (n < pows.length)
return pows[n];
else
return expandBigIntegerTenPowers(n);
}
return BigInteger.TEN.pow(n);
} |
Return 10 to the power n, as a {@code BigInteger}.
@param n the power of ten to be returned (>=0)
@return a {@code BigInteger} with the value (10<sup>n</sup>)
| StringBuilderHelper::bigTenToThe | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in all fields
s.defaultReadObject();
// validate possibly bad fields
if (intVal == null) {
String message = "BigDecimal: null intVal in stream";
throw new java.io.StreamCorruptedException(message);
// [all values of scale are now allowed]
}
UnsafeHolder.setIntCompactVolatile(this, compactValFor(intVal));
} |
Reconstitute the {@code BigDecimal} instance from a stream (that is,
deserialize it).
@param s the stream being read.
| StringBuilderHelper::readObject | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Must inflate to maintain compatible serial form.
if (this.intVal == null)
UnsafeHolder.setIntValVolatile(this, BigInteger.valueOf(this.intCompact));
// Could reset intVal back to null if it has to be set.
s.defaultWriteObject();
} |
Serialize this {@code BigDecimal} to the stream in question
@param s the stream to serialize to.
| StringBuilderHelper::writeObject | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
MutableBigInteger(int[] val) {
value = val;
intLen = val.length;
} |
Construct a new MutableBigInteger with the specified value array
up to the length of the array supplied.
| MutableBigInteger::MutableBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger(BigInteger b) {
intLen = b.mag.length;
value = Arrays.copyOf(b.mag, intLen);
} |
Construct a new MutableBigInteger with a magnitude equal to the
specified BigInteger.
| MutableBigInteger::MutableBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private void ones(int n) {
if (n > value.length)
value = new int[n];
Arrays.fill(value, -1);
offset = 0;
intLen = n;
} |
Makes this number an {@code n}-int number all of whose bits are ones.
Used by Burnikel-Ziegler division.
@param n number of ints in the {@code value} array
@return a number equal to {@code ((1<<(32*n)))-1}
| MutableBigInteger::ones | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int[] getMagnitudeArray() {
if (offset > 0 || value.length != intLen)
return Arrays.copyOfRange(value, offset, offset + intLen);
return value;
} |
Internal helper method to return the magnitude array. The caller is not
supposed to modify the returned array.
| MutableBigInteger::getMagnitudeArray | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private long toLong() {
assert (intLen <= 2) : "this MutableBigInteger exceeds the range of long";
if (intLen == 0)
return 0;
long d = value[offset] & LONG_MASK;
return (intLen == 2) ? d << 32 | (value[offset + 1] & LONG_MASK) : d;
} |
Convert this MutableBigInteger to a long value. The caller has to make
sure this MutableBigInteger can be fit into long.
| MutableBigInteger::toLong | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
BigInteger toBigInteger(int sign) {
if (intLen == 0 || sign == 0)
return BigInteger.ZERO;
return new BigInteger(getMagnitudeArray(), sign);
} |
Convert this MutableBigInteger to a BigInteger object.
| MutableBigInteger::toBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
BigInteger toBigInteger() {
normalize();
return toBigInteger(isZero() ? 0 : 1);
} |
Converts this number to a nonnegative {@code BigInteger}.
| MutableBigInteger::toBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
BigDecimal toBigDecimal(int sign, int scale) {
if (intLen == 0 || sign == 0)
return BigDecimal.zeroValueOf(scale);
int[] mag = getMagnitudeArray();
int len = mag.length;
int d = mag[0];
// If this MutableBigInteger can't be fit into long, we need to
// make a BigInteger object for the resultant BigDecimal object.
if (len > 2 || (d < 0 && len == 2))
return new BigDecimal(new BigInteger(mag, sign), INFLATED, scale, 0);
long v = (len == 2) ?
((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
d & LONG_MASK;
return BigDecimal.valueOf(sign == -1 ? -v : v, scale);
} |
Convert this MutableBigInteger to BigDecimal object with the specified sign
and scale.
| MutableBigInteger::toBigDecimal | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
long toCompactValue(int sign) {
if (intLen == 0 || sign == 0)
return 0L;
int[] mag = getMagnitudeArray();
int len = mag.length;
int d = mag[0];
// If this MutableBigInteger can not be fitted into long, we need to
// make a BigInteger object for the resultant BigDecimal object.
if (len > 2 || (d < 0 && len == 2))
return INFLATED;
long v = (len == 2) ?
((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
d & LONG_MASK;
return sign == -1 ? -v : v;
} |
This is for internal use in converting from a MutableBigInteger
object into a long value given a specified sign.
returns INFLATED if value is not fit into long
| MutableBigInteger::toCompactValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void clear() {
offset = intLen = 0;
for (int index=0, n=value.length; index < n; index++)
value[index] = 0;
} |
Clear out a MutableBigInteger for reuse.
| MutableBigInteger::clear | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void reset() {
offset = intLen = 0;
} |
Set a MutableBigInteger to zero, removing its offset.
| MutableBigInteger::reset | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
final int compare(MutableBigInteger b) {
int blen = b.intLen;
if (intLen < blen)
return -1;
if (intLen > blen)
return 1;
// Add Integer.MIN_VALUE to make the comparison act as unsigned integer
// comparison.
int[] bval = b.value;
for (int i = offset, j = b.offset; i < intLen + offset; i++, j++) {
int b1 = value[i] + 0x80000000;
int b2 = bval[j] + 0x80000000;
if (b1 < b2)
return -1;
if (b1 > b2)
return 1;
}
return 0;
} |
Compare the magnitude of two MutableBigIntegers. Returns -1, 0 or 1
as this MutableBigInteger is numerically less than, equal to, or
greater than <tt>b</tt>.
| MutableBigInteger::compare | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int compareShifted(MutableBigInteger b, int ints) {
int blen = b.intLen;
int alen = intLen - ints;
if (alen < blen)
return -1;
if (alen > blen)
return 1;
// Add Integer.MIN_VALUE to make the comparison act as unsigned integer
// comparison.
int[] bval = b.value;
for (int i = offset, j = b.offset; i < alen + offset; i++, j++) {
int b1 = value[i] + 0x80000000;
int b2 = bval[j] + 0x80000000;
if (b1 < b2)
return -1;
if (b1 > b2)
return 1;
}
return 0;
} |
Returns a value equal to what {@code b.leftShift(32*ints); return compare(b);}
would return, but doesn't change the value of {@code b}.
| MutableBigInteger::compareShifted | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
final int compareHalf(MutableBigInteger b) {
int blen = b.intLen;
int len = intLen;
if (len <= 0)
return blen <= 0 ? 0 : -1;
if (len > blen)
return 1;
if (len < blen - 1)
return -1;
int[] bval = b.value;
int bstart = 0;
int carry = 0;
// Only 2 cases left:len == blen or len == blen - 1
if (len != blen) { // len == blen - 1
if (bval[bstart] == 1) {
++bstart;
carry = 0x80000000;
} else
return -1;
}
// compare values with right-shifted values of b,
// carrying shifted-out bits across words
int[] val = value;
for (int i = offset, j = bstart; i < len + offset;) {
int bv = bval[j++];
long hb = ((bv >>> 1) + carry) & LONG_MASK;
long v = val[i++] & LONG_MASK;
if (v != hb)
return v < hb ? -1 : 1;
carry = (bv & 1) << 31; // carray will be either 0x80000000 or 0
}
return carry == 0 ? 0 : -1;
} |
Compare this against half of a MutableBigInteger object (Needed for
remainder tests).
Assumes no leading unnecessary zeros, which holds for results
from divide().
| MutableBigInteger::compareHalf | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final int getLowestSetBit() {
if (intLen == 0)
return -1;
int j, b;
for (j=intLen-1; (j > 0) && (value[j+offset] == 0); j--)
;
b = value[j+offset];
if (b == 0)
return -1;
return ((intLen-1-j)<<5) + Integer.numberOfTrailingZeros(b);
} |
Return the index of the lowest set bit in this MutableBigInteger. If the
magnitude of this MutableBigInteger is zero, -1 is returned.
| MutableBigInteger::getLowestSetBit | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final int getInt(int index) {
return value[offset+index];
} |
Return the int in use in this MutableBigInteger at the specified
index. This method is not used because it is not inlined on all
platforms.
| MutableBigInteger::getInt | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final long getLong(int index) {
return value[offset+index] & LONG_MASK;
} |
Return a long which is equal to the unsigned value of the int in
use in this MutableBigInteger at the specified index. This method is
not used because it is not inlined on all platforms.
| MutableBigInteger::getLong | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
final void normalize() {
if (intLen == 0) {
offset = 0;
return;
}
int index = offset;
if (value[index] != 0)
return;
int indexBound = index+intLen;
do {
index++;
} while(index < indexBound && value[index] == 0);
int numZeros = index - offset;
intLen -= numZeros;
offset = (intLen == 0 ? 0 : offset+numZeros);
} |
Ensure that the MutableBigInteger is in normal form, specifically
making sure that there are no leading zeros, and that if the
magnitude is zero, then intLen is zero.
| MutableBigInteger::normalize | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final void ensureCapacity(int len) {
if (value.length < len) {
value = new int[len];
offset = 0;
intLen = len;
}
} |
If this MutableBigInteger cannot hold len words, increase the size
of the value array to len words.
| MutableBigInteger::ensureCapacity | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
int[] toIntArray() {
int[] result = new int[intLen];
for(int i=0; i < intLen; i++)
result[i] = value[offset+i];
return result;
} |
Convert this MutableBigInteger into an int array with no leading
zeros, of a length that is equal to this MutableBigInteger's intLen.
| MutableBigInteger::toIntArray | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void setInt(int index, int val) {
value[offset + index] = val;
} |
Sets the int at index+offset in this MutableBigInteger to val.
This does not get inlined on all platforms so it is not used
as often as originally intended.
| MutableBigInteger::setInt | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void setValue(int[] val, int length) {
value = val;
intLen = length;
offset = 0;
} |
Sets this MutableBigInteger's value array to the specified array.
The intLen is set to the specified length.
| MutableBigInteger::setValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void copyValue(MutableBigInteger src) {
int len = src.intLen;
if (value.length < len)
value = new int[len];
System.arraycopy(src.value, src.offset, value, 0, len);
intLen = len;
offset = 0;
} |
Sets this MutableBigInteger's value array to a copy of the specified
array. The intLen is set to the length of the new array.
| MutableBigInteger::copyValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void copyValue(int[] val) {
int len = val.length;
if (value.length < len)
value = new int[len];
System.arraycopy(val, 0, value, 0, len);
intLen = len;
offset = 0;
} |
Sets this MutableBigInteger's value array to a copy of the specified
array. The intLen is set to the length of the specified array.
| MutableBigInteger::copyValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isOne() {
return (intLen == 1) && (value[offset] == 1);
} |
Returns true iff this MutableBigInteger has a value of one.
| MutableBigInteger::isOne | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isZero() {
return (intLen == 0);
} |
Returns true iff this MutableBigInteger has a value of zero.
| MutableBigInteger::isZero | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isEven() {
return (intLen == 0) || ((value[offset + intLen - 1] & 1) == 0);
} |
Returns true iff this MutableBigInteger is even.
| MutableBigInteger::isEven | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isOdd() {
return isZero() ? false : ((value[offset + intLen - 1] & 1) == 1);
} |
Returns true iff this MutableBigInteger is odd.
| MutableBigInteger::isOdd | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isNormal() {
if (intLen + offset > value.length)
return false;
if (intLen == 0)
return true;
return (value[offset] != 0);
} |
Returns true iff this MutableBigInteger is in normal form. A
MutableBigInteger is in normal form if it has no leading zeros
after the offset, and intLen + offset <= value.length.
| MutableBigInteger::isNormal | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
public String toString() {
BigInteger b = toBigInteger(1);
return b.toString();
} |
Returns a String representation of this MutableBigInteger in radix 10.
| MutableBigInteger::toString | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void safeRightShift(int n) {
if (n/32 >= intLen) {
reset();
} else {
rightShift(n);
}
} |
Like {@link #rightShift(int)} but {@code n} can be greater than the length of the number.
| MutableBigInteger::safeRightShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void rightShift(int n) {
if (intLen == 0)
return;
int nInts = n >>> 5;
int nBits = n & 0x1F;
this.intLen -= nInts;
if (nBits == 0)
return;
int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
if (nBits >= bitsInHighWord) {
this.primitiveLeftShift(32 - nBits);
this.intLen--;
} else {
primitiveRightShift(nBits);
}
} |
Right shift this MutableBigInteger n bits. The MutableBigInteger is left
in normal form.
| MutableBigInteger::rightShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void safeLeftShift(int n) {
if (n > 0) {
leftShift(n);
}
} |
Like {@link #leftShift(int)} but {@code n} can be zero.
| MutableBigInteger::safeLeftShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void leftShift(int n) {
/*
* If there is enough storage space in this MutableBigInteger already
* the available space will be used. Space to the right of the used
* ints in the value array is faster to utilize, so the extra space
* will be taken from the right if possible.
*/
if (intLen == 0)
return;
int nInts = n >>> 5;
int nBits = n&0x1F;
int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
// If shift can be done without moving words, do so
if (n <= (32-bitsInHighWord)) {
primitiveLeftShift(nBits);
return;
}
int newLen = intLen + nInts +1;
if (nBits <= (32-bitsInHighWord))
newLen--;
if (value.length < newLen) {
// The array must grow
int[] result = new int[newLen];
for (int i=0; i < intLen; i++)
result[i] = value[offset+i];
setValue(result, newLen);
} else if (value.length - offset >= newLen) {
// Use space on right
for(int i=0; i < newLen - intLen; i++)
value[offset+intLen+i] = 0;
} else {
// Must use space on left
for (int i=0; i < intLen; i++)
value[i] = value[offset+i];
for (int i=intLen; i < newLen; i++)
value[i] = 0;
offset = 0;
}
intLen = newLen;
if (nBits == 0)
return;
if (nBits <= (32-bitsInHighWord))
primitiveLeftShift(nBits);
else
primitiveRightShift(32 -nBits);
} |
Left shift this MutableBigInteger n bits.
| MutableBigInteger::leftShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int divadd(int[] a, int[] result, int offset) {
long carry = 0;
for (int j=a.length-1; j >= 0; j--) {
long sum = (a[j] & LONG_MASK) +
(result[j+offset] & LONG_MASK) + carry;
result[j+offset] = (int)sum;
carry = sum >>> 32;
}
return (int)carry;
} |
A primitive used for division. This method adds in one multiple of the
divisor a back to the dividend result at a specified offset. It is used
when qhat was estimated too large, and must be adjusted.
| MutableBigInteger::divadd | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final void primitiveLeftShift(int n) {
int[] val = value;
int n2 = 32 - n;
for (int i=offset, c=val[i], m=i+intLen-1; i < m; i++) {
int b = c;
c = val[i+1];
val[i] = (b << n) | (c >>> n2);
}
val[offset+intLen-1] <<= n;
} |
Left shift this MutableBigInteger n bits, where n is
less than 32.
Assumes that intLen > 0, n > 0 for speed
| MutableBigInteger::primitiveLeftShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private BigInteger getLower(int n) {
if (isZero()) {
return BigInteger.ZERO;
} else if (intLen < n) {
return toBigInteger(1);
} else {
// strip zeros
int len = n;
while (len > 0 && value[offset+intLen-len] == 0)
len--;
int sign = len > 0 ? 1 : 0;
return new BigInteger(Arrays.copyOfRange(value, offset+intLen-len, offset+intLen), sign);
}
} |
Returns a {@code BigInteger} equal to the {@code n}
low ints of this number.
| MutableBigInteger::getLower | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private void keepLower(int n) {
if (intLen >= n) {
offset += intLen - n;
intLen = n;
}
} |
Discards all ints whose index is greater than {@code n}.
| MutableBigInteger::keepLower | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void add(MutableBigInteger addend) {
int x = intLen;
int y = addend.intLen;
int resultLen = (intLen > addend.intLen ? intLen : addend.intLen);
int[] result = (value.length < resultLen ? new int[resultLen] : value);
int rstart = result.length-1;
long sum;
long carry = 0;
// Add common parts of both numbers
while(x > 0 && y > 0) {
x--; y--;
sum = (value[x+offset] & LONG_MASK) +
(addend.value[y+addend.offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
// Add remainder of the longer number
while(x > 0) {
x--;
if (carry == 0 && result == value && rstart == (x + offset))
return;
sum = (value[x+offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
while(y > 0) {
y--;
sum = (addend.value[y+addend.offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
if (carry > 0) { // Result must grow in length
resultLen++;
if (result.length < resultLen) {
int temp[] = new int[resultLen];
// Result one word longer from carry-out; copy low-order
// bits into new result.
System.arraycopy(result, 0, temp, 1, result.length);
temp[0] = 1;
result = temp;
} else {
result[rstart--] = 1;
}
}
value = result;
intLen = resultLen;
offset = result.length - resultLen;
} |
Adds the contents of two MutableBigInteger objects.The result
is placed within this MutableBigInteger.
The contents of the addend are not changed.
| MutableBigInteger::add | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void addShifted(MutableBigInteger addend, int n) {
if (addend.isZero()) {
return;
}
int x = intLen;
int y = addend.intLen + n;
int resultLen = (intLen > y ? intLen : y);
int[] result = (value.length < resultLen ? new int[resultLen] : value);
int rstart = result.length-1;
long sum;
long carry = 0;
// Add common parts of both numbers
while (x > 0 && y > 0) {
x--; y--;
int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
sum = (value[x+offset] & LONG_MASK) +
(bval & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
// Add remainder of the longer number
while (x > 0) {
x--;
if (carry == 0 && result == value && rstart == (x + offset)) {
return;
}
sum = (value[x+offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
while (y > 0) {
y--;
int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
sum = (bval & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
if (carry > 0) { // Result must grow in length
resultLen++;
if (result.length < resultLen) {
int temp[] = new int[resultLen];
// Result one word longer from carry-out; copy low-order
// bits into new result.
System.arraycopy(result, 0, temp, 1, result.length);
temp[0] = 1;
result = temp;
} else {
result[rstart--] = 1;
}
}
value = result;
intLen = resultLen;
offset = result.length - resultLen;
} |
Adds the value of {@code addend} shifted {@code n} ints to the left.
Has the same effect as {@code addend.leftShift(32*ints); add(addend);}
but doesn't change the value of {@code addend}.
| MutableBigInteger::addShifted | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void addDisjoint(MutableBigInteger addend, int n) {
if (addend.isZero())
return;
int x = intLen;
int y = addend.intLen + n;
int resultLen = (intLen > y ? intLen : y);
int[] result;
if (value.length < resultLen)
result = new int[resultLen];
else {
result = value;
Arrays.fill(value, offset+intLen, value.length, 0);
}
int rstart = result.length-1;
// copy from this if needed
System.arraycopy(value, offset, result, rstart+1-x, x);
y -= x;
rstart -= x;
int len = Math.min(y, addend.value.length-addend.offset);
System.arraycopy(addend.value, addend.offset, result, rstart+1-y, len);
// zero the gap
for (int i=rstart+1-y+len; i < rstart+1; i++)
result[i] = 0;
value = result;
intLen = resultLen;
offset = result.length - resultLen;
} |
Like {@link #addShifted(MutableBigInteger, int)} but {@code this.intLen} must
not be greater than {@code n}. In other words, concatenates {@code this}
and {@code addend}.
| MutableBigInteger::addDisjoint | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void addLower(MutableBigInteger addend, int n) {
MutableBigInteger a = new MutableBigInteger(addend);
if (a.offset + a.intLen >= n) {
a.offset = a.offset + a.intLen - n;
a.intLen = n;
}
a.normalize();
add(a);
} |
Adds the low {@code n} ints of {@code addend}.
| MutableBigInteger::addLower | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
int subtract(MutableBigInteger b) {
MutableBigInteger a = this;
int[] result = value;
int sign = a.compare(b);
if (sign == 0) {
reset();
return 0;
}
if (sign < 0) {
MutableBigInteger tmp = a;
a = b;
b = tmp;
}
int resultLen = a.intLen;
if (result.length < resultLen)
result = new int[resultLen];
long diff = 0;
int x = a.intLen;
int y = b.intLen;
int rstart = result.length - 1;
// Subtract common parts of both numbers
while (y > 0) {
x--; y--;
diff = (a.value[x+a.offset] & LONG_MASK) -
(b.value[y+b.offset] & LONG_MASK) - ((int)-(diff>>32));
result[rstart--] = (int)diff;
}
// Subtract remainder of longer number
while (x > 0) {
x--;
diff = (a.value[x+a.offset] & LONG_MASK) - ((int)-(diff>>32));
result[rstart--] = (int)diff;
}
value = result;
intLen = resultLen;
offset = value.length - resultLen;
normalize();
return sign;
} |
Subtracts the smaller of this and b from the larger and places the
result into this MutableBigInteger.
| MutableBigInteger::subtract | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int difference(MutableBigInteger b) {
MutableBigInteger a = this;
int sign = a.compare(b);
if (sign == 0)
return 0;
if (sign < 0) {
MutableBigInteger tmp = a;
a = b;
b = tmp;
}
long diff = 0;
int x = a.intLen;
int y = b.intLen;
// Subtract common parts of both numbers
while (y > 0) {
x--; y--;
diff = (a.value[a.offset+ x] & LONG_MASK) -
(b.value[b.offset+ y] & LONG_MASK) - ((int)-(diff>>32));
a.value[a.offset+x] = (int)diff;
}
// Subtract remainder of longer number
while (x > 0) {
x--;
diff = (a.value[a.offset+ x] & LONG_MASK) - ((int)-(diff>>32));
a.value[a.offset+x] = (int)diff;
}
a.normalize();
return sign;
} |
Subtracts the smaller of a and b from the larger and places the result
into the larger. Returns 1 if the answer is in a, -1 if in b, 0 if no
operation was performed.
| MutableBigInteger::difference | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void multiply(MutableBigInteger y, MutableBigInteger z) {
int xLen = intLen;
int yLen = y.intLen;
int newLen = xLen + yLen;
// Put z into an appropriate state to receive product
if (z.value.length < newLen)
z.value = new int[newLen];
z.offset = 0;
z.intLen = newLen;
// The first iteration is hoisted out of the loop to avoid extra add
long carry = 0;
for (int j=yLen-1, k=yLen+xLen-1; j >= 0; j--, k--) {
long product = (y.value[j+y.offset] & LONG_MASK) *
(value[xLen-1+offset] & LONG_MASK) + carry;
z.value[k] = (int)product;
carry = product >>> 32;
}
z.value[xLen-1] = (int)carry;
// Perform the multiplication word by word
for (int i = xLen-2; i >= 0; i--) {
carry = 0;
for (int j=yLen-1, k=yLen+i; j >= 0; j--, k--) {
long product = (y.value[j+y.offset] & LONG_MASK) *
(value[i+offset] & LONG_MASK) +
(z.value[k] & LONG_MASK) + carry;
z.value[k] = (int)product;
carry = product >>> 32;
}
z.value[i] = (int)carry;
}
// Remove leading zeros from product
z.normalize();
} |
Multiply the contents of two MutableBigInteger objects. The result is
placed into MutableBigInteger z. The contents of y are not changed.
| MutableBigInteger::multiply | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void mul(int y, MutableBigInteger z) {
if (y == 1) {
z.copyValue(this);
return;
}
if (y == 0) {
z.clear();
return;
}
// Perform the multiplication word by word
long ylong = y & LONG_MASK;
int[] zval = (z.value.length < intLen+1 ? new int[intLen + 1]
: z.value);
long carry = 0;
for (int i = intLen-1; i >= 0; i--) {
long product = ylong * (value[i+offset] & LONG_MASK) + carry;
zval[i+1] = (int)product;
carry = product >>> 32;
}
if (carry == 0) {
z.offset = 1;
z.intLen = intLen;
} else {
z.offset = 0;
z.intLen = intLen + 1;
zval[0] = (int)carry;
}
z.value = zval;
} |
Multiply the contents of this MutableBigInteger by the word y. The
result is placed into z.
| MutableBigInteger::mul | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
int divideOneWord(int divisor, MutableBigInteger quotient) {
long divisorLong = divisor & LONG_MASK;
// Special case of one word dividend
if (intLen == 1) {
long dividendValue = value[offset] & LONG_MASK;
int q = (int) (dividendValue / divisorLong);
int r = (int) (dividendValue - q * divisorLong);
quotient.value[0] = q;
quotient.intLen = (q == 0) ? 0 : 1;
quotient.offset = 0;
return r;
}
if (quotient.value.length < intLen)
quotient.value = new int[intLen];
quotient.offset = 0;
quotient.intLen = intLen;
// Normalize the divisor
int shift = Integer.numberOfLeadingZeros(divisor);
int rem = value[offset];
long remLong = rem & LONG_MASK;
if (remLong < divisorLong) {
quotient.value[0] = 0;
} else {
quotient.value[0] = (int)(remLong / divisorLong);
rem = (int) (remLong - (quotient.value[0] * divisorLong));
remLong = rem & LONG_MASK;
}
int xlen = intLen;
while (--xlen > 0) {
long dividendEstimate = (remLong << 32) |
(value[offset + intLen - xlen] & LONG_MASK);
int q;
if (dividendEstimate >= 0) {
q = (int) (dividendEstimate / divisorLong);
rem = (int) (dividendEstimate - q * divisorLong);
} else {
long tmp = divWord(dividendEstimate, divisor);
q = (int) (tmp & LONG_MASK);
rem = (int) (tmp >>> 32);
}
quotient.value[intLen - xlen] = q;
remLong = rem & LONG_MASK;
}
quotient.normalize();
// Unnormalize
if (shift > 0)
return rem % divisor;
else
return rem;
} |
This method is used for division of an n word dividend by a one word
divisor. The quotient is placed into quotient. The one word divisor is
specified by divisor.
@return the remainder of the division is returned.
| MutableBigInteger::divideOneWord | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient) {
return divide(b,quotient,true);
} |
Calculates the quotient of this div b and places the quotient in the
provided MutableBigInteger objects and the remainder object is returned.
| MutableBigInteger::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divideKnuth(MutableBigInteger b, MutableBigInteger quotient) {
return divideKnuth(b,quotient,true);
} |
@see #divideKnuth(MutableBigInteger, MutableBigInteger, boolean)
| MutableBigInteger::divideKnuth | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divideKnuth(MutableBigInteger b, MutableBigInteger quotient, boolean needRemainder) {
if (b.intLen == 0)
throw new ArithmeticException("BigInteger divide by zero");
// Dividend is zero
if (intLen == 0) {
quotient.intLen = quotient.offset = 0;
return needRemainder ? new MutableBigInteger() : null;
}
int cmp = compare(b);
// Dividend less than divisor
if (cmp < 0) {
quotient.intLen = quotient.offset = 0;
return needRemainder ? new MutableBigInteger(this) : null;
}
// Dividend equal to divisor
if (cmp == 0) {
quotient.value[0] = quotient.intLen = 1;
quotient.offset = 0;
return needRemainder ? new MutableBigInteger() : null;
}
quotient.clear();
// Special case one word divisor
if (b.intLen == 1) {
int r = divideOneWord(b.value[b.offset], quotient);
if(needRemainder) {
if (r == 0)
return new MutableBigInteger();
return new MutableBigInteger(r);
} else {
return null;
}
}
// Cancel common powers of two if we're above the KNUTH_POW2_* thresholds
if (intLen >= KNUTH_POW2_THRESH_LEN) {
int trailingZeroBits = Math.min(getLowestSetBit(), b.getLowestSetBit());
if (trailingZeroBits >= KNUTH_POW2_THRESH_ZEROS*32) {
MutableBigInteger a = new MutableBigInteger(this);
b = new MutableBigInteger(b);
a.rightShift(trailingZeroBits);
b.rightShift(trailingZeroBits);
MutableBigInteger r = a.divideKnuth(b, quotient);
r.leftShift(trailingZeroBits);
return r;
}
}
return divideMagnitude(b, quotient, needRemainder);
} |
Calculates the quotient of this div b and places the quotient in the
provided MutableBigInteger objects and the remainder object is returned.
Uses Algorithm D in Knuth section 4.3.1.
Many optimizations to that algorithm have been adapted from the Colin
Plumb C library.
It special cases one word divisors for speed. The content of b is not
changed.
| MutableBigInteger::divideKnuth | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divideAndRemainderBurnikelZiegler(MutableBigInteger b, MutableBigInteger quotient) {
int r = intLen;
int s = b.intLen;
// Clear the quotient
quotient.offset = quotient.intLen = 0;
if (r < s) {
return this;
} else {
// Unlike Knuth division, we don't check for common powers of two here because
// BZ already runs faster if both numbers contain powers of two and cancelling them has no
// additional benefit.
// step 1: let m = min{2^k | (2^k)*BURNIKEL_ZIEGLER_THRESHOLD > s}
int m = 1 << (32-Integer.numberOfLeadingZeros(s/BigInteger.BURNIKEL_ZIEGLER_THRESHOLD));
int j = (s+m-1) / m; // step 2a: j = ceil(s/m)
int n = j * m; // step 2b: block length in 32-bit units
long n32 = 32L * n; // block length in bits
int sigma = (int) Math.max(0, n32 - b.bitLength()); // step 3: sigma = max{T | (2^T)*B < beta^n}
MutableBigInteger bShifted = new MutableBigInteger(b);
bShifted.safeLeftShift(sigma); // step 4a: shift b so its length is a multiple of n
safeLeftShift(sigma); // step 4b: shift this by the same amount
// step 5: t is the number of blocks needed to accommodate this plus one additional bit
int t = (int) ((bitLength()+n32) / n32);
if (t < 2) {
t = 2;
}
// step 6: conceptually split this into blocks a[t-1], ..., a[0]
MutableBigInteger a1 = getBlock(t-1, t, n); // the most significant block of this
// step 7: z[t-2] = [a[t-1], a[t-2]]
MutableBigInteger z = getBlock(t-2, t, n); // the second to most significant block
z.addDisjoint(a1, n); // z[t-2]
// do schoolbook division on blocks, dividing 2-block numbers by 1-block numbers
MutableBigInteger qi = new MutableBigInteger();
MutableBigInteger ri;
for (int i=t-2; i > 0; i--) {
// step 8a: compute (qi,ri) such that z=b*qi+ri
ri = z.divide2n1n(bShifted, qi);
// step 8b: z = [ri, a[i-1]]
z = getBlock(i-1, t, n); // a[i-1]
z.addDisjoint(ri, n);
quotient.addShifted(qi, i*n); // update q (part of step 9)
}
// final iteration of step 8: do the loop one more time for i=0 but leave z unchanged
ri = z.divide2n1n(bShifted, qi);
quotient.add(qi);
ri.rightShift(sigma); // step 9: this and b were shifted, so shift back
return ri;
}
} |
Computes {@code this/b} and {@code this%b} using the
<a href="http://cr.yp.to/bib/1998/burnikel.ps"> Burnikel-Ziegler algorithm</a>.
This method implements algorithm 3 from pg. 9 of the Burnikel-Ziegler paper.
The parameter beta was chosen to b 2<sup>32</sup> so almost all shifts are
multiples of 32 bits.<br/>
{@code this} and {@code b} must be nonnegative.
@param b the divisor
@param quotient output parameter for {@code this/b}
@return the remainder
| MutableBigInteger::divideAndRemainderBurnikelZiegler | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger divide2n1n(MutableBigInteger b, MutableBigInteger quotient) {
int n = b.intLen;
// step 1: base case
if (n%2 != 0 || n < BigInteger.BURNIKEL_ZIEGLER_THRESHOLD) {
return divideKnuth(b, quotient);
}
// step 2: view this as [a1,a2,a3,a4] where each ai is n/2 ints or less
MutableBigInteger aUpper = new MutableBigInteger(this);
aUpper.safeRightShift(32*(n/2)); // aUpper = [a1,a2,a3]
keepLower(n/2); // this = a4
// step 3: q1=aUpper/b, r1=aUpper%b
MutableBigInteger q1 = new MutableBigInteger();
MutableBigInteger r1 = aUpper.divide3n2n(b, q1);
// step 4: quotient=[r1,this]/b, r2=[r1,this]%b
addDisjoint(r1, n/2); // this = [r1,this]
MutableBigInteger r2 = divide3n2n(b, quotient);
// step 5: let quotient=[q1,quotient] and return r2
quotient.addDisjoint(q1, n/2);
return r2;
} |
This method implements algorithm 1 from pg. 4 of the Burnikel-Ziegler paper.
It divides a 2n-digit number by a n-digit number.<br/>
The parameter beta is 2<sup>32</sup> so all shifts are multiples of 32 bits.
<br/>
{@code this} must be a nonnegative number such that {@code this.bitLength() <= 2*b.bitLength()}
@param b a positive number such that {@code b.bitLength()} is even
@param quotient output parameter for {@code this/b}
@return {@code this%b}
| MutableBigInteger::divide2n1n | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger divide3n2n(MutableBigInteger b, MutableBigInteger quotient) {
int n = b.intLen / 2; // half the length of b in ints
// step 1: view this as [a1,a2,a3] where each ai is n ints or less; let a12=[a1,a2]
MutableBigInteger a12 = new MutableBigInteger(this);
a12.safeRightShift(32*n);
// step 2: view b as [b1,b2] where each bi is n ints or less
MutableBigInteger b1 = new MutableBigInteger(b);
b1.safeRightShift(n * 32);
BigInteger b2 = b.getLower(n);
MutableBigInteger r;
MutableBigInteger d;
if (compareShifted(b, n) < 0) {
// step 3a: if a1<b1, let quotient=a12/b1 and r=a12%b1
r = a12.divide2n1n(b1, quotient);
// step 4: d=quotient*b2
d = new MutableBigInteger(quotient.toBigInteger().multiply(b2));
} else {
// step 3b: if a1>=b1, let quotient=beta^n-1 and r=a12-b1*2^n+b1
quotient.ones(n);
a12.add(b1);
b1.leftShift(32*n);
a12.subtract(b1);
r = a12;
// step 4: d=quotient*b2=(b2 << 32*n) - b2
d = new MutableBigInteger(b2);
d.leftShift(32 * n);
d.subtract(new MutableBigInteger(b2));
}
// step 5: r = r*beta^n + a3 - d (paper says a4)
// However, don't subtract d until after the while loop so r doesn't become negative
r.leftShift(32 * n);
r.addLower(this, n);
// step 6: add b until r>=d
while (r.compare(d) < 0) {
r.add(b);
quotient.subtract(MutableBigInteger.ONE);
}
r.subtract(d);
return r;
} |
This method implements algorithm 2 from pg. 5 of the Burnikel-Ziegler paper.
It divides a 3n-digit number by a 2n-digit number.<br/>
The parameter beta is 2<sup>32</sup> so all shifts are multiples of 32 bits.<br/>
<br/>
{@code this} must be a nonnegative number such that {@code 2*this.bitLength() <= 3*b.bitLength()}
@param quotient output parameter for {@code this/b}
@return {@code this%b}
| MutableBigInteger::divide3n2n | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) {
int blockStart = index * blockLength;
if (blockStart >= intLen) {
return new MutableBigInteger();
}
int blockEnd;
if (index == numBlocks-1) {
blockEnd = intLen;
} else {
blockEnd = (index+1) * blockLength;
}
if (blockEnd > intLen) {
return new MutableBigInteger();
}
int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart);
return new MutableBigInteger(newVal);
} |
Returns a {@code MutableBigInteger} containing {@code blockLength} ints from
{@code this} number, starting at {@code index*blockLength}.<br/>
Used by Burnikel-Ziegler division.
@param index the block index
@param numBlocks the total number of blocks in {@code this} number
@param blockLength length of one block in units of 32 bits
@return
| MutableBigInteger::getBlock | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
long bitLength() {
if (intLen == 0)
return 0;
return intLen*32L - Integer.numberOfLeadingZeros(value[offset]);
} |
Returns a {@code MutableBigInteger} containing {@code blockLength} ints from
{@code this} number, starting at {@code index*blockLength}.<br/>
Used by Burnikel-Ziegler division.
@param index the block index
@param numBlocks the total number of blocks in {@code this} number
@param blockLength length of one block in units of 32 bits
@return
private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) {
int blockStart = index * blockLength;
if (blockStart >= intLen) {
return new MutableBigInteger();
}
int blockEnd;
if (index == numBlocks-1) {
blockEnd = intLen;
} else {
blockEnd = (index+1) * blockLength;
}
if (blockEnd > intLen) {
return new MutableBigInteger();
}
int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart);
return new MutableBigInteger(newVal);
}
/** @see BigInteger#bitLength() | MutableBigInteger::bitLength | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
long divide(long v, MutableBigInteger quotient) {
if (v == 0)
throw new ArithmeticException("BigInteger divide by zero");
// Dividend is zero
if (intLen == 0) {
quotient.intLen = quotient.offset = 0;
return 0;
}
if (v < 0)
v = -v;
int d = (int)(v >>> 32);
quotient.clear();
// Special case on word divisor
if (d == 0)
return divideOneWord((int)v, quotient) & LONG_MASK;
else {
return divideLongMagnitude(v, quotient).toLong();
}
} |
Internally used to calculate the quotient of this div v and places the
quotient in the provided MutableBigInteger object and the remainder is
returned.
@return the remainder of the division will be returned.
| MutableBigInteger::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.