use of java.math.MathContext in project robovm by robovm.
the class BigDecimalTest method testRound.
public void testRound() {
BigDecimal bigDecimal = new BigDecimal("0.999999999999999");
BigDecimal rounded = bigDecimal.round(new MathContext(2, RoundingMode.FLOOR));
assertEquals("0.99", rounded.toString());
}
use of java.math.MathContext in project robovm by robovm.
the class OldBigDecimalArithmeticTest method testAddMathContextNonTrivial.
public void testAddMathContextNonTrivial() {
MathContext mc;
BigDecimal a, b, res;
mc = new MathContext(17, RoundingMode.FLOOR);
a = new BigDecimal("123456789012345.678");
b = new BigDecimal("100000000000000.009");
assertEquals("incorrect value", "123456789012345.67", a.round(mc).toString());
assertEquals("incorrect value", "100000000000000.00", b.round(mc).toString());
assertEquals("incorrect value", "223456789012345.67", a.round(mc).add(b.round(mc)).toString());
res = a.add(b, mc);
assertEquals("incorrect value", "223456789012345.68", res.toString());
mc = new MathContext(33, RoundingMode.UNNECESSARY);
a = new BigDecimal("1234567890123456789012345678.9012395");
b = new BigDecimal("1000000000000000090000000000.0000005");
res = a.add(b, mc);
assertEquals("Incorrect value!", "2234567890123456879012345678.90124", res.toString());
assertEquals("Incorrect scale!", 5, res.scale());
assertEquals("Incorrect precision!", 33, res.precision());
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigComplexMath method exp.
/**
* Calculates the natural exponent of {@link BigComplex} x (e<sup>x</sup>) in the complex domain.
*
* <p>See: <a href="https://en.wikipedia.org/wiki/Exponential_function#Complex_plane">Wikipedia: Exponent (Complex plane)</a></p>
*
* @param x the {@link BigComplex} to calculate the exponent for
* @param mathContext the {@link MathContext} used for the result
* @return the calculated exponent {@link BigComplex} with the precision specified in the <code>mathContext</code>
*/
public static BigComplex exp(BigComplex x, MathContext mathContext) {
MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
BigDecimal expRe = BigDecimalMath.exp(x.re, mc);
return BigComplex.valueOf(expRe.multiply(BigDecimalMath.cos(x.im, mc), mc).round(mathContext), expRe.multiply(BigDecimalMath.sin(x.im, mc), mc)).round(mathContext);
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigComplexMath method toRangePi.
// TODO add root() for the k'th root - https://math.stackexchange.com/questions/322481/principal-nth-root-of-a-complex-number
private static BigDecimal toRangePi(BigDecimal angle, MathContext mathContext) {
MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
BigDecimal pi = BigDecimalMath.pi(mc);
if (angle.compareTo(pi.negate()) < 0) {
return angle.add(pi, mc).add(pi, mc).round(mathContext);
}
if (angle.compareTo(pi) > 0) {
return angle.subtract(pi, mc).subtract(pi, mc).round(mathContext);
}
return angle;
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMath method piChudnovski.
private static BigDecimal piChudnovski(MathContext mathContext) {
MathContext mc = new MathContext(mathContext.getPrecision() + 10, mathContext.getRoundingMode());
final BigDecimal value24 = BigDecimal.valueOf(24);
final BigDecimal value640320 = BigDecimal.valueOf(640320);
final BigDecimal value13591409 = BigDecimal.valueOf(13591409);
final BigDecimal value545140134 = BigDecimal.valueOf(545140134);
final BigDecimal valueDivisor = value640320.pow(3).divide(value24, mc);
BigDecimal sumA = BigDecimal.ONE;
BigDecimal sumB = BigDecimal.ZERO;
BigDecimal a = BigDecimal.ONE;
// -(6*k - 5)
long dividendTerm1 = 5;
// 2*k - 1
long dividendTerm2 = -1;
// 6*k - 1
long dividendTerm3 = -1;
BigDecimal kPower3 = BigDecimal.ZERO;
long iterationCount = (mc.getPrecision() + 13) / 14;
for (long k = 1; k <= iterationCount; k++) {
BigDecimal valueK = BigDecimal.valueOf(k);
dividendTerm1 += -6;
dividendTerm2 += 2;
dividendTerm3 += 6;
BigDecimal dividend = BigDecimal.valueOf(dividendTerm1).multiply(BigDecimal.valueOf(dividendTerm2)).multiply(BigDecimal.valueOf(dividendTerm3));
kPower3 = valueK.pow(3);
BigDecimal divisor = kPower3.multiply(valueDivisor, mc);
a = a.multiply(dividend).divide(divisor, mc);
BigDecimal b = valueK.multiply(a, mc);
sumA = sumA.add(a);
sumB = sumB.add(b);
}
final BigDecimal value426880 = BigDecimal.valueOf(426880);
final BigDecimal value10005 = BigDecimal.valueOf(10005);
final BigDecimal factor = value426880.multiply(sqrt(value10005, mc));
BigDecimal pi = factor.divide(value13591409.multiply(sumA, mc).add(value545140134.multiply(sumB, mc)), mc);
return pi.round(mathContext);
}
Aggregations