Skip to content

Commit

Permalink
Adding full DECIMAL128 precision to Calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
svanteschubert committed Nov 14, 2020
1 parent 7d9bb61 commit 68c538a
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/main/java/net/sf/saxon/expr/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;

/**
Expand Down Expand Up @@ -817,7 +818,7 @@ public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws
return ((IntegerValue) a).times((IntegerValue) b);
} else {
return new BigDecimalValue(
((NumericValue) a).getDecimalValue().multiply(((NumericValue) b).getDecimalValue()));
((NumericValue) a).getDecimalValue().multiply(((NumericValue) b).getDecimalValue(), MathContext.DECIMAL128));
}
}

Expand Down Expand Up @@ -846,12 +847,9 @@ public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
public static BigDecimalValue decimalDivide(NumericValue a, NumericValue b) throws XPathException {
final BigDecimal A = a.getDecimalValue();
final BigDecimal B = b.getDecimalValue();
// int scale = Math.max(DecimalValue.DIVIDE_PRECISION,
// Math.max(A.scale(), B.scale()));
int scale = Math.max(BigDecimalValue.DIVIDE_PRECISION, A.scale() - B.scale() + BigDecimalValue.DIVIDE_PRECISION);

try {
BigDecimal result = A.divide(B, scale, RoundingMode.HALF_DOWN);
BigDecimal result = A.divide(B, MathContext.DECIMAL128);
return new BigDecimalValue(result);
} catch (ArithmeticException err) {
if (b.compareTo(0) == 0) {
Expand Down

0 comments on commit 68c538a

Please sign in to comment.