Skip to content

Commit

Permalink
Use BigDecimal for subtraction to resolve precision issues with double
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Emmanuel committed Nov 30, 2014
1 parent 7327763 commit 5fb74ed
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/org/javia/arity/BigDecimalUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.javia.arity;

import java.math.BigDecimal;

/**
* Utilities for Math using BigDecimal
*/
public class BigDecimalUtils {

private BigDecimalUtils() {}

private static boolean isSupported(double value) {
return !Double.isInfinite(value) && !Double.isNaN(value);
}

private static BigDecimal getBigDecimalFrom(double value) {
return new BigDecimal(String.valueOf(value));
}

public static double substract(double a, double b) {
if (isSupported(a) && isSupported(b)) {
return getBigDecimalFrom(a).subtract(getBigDecimalFrom(b)).doubleValue();
}

return a - b;
}
}
2 changes: 1 addition & 1 deletion src/org/javia/arity/CompiledFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ int execWithoutCheck(EvalContext context, int p) throws IsComplexException {

case VM.SUB: {
final double a = s[--p];
double res = a - (percentPC == pc-1 ? s[p] * s[p+1] : s[p+1]);
double res = BigDecimalUtils.substract(a, (percentPC == pc-1 ? s[p] * s[p+1] : s[p+1]));
if (Math.abs(res) < Math.ulp(a) * 1024) {
// hack for "1.1-1-.1"
res = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/org/javia/arity/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public final Complex add(Complex o) {
*/
public final Complex sub(Complex o) {
final double ulp = Math.ulp(re);
re -= o.re;
im -= o.im;
re = BigDecimalUtils.substract(re, o.re);
im = BigDecimalUtils.substract(im, o.im);
// hack for "1.1-1-.1"
if (Math.abs(re) < ulp * 1024) {
re = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/org/javia/arity/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class TestEval {
new EvalCase("imag(8.123)", 0),
new EvalCase("im(sqrt(-1))", 1),
new EvalCase("im(nan)", Double.NaN),

new EvalCase("56.3 - 55.7", .6),
};

private static final double ONE_SQRT2 = 0.7071067811865475; // sin(pi/4)
Expand Down

0 comments on commit 5fb74ed

Please sign in to comment.