Skip to content

Commit

Permalink
#261 java BigDecimal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszgromada committed May 29, 2022
1 parent 4bf6be3 commit 853b5d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ public static double div(double a, double b) {
if (Double.isInfinite(b)) return a / b;
BigDecimal da = BigDecimal.valueOf(a);
BigDecimal db = BigDecimal.valueOf(b);
return da.divide(db, MathContext.DECIMAL128).doubleValue();
try {
return da.divide(db, MathContext.DECIMAL128).doubleValue();
} catch (Throwable e) {
return a / b;
}
}
/**
* Bell Numbers
Expand Down Expand Up @@ -1064,8 +1068,12 @@ private static double powInt(double a, int n) {
if (n == 1) return a;
if (mXparser.checkIfCanonicalRounding()) {
BigDecimal da = BigDecimal.valueOf(a);
if (n >= 0) return da.pow(n).doubleValue();
else return BigDecimal.ONE.divide(da, MathContext.DECIMAL128).pow(-n).doubleValue();
try {
if (n >= 0) return da.pow(n).doubleValue();
else return BigDecimal.ONE.divide(da, MathContext.DECIMAL128).pow(-n).doubleValue();
} catch (Throwable e) {
return Math.pow(a, n);
}
} else {
return Math.pow(a, n);
}
Expand Down Expand Up @@ -1859,9 +1867,13 @@ public static double round(double value, int places) {
if (Double.isNaN(value)) return Double.NaN;
if (Double.isInfinite(value)) return value;
if (places < 0) return Double.NaN;
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
try {
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
} catch (Throwable e) {
return roundHalfUp(value, places);
}
}
/**
* Double half up rounding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)ProbabilityDistributions.java 5.0.4 2022-05-22
* @(#)ProbabilityDistributions.java 5.0.5 2022-05-29
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -182,12 +182,9 @@

import java.util.Random;

import org.mariuszgromada.math.mxparser.Argument;
import org.mariuszgromada.math.mxparser.Constant;
import org.mariuszgromada.math.mxparser.Expression;
import org.mariuszgromada.math.mxparser.mXparser;
import org.mariuszgromada.math.mxparser.parsertokens.BinaryRelation;

/**
* ProbabilityDistributions - random number generators, PDF - Probability Distribution Functions,
* CDF - Cumulative Distribution Functions, QNT - Quantile Functions (Inverse Cumulative Distribution
Expand All @@ -203,7 +200,7 @@
* <a href="https://play.google.com/store/apps/details?id=org.mathparser.scalar.pro" target="_blank">Scalar Pro</a><br>
* <a href="https://mathspace.pl" target="_blank">MathSpace.pl</a><br>
*
* @version 5.0.0
* @version 5.0.5
*/
public final class ProbabilityDistributions {
/**
Expand Down

0 comments on commit 853b5d0

Please sign in to comment.