forked from preda/arithmetic
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use BigDecimal for subtraction to resolve precision issues with double
- Loading branch information
Bryan Emmanuel
committed
Nov 30, 2014
1 parent
7327763
commit 5fb74ed
Showing
4 changed files
with
32 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters