Liking cljdoc? Tell your friends :D

jdk.math.BigDecimal

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale).

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.

The BigDecimal class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an exception is thrown; otherwise, calculations can be carried out to a chosen precision and rounding mode by supplying an appropriate MathContext object to the operation. In either case, eight rounding modes are provided for the control of rounding. Using the integer fields in this class (such as ROUND_HALF_UP) to represent rounding mode is largely obsolete; the enumeration values of the RoundingMode enum, (such as RoundingMode.HALF_UP) should be used instead.

When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.) As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

When the precision setting is not 0, the rules of BigDecimal arithmetic are broadly compatible with selected modes of operation of the arithmetic defined in ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 (section 7.4). Unlike those standards, BigDecimal includes many rounding modes, which were mandatory for division in BigDecimal releases prior to 5. Any conflicts between these ANSI standards and the BigDecimal specification are resolved in favor of BigDecimal.

Since the same numerical value can have different representations (with different scales), the rules of arithmetic and rounding must specify both the numerical result and the scale used in the result's representation.

In general the rounding modes and precision setting determine how operations return results with a limited number of digits when the exact result has more digits (perhaps infinitely many in the case of division) than the number of digits returned.

First, the total number of digits to return is specified by the MathContext's precision setting; this determines the result's precision. The digit count starts from the leftmost nonzero digit of the exact result. The rounding mode determines how any discarded trailing digits affect the returned result.

For all arithmetic operators , the operation is carried out as though an exact intermediate result were first calculated and then rounded to the number of digits specified by the precision setting (if necessary), using the selected rounding mode. If the exact result is not returned, some digit positions of the exact result are discarded. When rounding increases the magnitude of the returned result, it is possible for a new digit position to be created by a carry propagating to a leading "9" digit. For example, rounding the value 999.9 to three digits rounding up would be numerically equal to one thousand, represented as 100×101. In such cases, the new "1" is the leading digit position of the returned result.

Besides a logical exact result, each arithmetic operation has a preferred scale for representing a result. The preferred scale for each operation is listed in the table below.

Preferred Scales for Results of Arithmetic Operations

OperationPreferred Scale of Result Addmax(addend.scale(), augend.scale()) Subtractmax(minuend.scale(), subtrahend.scale()) Multiplymultiplier.scale() multiplicand.scale() Dividedividend.scale() - divisor.scale()

These scales are the ones used by the methods which return exact arithmetic results; except that an exact divide may have to use a larger scale since the exact result may have more digits. For example, 1/32 is 0.03125.

Before rounding, the scale of the logical exact intermediate result is the preferred scale for that operation. If the exact numerical result cannot be represented in precision digits, rounding selects the set of digits to return and the scale of the result is reduced from the scale of the intermediate result to the least scale which can represent the precision digits actually returned. If the exact result can be represented with at most precision digits, the representation of the result with the scale closest to the preferred scale is returned. In particular, an exactly representable quotient may be represented in fewer than precision digits by removing trailing zeros and decreasing the scale. For example, rounding to three digits using the floor rounding mode,

19/100 = 0.19 // integer=19, scale=2

but

21/110 = 0.190 // integer=190, scale=3

Note that for add, subtract, and multiply, the reduction in scale will equal the number of digit positions of the exact result which are discarded. If the rounding causes a carry propagation to create a new high-order digit position, an additional digit of the result is discarded than when no new digit position is created.

Other methods may have slightly different rounding semantics. For example, the result of the pow method using the specified algorithm can occasionally differ from the rounded mathematical result by more than one unit in the last place, one ulp.

Two types of operations are provided for manipulating the scale of a BigDecimal: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations (setScale and round) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale or precision is the specified value; that is, they increase or decrease the precision of the stored number with minimal effect on its value. Decimal point motion operations (movePointLeft and movePointRight) return a BigDecimal created from the operand by moving the decimal point a specified distance in the specified direction.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of BigDecimal methods. The pseudo-code expression (i j) is shorthand for "a BigDecimal whose value is that of the BigDecimal i added to that of the BigDecimal j." The pseudo-code expression (i == j) is shorthand for "true if and only if the BigDecimal i represents the same value as the BigDecimal j." Other pseudo-code expressions are interpreted similarly. Square brackets are used to represent the particular BigInteger and scale pair defining a BigDecimal value; for example [19, 2] is the BigDecimal numerically equal to 0.19 having a scale of 2.

Note: care should be exercised if BigDecimal objects are used as keys in a SortedMap or elements in a SortedSet since BigDecimal's natural ordering is inconsistent with equals. See Comparable, SortedMap or SortedSet for more information.

All methods and constructors for this class throw NullPointerException when passed a null object reference for any input parameter.

Immutable, arbitrary-precision signed decimal numbers.  A
BigDecimal consists of an arbitrary precision integer
unscaled value and a 32-bit integer scale.  If zero
or positive, the scale is the number of digits to the right of the
decimal point.  If negative, the unscaled value of the number is
multiplied by ten to the power of the negation of the scale.  The
value of the number represented by the BigDecimal is
therefore (unscaledValue × 10-scale).

The BigDecimal class provides operations for
arithmetic, scale manipulation, rounding, comparison, hashing, and
format conversion.  The toString() method provides a
canonical representation of a BigDecimal.

The BigDecimal class gives its user complete control
over rounding behavior.  If no rounding mode is specified and the
exact result cannot be represented, an exception is thrown;
otherwise, calculations can be carried out to a chosen precision
and rounding mode by supplying an appropriate MathContext
object to the operation.  In either case, eight rounding
modes are provided for the control of rounding.  Using the
integer fields in this class (such as ROUND_HALF_UP) to
represent rounding mode is largely obsolete; the enumeration values
of the RoundingMode enum, (such as RoundingMode.HALF_UP) should be used instead.

When a MathContext object is supplied with a precision
setting of 0 (for example, MathContext.UNLIMITED),
arithmetic operations are exact, as are the arithmetic methods
which take no MathContext object.  (This is the only
behavior that was supported in releases prior to 5.)  As a
corollary of computing the exact result, the rounding mode setting
of a MathContext object with a precision setting of 0 is
not used and thus irrelevant.  In the case of divide, the exact
quotient could have an infinitely long decimal expansion; for
example, 1 divided by 3.  If the quotient has a nonterminating
decimal expansion and the operation is specified to return an exact
result, an ArithmeticException is thrown.  Otherwise, the
exact result of the division is returned, as done for other
operations.

When the precision setting is not 0, the rules of
BigDecimal arithmetic are broadly compatible with selected
modes of operation of the arithmetic defined in ANSI X3.274-1996
and ANSI X3.274-1996/AM 1-2000 (section 7.4).  Unlike those
standards, BigDecimal includes many rounding modes, which
were mandatory for division in BigDecimal releases prior
to 5.  Any conflicts between these ANSI standards and the
BigDecimal specification are resolved in favor of
BigDecimal.

Since the same numerical value can have different
representations (with different scales), the rules of arithmetic
and rounding must specify both the numerical result and the scale
used in the result's representation.


In general the rounding modes and precision setting determine
how operations return results with a limited number of digits when
the exact result has more digits (perhaps infinitely many in the
case of division) than the number of digits returned.

First, the
total number of digits to return is specified by the
MathContext's precision setting; this determines
the result's precision.  The digit count starts from the
leftmost nonzero digit of the exact result.  The rounding mode
determines how any discarded trailing digits affect the returned
result.

For all arithmetic operators , the operation is carried out as
though an exact intermediate result were first calculated and then
rounded to the number of digits specified by the precision setting
(if necessary), using the selected rounding mode.  If the exact
result is not returned, some digit positions of the exact result
are discarded.  When rounding increases the magnitude of the
returned result, it is possible for a new digit position to be
created by a carry propagating to a leading "9" digit.
For example, rounding the value 999.9 to three digits rounding up
would be numerically equal to one thousand, represented as
100×101.  In such cases, the new "1" is
the leading digit position of the returned result.

Besides a logical exact result, each arithmetic operation has a
preferred scale for representing a result.  The preferred
scale for each operation is listed in the table below.


Preferred Scales for Results of Arithmetic Operations

OperationPreferred Scale of Result
Addmax(addend.scale(), augend.scale())
Subtractmax(minuend.scale(), subtrahend.scale())
Multiplymultiplier.scale()  multiplicand.scale()
Dividedividend.scale() - divisor.scale()


These scales are the ones used by the methods which return exact
arithmetic results; except that an exact divide may have to use a
larger scale since the exact result may have more digits.  For
example, 1/32 is 0.03125.

Before rounding, the scale of the logical exact intermediate
result is the preferred scale for that operation.  If the exact
numerical result cannot be represented in precision
digits, rounding selects the set of digits to return and the scale
of the result is reduced from the scale of the intermediate result
to the least scale which can represent the precision
digits actually returned.  If the exact result can be represented
with at most precision digits, the representation
of the result with the scale closest to the preferred scale is
returned.  In particular, an exactly representable quotient may be
represented in fewer than precision digits by removing
trailing zeros and decreasing the scale.  For example, rounding to
three digits using the floor
rounding mode,

19/100 = 0.19   // integer=19,  scale=2

but

21/110 = 0.190  // integer=190, scale=3

Note that for add, subtract, and multiply, the reduction in
scale will equal the number of digit positions of the exact result
which are discarded. If the rounding causes a carry propagation to
create a new high-order digit position, an additional digit of the
result is discarded than when no new digit position is created.

Other methods may have slightly different rounding semantics.
For example, the result of the pow method using the
specified algorithm can
occasionally differ from the rounded mathematical result by more
than one unit in the last place, one ulp.

Two types of operations are provided for manipulating the scale
of a BigDecimal: scaling/rounding operations and decimal
point motion operations.  Scaling/rounding operations (setScale and round) return a
BigDecimal whose value is approximately (or exactly) equal
to that of the operand, but whose scale or precision is the
specified value; that is, they increase or decrease the precision
of the stored number with minimal effect on its value.  Decimal
point motion operations (movePointLeft and
movePointRight) return a
BigDecimal created from the operand by moving the decimal
point a specified distance in the specified direction.

For the sake of brevity and clarity, pseudo-code is used
throughout the descriptions of BigDecimal methods.  The
pseudo-code expression (i  j) is shorthand for "a
BigDecimal whose value is that of the BigDecimal
i added to that of the BigDecimal
j." The pseudo-code expression (i == j) is
shorthand for "true if and only if the
BigDecimal i represents the same value as the
BigDecimal j." Other pseudo-code expressions
are interpreted similarly.  Square brackets are used to represent
the particular BigInteger and scale pair defining a
BigDecimal value; for example [19, 2] is the
BigDecimal numerically equal to 0.19 having a scale of 2.

Note: care should be exercised if BigDecimal objects
are used as keys in a SortedMap or
elements in a SortedSet since
BigDecimal's natural ordering is inconsistent
with equals.  See Comparable, SortedMap or SortedSet for more
information.

All methods and constructors for this class throw
NullPointerException when passed a null object
reference for any input parameter.
raw docstring

jdk.math.BigInteger

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification. For example, division by zero throws an ArithmeticException, and division of a negative by a positive yields a negative (or zero) remainder. All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.

Semantics of shift operations extend those of Java's shift operators to allow for negative shift distances. A right-shift with a negative shift distance results in a left shift, and vice-versa. The unsigned right shift operator (>>>) is omitted, as this operation makes little sense in combination with the "infinite word size" abstraction provided by this class.

Semantics of bitwise logical operations exactly mimic those of Java's bitwise integer operators. The binary operators (and, or, xor) implicitly perform sign extension on the shorter of the two operands prior to performing the operation.

Comparison operations perform signed integer comparisons, analogous to those performed by Java's relational and equality operators.

Modular arithmetic operations are provided to compute residues, perform exponentiation, and compute multiplicative inverses. These methods always return a non-negative result, between 0 and (modulus - 1), inclusive.

Bit operations operate on a single bit of the two's-complement representation of their operand. If necessary, the operand is sign- extended so that it contains the designated bit. None of the single-bit operations can produce a BigInteger with a different sign from the BigInteger being operated on, as they affect only a single bit, and the "infinite word size" abstraction provided by this class ensures that there are infinitely many "virtual sign bits" preceding each BigInteger.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of BigInteger methods. The pseudo-code expression (i j) is shorthand for "a BigInteger whose value is that of the BigInteger i plus that of the BigInteger j." The pseudo-code expression (i == j) is shorthand for "true if and only if the BigInteger i represents the same value as the BigInteger j." Other pseudo-code expressions are interpreted similarly.

All methods and constructors in this class throw NullPointerException when passed a null object reference for any input parameter.

BigInteger must support values in the range -2Integer.MAX_VALUE (exclusive) to 2Integer.MAX_VALUE (exclusive) and may support values outside of that range.

The range of probable prime values is limited and may be less than the full supported positive range of BigInteger. The range must be at least 1 to 2500000000.

Immutable arbitrary-precision integers.  All operations behave as if
BigIntegers were represented in two's-complement notation (like Java's
primitive integer types).  BigInteger provides analogues to all of Java's
primitive integer operators, and all relevant methods from java.lang.Math.
Additionally, BigInteger provides operations for modular arithmetic, GCD
calculation, primality testing, prime generation, bit manipulation,
and a few other miscellaneous operations.

Semantics of arithmetic operations exactly mimic those of Java's integer
arithmetic operators, as defined in The Java Language Specification.
For example, division by zero throws an ArithmeticException, and
division of a negative by a positive yields a negative (or zero) remainder.
All of the details in the Spec concerning overflow are ignored, as
BigIntegers are made as large as necessary to accommodate the results of an
operation.

Semantics of shift operations extend those of Java's shift operators
to allow for negative shift distances.  A right-shift with a negative
shift distance results in a left shift, and vice-versa.  The unsigned
right shift operator (>>>) is omitted, as this operation makes
little sense in combination with the "infinite word size" abstraction
provided by this class.

Semantics of bitwise logical operations exactly mimic those of Java's
bitwise integer operators.  The binary operators (and,
or, xor) implicitly perform sign extension on the shorter
of the two operands prior to performing the operation.

Comparison operations perform signed integer comparisons, analogous to
those performed by Java's relational and equality operators.

Modular arithmetic operations are provided to compute residues, perform
exponentiation, and compute multiplicative inverses.  These methods always
return a non-negative result, between 0 and (modulus - 1),
inclusive.

Bit operations operate on a single bit of the two's-complement
representation of their operand.  If necessary, the operand is sign-
extended so that it contains the designated bit.  None of the single-bit
operations can produce a BigInteger with a different sign from the
BigInteger being operated on, as they affect only a single bit, and the
"infinite word size" abstraction provided by this class ensures that there
are infinitely many "virtual sign bits" preceding each BigInteger.

For the sake of brevity and clarity, pseudo-code is used throughout the
descriptions of BigInteger methods.  The pseudo-code expression
(i  j) is shorthand for "a BigInteger whose value is
that of the BigInteger i plus that of the BigInteger j."
The pseudo-code expression (i == j) is shorthand for
"true if and only if the BigInteger i represents the same
value as the BigInteger j."  Other pseudo-code expressions are
interpreted similarly.

All methods and constructors in this class throw
NullPointerException when passed
a null object reference for any input parameter.

BigInteger must support values in the range
-2Integer.MAX_VALUE (exclusive) to
2Integer.MAX_VALUE (exclusive)
and may support values outside of that range.

The range of probable prime values is limited and may be less than
the full supported positive range of BigInteger.
The range must be at least 1 to 2500000000.
raw docstring

jdk.math.core

No vars found in this namespace.

jdk.math.MathContext

Immutable objects which encapsulate the context settings which describe certain rules for numerical operators, such as those implemented by the BigDecimal class.

The base-independent settings are:

precision: the number of digits to be used for an operation; results are rounded to this precision

roundingMode: a RoundingMode object which specifies the algorithm to be used for rounding.

Immutable objects which encapsulate the context settings which
describe certain rules for numerical operators, such as those
implemented by the BigDecimal class.

The base-independent settings are:

precision:
the number of digits to be used for an operation; results are
rounded to this precision

roundingMode:
a RoundingMode object which specifies the algorithm to be
used for rounding.
raw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close