Category: 16. Java.math package

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQVrxWlEfPMH2TPAc3JObfQozIB0zgZKfaHvg&s

  • MathContext 

    Introduction

    The java.math.MathContext class provides immutable objects which encapsulate the context settings and describes 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.

    Class declaration

    Following is the declaration for java.math.MathContext class −

    public final class MathContext
       extends Object
    
      implements Serializable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.math.MathContext class −

    • static MathContext DECIMAL128 − A MathContext object with a precision setting matching the IEEE 754R Decimal128 format, 34 digits, and a rounding mode of HALF_EVEN, the IEEE 754R default.
    • static MathContext DECIMAL32 − A MathContext object with a precision setting matching the IEEE 754R Decimal32 format, 7 digits, and a rounding mode of HALF_EVEN, the IEEE 754R default.
    • static MathContext DECIMAL64 − A MathContext object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of HALF_EVEN, the IEEE 754R default.
    • static MathContext UNLIMITED − A MathContext object whose settings have the values required for unlimited precision arithmetic.

    Class constructors

    Sr.No.Constructor & Description
    1MathContext(int setPrecision)This constructor, constructs a new MathContext with the specified precision and the HALF_UP rounding mode.
    2MathContext(int setPrecision, RoundingMode setRoundingMode)This constructor, constructs a new MathContext with a specified precision and rounding mode.
    3MathContext(String val)This constructor, constructs a new MathContext from a string.

    Class methods

    Sr.No.Method & Description
    1boolean equals(Object x)This method compares this MathContext with the specified Object for equality.
    2int getPrecision()This method returns the precision setting.
    3RoundingMode getRoundingMode()This method returns the roundingMode setting.
    4int hashCode()This method returns the hash code for this MathContext.
    5String toString()This method returns the string representation of this MathContext.
  • BigInteger 

    Introduction

    The java.math.BigInteger class provides operations analogues to all of Java’s primitive integer operators and for all relevant methods from java.lang.Math.

    It also provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. All operations behave as if BigIntegers were represented in two’s-complement notation.

    Semantics of arithmetic operations and bitwise logical operations are similar to those of Java’s integer arithmetic operators and Java’s bitwise integer operators respectively. Semantics of shift operations extend those of Java’s shift operators to allow for negative shift distances.

    Comparison operations perform signed integer comparisons. Modular arithmetic operations are provided to compute residues, perform exponentiation, and compute multiplicative inverses. Bit operations operate on a single bit of the two’s-complement representation of their operand.

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

    Class declaration

    Following is the declaration for java.math.BigInteger class −

    public class BigInteger
       extends Number
    
      implements Comparable<BigInteger>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.math.BigInteger class −

    • static BigInteger ONE − The BigInteger constant one.
    • static BigInteger TEN − The BigInteger constant ten.
    • static BigInteger ZERO − The BigInteger constant zero.

    Class constructors

    Sr.No.Constructor & Description
    1BigInteger(byte[] val)This constructor is used to translate a byte array containing the two’s-complement binary representation of a BigInteger into a BigInteger.
    2BigInteger(int signum, byte[] magnitude)This constructor is used to translate the sign-magnitude representation of a BigInteger into a BigInteger.
    3BigInteger(int bitLength, int certainty, Random rnd)This constructor is used to construct a randomly generated positive BigInteger that is probably prime, with the specified bitLength.
    4BigInteger(int numBits, Random rnd)This constructor is used to construct a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits – 1), inclusive.
    5BigInteger(String val)This constructor is used to translate the decimal String representation of a BigInteger into a BigInteger.
    6BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.

    Class methods

    Sr.No.Method & Description
    1BigInteger abs()This method returns a BigInteger whose value is the absolute value of this BigInteger.
    2BigInteger add(BigInteger val)This method returns a BigInteger whose value is (this + val).
    3BigInteger and(BigInteger val)This method returns a BigInteger whose value is (this & val).
    4BigInteger andNot(BigInteger val)This method returns a BigInteger whose value is (this & ~val).
    5int bitCount()This method returns the number of bits in the two’s complement representation of this BigInteger that differ from its sign bit.
    6int bitLength()This method returns the number of bits in the minimal two’s-complement representation of this BigInteger, excluding a sign bit.
    7BigInteger clearBit(int n)This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.
    8int compareTo(BigInteger val)This method compares this BigInteger with the specified BigInteger.
    9BigInteger divide(BigInteger val)This method returns a BigInteger whose value is (this / val).
    10BigInteger[ ] divideAndRemainder(BigInteger val)This method returns an array of two BigIntegers containing (this / val) followed by (this % val).
    11double doubleValue()This method converts this BigInteger to a double.
    12boolean equals(Object x)This method compares this BigInteger with the specified Object for equality.
    13BigInteger flipBit(int n)This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.
    14float floatValue()This method converts this BigInteger to a float.
    15BigInteger gcd(BigInteger val)This method returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val).
    16int getLowestSetBit()This method returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit).
    17int hashCode()This method returns the hash code for this BigInteger.
    18int intValue()This method converts this BigInteger to an int.
    19boolean isProbablePrime(int certainty)This method returns true if this BigInteger is probably prime, false if it’s definitely composite.
    20long longValue()This method converts this BigInteger to a long.
    21BigInteger max(BigInteger val)This method returns the maximum of this BigInteger and val.
    22BigInteger min(BigInteger val)This method returns the minimum of this BigInteger and val.
    23BigInteger mod(BigInteger m)This method returns a BigInteger whose value is (this mod m).
    24BigInteger modInverse(BigInteger m)This method returns a BigInteger whose value is (this-1 mod m).
    25BigInteger modPow(BigInteger exponent, BigInteger m)This method returns a BigInteger whose value is (thisexponent mod m).
    26BigInteger multiply(BigInteger val)This method returns a BigInteger whose value is (this * val).
    27BigInteger negate()This method returns a BigInteger whose value is (-this).
    28BigInteger nextProbablePrime()This method returns the first integer greater than this BigInteger that is probably prime.
    29BigInteger not()This method returns a BigInteger whose value is (~this).
    30BigInteger or(BigInteger val)This method returns a BigInteger whose value is (this | val).
    31BigInteger pow(int exponent)This method returns a BigInteger whose value is (thisexponent).
    32static BigInteger probablePrime(int bitLength, Random rnd)This method returns a positive BigInteger that is probably prime, with the specified bitLength.
    33BigInteger remainder(BigInteger val)This method returns a BigInteger whose value is (this % val).
    34BigInteger setBit(int n)This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.
    35BigInteger shiftLeft(int n)This method returns a BigInteger whose value is (this << n).
    36BigInteger shiftRight(int n)This method returns a BigInteger whose value is (this >> n).
    37int signum()This method returns the signum function of this BigInteger.
    38BigInteger subtract(BigInteger val)This method returns a BigInteger whose value is (this – val).
    39boolean testBit(int n)This method returns true if and only if the designated bit is set.
    40byte[ ] toByteArray()This method returns a byte array containing the two’s-complement representation of this BigInteger.
    41String toString()This method returns the decimal String representation of this BigInteger.
    42String toString(int radix)This method returns the String representation of this BigInteger in the given radix.
    43static BigInteger valueOf(long val)This method returns a BigInteger whose value is equal to that of the specified long.
    44BigInteger xor(BigInteger val)This method returns a BigInteger whose value is (this ^ val).
  • BigDecimal 

    Introduction

    The java.math.BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.

    The toString() method provides a canonical representation of a BigDecimal. It gives the user complete control over rounding behavior.

    Two types of operations are provided for manipulating the scale of a BigDecimal −

    • scaling/rounding operations
    • decimal point motion operations

    This class and its iterator implement all of the optional methods of the Comparable interfaces.

    Class declaration

    Following is the declaration for java.math.BigDecimal class −

    public class BigDecimal
       extends Number
    
      implements Comparable&lt;BigDecimal&gt;

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.math.BigDecimal class −

    • static BigDecimal ONE − The value 1, with a scale of 0.
    • static int ROUND_CEILING − Rounding mode to round towards positive infinity.
    • static int ROUND_DOWN − Rounding mode to round towards zero.
    • static int ROUND_FLOOR − Rounding mode to round towards negative infinity.
    • static int ROUND_HALF_DOWN − Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
    • static int ROUND_HALF_EVEN − Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
    • static int ROUND_HALF_UP − Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
    • static int ROUND_UNNECESSARY − Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
    • static int ROUND_UP − Rounding mode to round away from zero.
    • static BigDecimal TEN − The value 10, with a scale of 0.
    • static BigDecimal ZERO − The value 0, with a scale of 0.

    Class constructors

    Sr.No.Constructor & Description
    1BigDecimal(BigInteger val)This constructor is used to translates a BigInteger into a BigDecimal.
    2BigDecimal(BigInteger unscaledVal, int scale)This constructor is used to translate a BigInteger unscaled value and an int scale into a BigDecimal.
    3BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)This constructor is used to translate a BigInteger unscaled value and an int scale into a BigDecimal, with rounding according to the context settings.
    4BigDecimal(BigInteger val, MathContext mc)This constructor is used to translate a BigInteger into a BigDecimal rounding according to the context settings.
    5BigDecimal(char[ ] in)This constructor is used to translate a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor.
    6BigDecimal(char[ ] in, int offset, int len)This constructor is used to translate a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified.
    7BigDecimal(char[ ] in, int offset, int len, MathContext mc)This constructor is used to translate a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified and with rounding according to the context settings.
    8BigDecimal(char[ ] in, MathContext mc)This constructor is used to translate a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor and with rounding according to the context settings.
    9BigDecimal(double val)This constructor is used to translates a double into a BigDecimal which is the exact decimal representation of the double’s binary floating-point value.
    10BigDecimal(double val, MathContext mc)This constructor is used to translates a double into a BigDecimal, with rounding according to the context settings.
    11BigDecimal(int val)This constructor is used to translates an int into a BigDecimal.
    12BigDecimal(int val, MathContext mc)This constructor is used to translates an int into a BigDecimal, with rounding according to the context settings.
    13BigDecimal(long val)This constructor is used to translate a long into a BigDecimal.
    14BigDecimal(long val, MathContext mc)This constructor is used to translates a BigInteger into a BigDecimal.
    15BigDecimal(String val)This constructor is used to the string representation of a BigDecimal into a BigDecimal.
    16BigDecimal(String val, MathContext mc)This constructor is used to translates the string representation of a BigDecimal into a BigDecimal, accepting the same strings as the BigDecimal(String) constructor, with rounding according to the context settings.

    Class methods

    Sr.No.Method & Description
    1BigDecimal abs()This method returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().
    2BigDecimal abs(MathContext mc)This method returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.
    3BigDecimal add(BigDecimal augend)This method returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).
    4BigDecimal add(BigDecimal augend, MathContext mc)This method returns a BigDecimal whose value is (this + augend), with rounding according to the context settings.
    5byte byteValueExact()This method converts the BigDecimal to a byte, checking for lost information.
    6int compareTo(BigDecimal val)This method compares the BigDecimal with the specified BigDecimal.
    7BigDecimal divide(BigDecimal divisor)This method returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() – divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.
    8BigDecimal divide(BigDecimal divisor, int roundingMode)This method returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().
    9BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)This method returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.
    10BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)This method returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.
    11BigDecimal divide(BigDecimal divisor, MathContext mc)This method returns a BigDecimal whose value is (this / divisor), with rounding according to the context settings.
    12BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode)This method returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().
    13BigDecimal[ ] divideAndRemainder(BigDecimal divisor)This method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
    14BigDecimal[ ] divideAndRemainder(BigDecimal divisor, MathContext mc)This method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands calculated with rounding according to the context settings.
    15BigDecimal divideToIntegralValue(BigDecimal divisor)This method returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.
    16BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc)This method returns a BigDecimal whose value is the integer part of (this / divisor).
    17double doubleValue()This method converts the BigDecimal to a double.
    18boolean equals(Object x)This method compares the BigDecimal with the specified Object for equality.
    19float floatValue()This method converts the BigDecimal to a float.
    20int hashCode()This method returns the hash code for this BigDecimal.
    21int intValue()This method converts the BigDecimal to an int.
    22int intValueExact()This method converts the BigDecimal to an int, checking for lost information.
    23long longValue()This method converts the BigDecimal to a long.
    24long longValueExact()This method converts the BigDecimal to a long, checking for lost information.
    25BigDecimal max(BigDecimal val)This method returns the maximum of this BigDecimal and val.
    26BigDecimal min(BigDecimal val)This method returns the minimum of this BigDecimal and val.
    27BigDecimal movePointLeft(int n)This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.
    28BigDecimal movePointRight(int n)This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.
    29BigDecimal multiply(BigDecimal multiplicand)This method returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).
    30BigDecimal multiply(BigDecimal multiplicand, MathContext mc)This method returns a BigDecimal whose value is (this × multiplicand), with rounding according to the context settings.
    31BigDecimal negate()This method returns a BigDecimal whose value is (+this), and whose scale is this.scale().
    32BigDecimal negate(MathContext mc)This method returns a BigDecimal whose value is (-this), with rounding according to the context settings.
    33BigDecimal plus()This method returns a BigDecimal whose value is (+this), and whose scale is this.scale().
    34BigDecimal plus(MathContext mc)This method returns a BigDecimal whose value is (+this), with rounding according to the context settings.
    35BigDecimal pow(int n)This method returns a BigDecimal whose value is (thisn), The power is computed exactly, to unlimited precision.
    36BigDecimal pow(int n, MathContext mc)This method returns a BigDecimal whose value is (thisn).
    37int precision()This method returns the precision of this BigDecimal.
    38BigDecimal remainder(BigDecimal divisor)This method converts this BigDecimal to a byte, checking for lost information.
    39BigDecimal remainder(BigDecimal divisor, MathContext mc)This method returns a BigDecimal whose value is (this % divisor), with rounding according to the context settings.
    40BigDecimal round(MathContext mc)This method returns a BigDecimal rounded according to the MathContext settings.
    41int scale()This method returns the scale of this BigDecimal.
    42BigDecimal scaleByPowerOfTen(int n)This method returns a BigDecimal whose numerical value is equal to (this * 10n).
    43BigDecimal setScale(int newScale)This method returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal’s.
    44BigDecimal setScale(int newScale, int roundingMode)This method returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal’s unscaled value by the appropriate power of ten to maintain its overall value.
    45BigDecimal setScale(int newScale, RoundingMode roundingMode)This method returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal’s unscaled value by the appropriate power of ten to maintain its overall value.
    46short shortValueExact()This method converts the BigDecimal to a short, checking for lost information.
    47int signum()This method returns the signum function of this BigDecimal.
    48BigDecimal stripTrailingZeros()This method returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.
    49BigDecimal subtract(BigDecimal subtrahend)This method returns a BigDecimal whose value is (this – subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).
    50BigDecimal subtract(BigDecimal subtrahend, MathContext mc)This method returns a BigDecimal whose value is (this – subtrahend), with rounding according to the context settings.
    51BigInteger toBigInteger()This method converts the BigDecimal to a BigInteger.
    52BigInteger toBigIntegerExact()This method converts the BigDecimal to a BigInteger, checking for lost information.
    53String toEngineeringString()This method returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.
    54String toPlainString()This method returns a string representation of this BigDecimal without an exponent field.
    55String toString()This method returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.
    56BigDecimal ulp()This method returns the size of an ulp, a unit in the last place, of this BigDecimal.
    57BigInteger unscaledValue()This method returns a BigInteger whose value is the unscaled value of this BigDecimal.
    58static BigDecimal valueOf(double val)This method translates a double into a BigDecimal, using the double’s canonical string representation provided by the Double.toString(double) method.
    59static BigDecimal valueOf(long val)This method translates a long value into a BigDecimal with a scale of zero.
    60static BigDecimal valueOf(long unscaledVal, int scale)This method translates a long unscaled value and an int scale into a BigDecimal.