Java Code Examples for java.math.MathContext#getRoundingMode()
The following examples show how to use
java.math.MathContext#getRoundingMode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 6 votes |
public static BigDecimal logAreaHyperbolicTangent(BigDecimal x, MathContext mathContext) { // http://en.wikipedia.org/wiki/Logarithm#Calculation MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1); BigDecimal magic = x.subtract(ONE, mc).divide(x.add(ONE), mc); BigDecimal result = ZERO; BigDecimal step; int i = 0; do { int doubleIndexPlusOne = i * 2 + 1; step = BigDecimalMath.pow(magic, doubleIndexPlusOne, mc).divide(valueOf(doubleIndexPlusOne), mc); result = result.add(step, mc); i++; } while (step.abs().compareTo(acceptableError) > 0); result = result.multiply(TWO, mc); return result.round(mathContext); }
Example 2
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 6 votes |
public static BigDecimal sqrtUsingNewton(BigDecimal x, MathContext mathContext) { switch (x.signum()) { case 0: return ZERO; case -1: throw new ArithmeticException("Illegal sqrt(x) for x < 0: x = " + x); } MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1); BigDecimal result = BigDecimal.valueOf(Math.sqrt(x.doubleValue())); BigDecimal last; do { last = result; result = x.divide(result, mc).add(last, mc).divide(TWO, mc); } while (result.subtract(last).abs().compareTo(acceptableError) > 0); return result.round(mathContext); }
Example 3
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 6 votes |
public static BigDecimal asin(BigDecimal x, MathContext mathContext) { if (x.compareTo(ONE) > 0) { throw new ArithmeticException("Illegal asin(x) for x > 1: x = " + x); } if (x.compareTo(MINUS_ONE) < 0) { throw new ArithmeticException("Illegal asin(x) for x < -1: x = " + x); } if (x.signum() == -1) { return asin(x.negate(), mathContext).negate(); } MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode()); if (x.compareTo(BigDecimal.valueOf(0.707107)) >= 0) { BigDecimal xTransformed = BigDecimalMath.sqrt(ONE.subtract(x.multiply(x, mc), mc), mc); return acos(xTransformed, mathContext); } BigDecimal result = AsinCalculator.INSTANCE.calculate(x, mc); return result.round(mathContext); }
Example 4
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 6 votes |
public static BigDecimal rootFixPrecision(BigDecimal n, BigDecimal x, MathContext mathContext) { switch (x.signum()) { case 0: return ZERO; case -1: throw new ArithmeticException("Illegal root(x) for x < 0: x = " + x); } MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1); BigDecimal factor = ONE.divide(n, mc); BigDecimal nMinus1 = n.subtract(ONE); BigDecimal result = x.divide(TWO, mc); BigDecimal step; do { step = factor.multiply(x.divide(BigDecimalMath.pow(result, nMinus1, mc), mc).subtract(result, mc), mc); result = result.add(step, mc); } while (step.abs().compareTo(acceptableError) > 0); return result.round(mathContext); }
Example 5
Source File: BigDecimalMath.java From big-math with MIT License | 6 votes |
/** * Calculates the arc sine (inverted sine) of {@link BigDecimal} x. * * <p>See: <a href="http://en.wikipedia.org/wiki/Arcsine">Wikipedia: Arcsine</a></p> * * @param x the {@link BigDecimal} to calculate the arc sine for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc sine {@link BigDecimal} with the precision specified in the <code>mathContext</code> * @throws ArithmeticException if x > 1 or x < -1 * @throws UnsupportedOperationException if the {@link MathContext} has unlimited precision */ public static BigDecimal asin(BigDecimal x, MathContext mathContext) { checkMathContext(mathContext); if (x.compareTo(ONE) > 0) { throw new ArithmeticException("Illegal asin(x) for x > 1: x = " + x); } if (x.compareTo(MINUS_ONE) < 0) { throw new ArithmeticException("Illegal asin(x) for x < -1: x = " + x); } if (x.signum() == -1) { return asin(x.negate(), mathContext).negate(); } MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode()); if (x.compareTo(BigDecimal.valueOf(0.707107)) >= 0) { BigDecimal xTransformed = sqrt(ONE.subtract(x.multiply(x)), mc); return acos(xTransformed, mathContext); } BigDecimal result = AsinCalculator.INSTANCE.calculate(x, mc); return round(result, mathContext); }
Example 6
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 6 votes |
public static BigDecimal sqrtUsingHalleyPrint(BigDecimal x, MathContext mathContext) { switch (x.signum()) { case 0: return ZERO; case -1: throw new ArithmeticException("Illegal sqrt(x) for x < 0: x = " + x); } MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1); BigDecimal threeX = x.multiply(THREE); BigDecimal result = BigDecimal.valueOf(Math.sqrt(x.doubleValue())); BigDecimal last; do { last = result; BigDecimal resultSquare = result.multiply(result); BigDecimal divisor = resultSquare.multiply(THREE).add(x); result = resultSquare.add(threeX).multiply(result).divide(divisor, mc); System.out.printf("%5d, ", countSameCharacters(last.toPlainString(), result.toPlainString())); } while (result.subtract(last).abs().compareTo(acceptableError) > 0); return result.round(mathContext); }
Example 7
Source File: BigDecimalMath.java From big-math with MIT License | 5 votes |
/** * Calculates the factorial of the specified {@link BigDecimal}. * * <p>This implementation uses * <a href="https://en.wikipedia.org/wiki/Spouge%27s_approximation">Spouge's approximation</a> * to calculate the factorial for non-integer values.</p> * * <p>This involves calculating a series of constants that depend on the desired precision. * Since this constant calculation is quite expensive (especially for higher precisions), * the constants for a specific precision will be cached * and subsequent calls to this method with the same precision will be much faster.</p> * * <p>It is therefore recommended to do one call to this method with the standard precision of your application during the startup phase * and to avoid calling it with many different precisions.</p> * * <p>See: <a href="https://en.wikipedia.org/wiki/Factorial#Extension_of_factorial_to_non-integer_values_of_argument">Wikipedia: Factorial - Extension of factorial to non-integer values of argument</a></p> * * @param x the {@link BigDecimal} * @param mathContext the {@link MathContext} used for the result * @return the factorial {@link BigDecimal} * @throws ArithmeticException if x is a negative integer value (-1, -2, -3, ...) * @throws UnsupportedOperationException if x is a non-integer value and the {@link MathContext} has unlimited precision * @see #factorial(int) * @see #gamma(BigDecimal, MathContext) */ public static BigDecimal factorial(BigDecimal x, MathContext mathContext) { if (isIntValue(x)) { return round(factorial(x.intValueExact()), mathContext); } // https://en.wikipedia.org/wiki/Spouge%27s_approximation checkMathContext(mathContext); MathContext mc = new MathContext(mathContext.getPrecision() << 1, mathContext.getRoundingMode()); int a = mathContext.getPrecision() * 13 / 10; List<BigDecimal> constants = getSpougeFactorialConstants(a); BigDecimal bigA = BigDecimal.valueOf(a); boolean negative = false; BigDecimal factor = constants.get(0); for (int k = 1; k < a; k++) { BigDecimal bigK = BigDecimal.valueOf(k); factor = factor.add(constants.get(k).divide(x.add(bigK), mc)); negative = !negative; } BigDecimal result = pow(x.add(bigA), x.add(BigDecimal.valueOf(0.5)), mc); result = result.multiply(exp(x.negate().subtract(bigA), mc)); result = result.multiply(factor); return round(result, mathContext); }
Example 8
Source File: BigDecimalMath.java From big-math with MIT License | 5 votes |
/** * Calculates {@link BigDecimal} x to the power of <code>long</code> y (x<sup>y</sup>). * * <p>The implementation tries to minimize the number of multiplications of {@link BigDecimal x} (using squares whenever possible).</p> * * <p>See: <a href="https://en.wikipedia.org/wiki/Exponentiation#Efficient_computation_with_integer_exponents">Wikipedia: Exponentiation - efficient computation</a></p> * * @param x the {@link BigDecimal} value to take to the power * @param y the <code>long</code> value to serve as exponent * @param mathContext the {@link MathContext} used for the result * @return the calculated x to the power of y with the precision specified in the <code>mathContext</code> * @throws ArithmeticException if y is negative and the result is inexact but the * rounding mode is {@code UNNECESSARY} or * {@code mc.precision == 0} and the quotient has a * non-terminating decimal expansion. * @throws ArithmeticException if the rounding mode is * {@code UNNECESSARY} and the * {@code BigDecimal} operation would require rounding. */ public static BigDecimal pow(BigDecimal x, long y, MathContext mathContext) { MathContext mc = mathContext.getPrecision() == 0 ? mathContext : new MathContext(mathContext.getPrecision() + 10, mathContext.getRoundingMode()); // TODO optimize y=0, y=1, y=10^k, y=-1, y=-10^k if (y < 0) { BigDecimal value = reciprocal(pow(x, -y, mc), mc); return round(value, mathContext); } BigDecimal result = ONE; while (y > 0) { if ((y & 1) == 1) { // odd exponent -> multiply result with x result = result.multiply(x, mc); y -= 1; } if (y > 0) { // even exponent -> square x x = x.multiply(x, mc); } y >>= 1; } return round(result, mathContext); }
Example 9
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 5 votes |
public static BigDecimal logUsingSqrt(BigDecimal x, MathContext mathContext, BiFunction<BigDecimal, MathContext, BigDecimal> logFunction) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal sqrtX = BigDecimalMath.sqrt(x, mc); BigDecimal result = logFunction.apply(sqrtX, mc).multiply(TWO, mc); return result.round(mathContext); }
Example 10
Source File: BigDecimalMath.java From big-math with MIT License | 5 votes |
/** * Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) of {@link BigDecimal} x. * * <p>See: <a href="https://en.wikipedia.org/wiki/Hyperbolic_function">Wikipedia: Hyperbolic function</a></p> * * @param x the {@link BigDecimal} to calculate the arc hyperbolic tangens for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc hyperbolic tangens {@link BigDecimal} with the precision specified in the <code>mathContext</code> * @throws UnsupportedOperationException if the {@link MathContext} has unlimited precision */ public static BigDecimal atanh(BigDecimal x, MathContext mathContext) { if (x.compareTo(BigDecimal.ONE) >= 0) { throw new ArithmeticException("Illegal atanh(x) for x >= 1: x = " + x); } if (x.compareTo(MINUS_ONE) <= 0) { throw new ArithmeticException("Illegal atanh(x) for x <= -1: x = " + x); } checkMathContext(mathContext); MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode()); BigDecimal result = log(ONE.add(x).divide(ONE.subtract(x), mc), mc).multiply(ONE_HALF); return round(result, mathContext); }
Example 11
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 5 votes |
public static BigDecimal acosUsingNewton(BigDecimal x, MathContext mathContext) { if (x.compareTo(ONE) > 0) { throw new ArithmeticException("Illegal acos(x) for x > 1: x = " + x); } if (x.compareTo(MINUS_ONE) < 0) { throw new ArithmeticException("Illegal acos(x) for x < -1: x = " + x); } MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode()); BigDecimal result = BigDecimalMath.pi(mc).divide(TWO, mc).subtract(asinUsingNewton(x, mc), mc); return result.round(mathContext); }
Example 12
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 5 votes |
public static BigDecimal sqrtUsingNewtonAdaptivePrecisionPrint(BigDecimal x, MathContext mathContext) { switch (x.signum()) { case 0: return ZERO; case -1: throw new ArithmeticException("Illegal sqrt(x) for x < 0: x = " + x); } int maxPrecision = mathContext.getPrecision() + 4; BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1); BigDecimal result = BigDecimal.valueOf(Math.sqrt(x.doubleValue())); int adaptivePrecision = 17; BigDecimal last; do { last = result; adaptivePrecision = adaptivePrecision * 2; if (adaptivePrecision > maxPrecision) { adaptivePrecision = maxPrecision; } MathContext mc = new MathContext(adaptivePrecision, mathContext.getRoundingMode()); result = x.divide(result, mc).add(last).divide(TWO, mc); System.out.printf("%5d, ", countSameCharacters(last.toPlainString(), result.toPlainString())); } while (adaptivePrecision < maxPrecision || result.subtract(last).abs().compareTo(acceptableError) > 0); return result.round(mathContext); }
Example 13
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 5 votes |
public static BigDecimal logUsingRoot(BigDecimal x, MathContext mathContext, BiFunction<BigDecimal, MathContext, BigDecimal> logFunction) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); // log(x) = log(root(r, x)^r) = r * log(root(r, x)) BigDecimal r = valueOf(Math.max(2, (int) (Math.log(x.doubleValue()) * 5))); BigDecimal result = BigDecimalMath.root(x, r, mc); result = logFunction.apply(result, mc).multiply(r, mc); return result.round(mathContext); }
Example 14
Source File: BigDecimalMath.java From big-math with MIT License | 3 votes |
/** * Calculates the hyperbolic cosine of {@link BigDecimal} x. * * <p>See: <a href="https://en.wikipedia.org/wiki/Hyperbolic_function">Wikipedia: Hyperbolic function</a></p> * * @param x the {@link BigDecimal} to calculate the hyperbolic cosine for * @param mathContext the {@link MathContext} used for the result * @return the calculated hyperbolic cosine {@link BigDecimal} with the precision specified in the <code>mathContext</code> * @throws UnsupportedOperationException if the {@link MathContext} has unlimited precision */ public static BigDecimal cosh(BigDecimal x, MathContext mathContext) { checkMathContext(mathContext); MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal result = CoshCalculator.INSTANCE.calculate(x, mc); return round(result, mathContext); }
Example 15
Source File: BigDecimalMathExperimental.java From big-math with MIT License | 3 votes |
public static BigDecimal exp(BigDecimal x, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal result = ExpCalculator.INSTANCE.calculate(x, mc); return result.round(mathContext); }
Example 16
Source File: BigDecimalMath.java From big-math with MIT License | 3 votes |
/** * Calculates the arc hyperbolic sine (inverse hyperbolic sine) of {@link BigDecimal} x. * * <p>See: <a href="https://en.wikipedia.org/wiki/Hyperbolic_function">Wikipedia: Hyperbolic function</a></p> * * @param x the {@link BigDecimal} to calculate the arc hyperbolic sine for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc hyperbolic sine {@link BigDecimal} with the precision specified in the <code>mathContext</code> * @throws UnsupportedOperationException if the {@link MathContext} has unlimited precision */ public static BigDecimal asinh(BigDecimal x, MathContext mathContext) { checkMathContext(mathContext); MathContext mc = new MathContext(mathContext.getPrecision() + 10, mathContext.getRoundingMode()); BigDecimal result = log(x.add(sqrt(x.multiply(x, mc).add(ONE, mc), mc)), mc); return round(result, mathContext); }
Example 17
Source File: BigDecimalMath.java From big-math with MIT License | 3 votes |
/** * Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) of {@link BigDecimal} x. * * <p>See: <a href="https://en.wikipedia.org/wiki/Hyperbolic_function">Wikipedia: Hyperbolic function</a></p> * * @param x the {@link BigDecimal} to calculate the arc hyperbolic cotangens for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc hyperbolic cotangens {@link BigDecimal} with the precision specified in the <code>mathContext</code> * @throws UnsupportedOperationException if the {@link MathContext} has unlimited precision */ public static BigDecimal acoth(BigDecimal x, MathContext mathContext) { checkMathContext(mathContext); MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode()); BigDecimal result = log(x.add(ONE).divide(x.subtract(ONE), mc), mc).multiply(ONE_HALF); return round(result, mathContext); }
Example 18
Source File: BigDecimalMath.java From big-math with MIT License | 3 votes |
/** * Calculates the inverse cotangens (arc cotangens) of {@link BigDecimal} x. * * <p>See: <a href="http://en.wikipedia.org/wiki/Arccotangens">Wikipedia: Arccotangens</a></p> * * @param x the {@link BigDecimal} to calculate the arc cotangens for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc cotangens {@link BigDecimal} with the precision specified in the <code>mathContext</code> * @throws UnsupportedOperationException if the {@link MathContext} has unlimited precision */ public static BigDecimal acot(BigDecimal x, MathContext mathContext) { checkMathContext(mathContext); MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal result = pi(mc).divide(TWO, mc).subtract(atan(x, mc)); return round(result, mathContext); }
Example 19
Source File: BigComplexMath.java From big-math with MIT License | 2 votes |
/** * Calculates {@link BigComplex} x to the power of {@link BigComplex} y (x<sup>y</sup>). * * @param x the {@link BigComplex} value to take to the power * @param y the {@link BigComplex} value to serve as exponent * @param mathContext the {@link MathContext} used for the result * @return the calculated x to the power of y with the precision specified in the <code>mathContext</code> */ public static BigComplex pow(BigComplex x, BigComplex y, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return exp(y.multiply(log(x, mc), mc), mc).round(mathContext); }
Example 20
Source File: BigComplexMath.java From big-math with MIT License | 2 votes |
/** * Calculates the arc tangens (inverted tangens) of {@link BigComplex} x in the complex domain. * * <p>See: <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_complex_plane">Wikipedia: Inverse trigonometric functions (Extension to complex plane)</a></p> * * @param x the {@link BigComplex} to calculate the arc tangens for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc tangens {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex atan(BigComplex x, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return log(I.subtract(x, mc).divide(I.add(x, mc), mc), mc).divide(I, mc).divide(TWO, mc).round(mathContext); }