forked from xsun2001/Applied-Energistics-2-Unofficial
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
376 additions
and
72 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package appeng.util.calculators; | ||
|
||
public class ArithHelper { | ||
|
||
private static final int DEF_DIV_SCALE = 16; | ||
|
||
private ArithHelper() { | ||
} | ||
|
||
/** | ||
* addition | ||
* | ||
* @param v1 p1 | ||
* @param v2 p2 | ||
* @return sum | ||
*/ | ||
|
||
public static double add(double v1, double v2) { | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1)); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2)); | ||
return b1.add(b2).doubleValue(); | ||
} | ||
|
||
public static double add(String v1, String v2) { | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(v1); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(v2); | ||
return b1.add(b2).doubleValue(); | ||
} | ||
|
||
/** | ||
* subtraction | ||
* | ||
* @param v1 p1 | ||
* @param v2 p2 | ||
* @return sub | ||
*/ | ||
|
||
public static double sub(double v1, double v2) { | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1)); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2)); | ||
return b1.subtract(b2).doubleValue(); | ||
} | ||
|
||
public static double sub(String v1, String v2) { | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(v1); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(v2); | ||
return b1.subtract(b2).doubleValue(); | ||
} | ||
|
||
/** | ||
* multiplication | ||
* | ||
* @param v1 | ||
* p1 | ||
* @param v2 | ||
* p2 | ||
* @return mul | ||
*/ | ||
|
||
public static double mul(double v1, double v2) { | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1)); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2)); | ||
return b1.multiply(b2).doubleValue(); | ||
} | ||
|
||
public static double mul(String v1, String v2) { | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(v1); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(v2); | ||
return b1.multiply(b2).doubleValue(); | ||
} | ||
|
||
/** | ||
* division. e = 10^-10 | ||
* | ||
* @param v1 | ||
* p1 | ||
* @param v2 | ||
* p2 | ||
* @return div | ||
*/ | ||
|
||
public static double div(double v1, double v2) { | ||
return div(v1, v2, DEF_DIV_SCALE); | ||
} | ||
|
||
public static double div(String v1, String v2) { | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(v1); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(v2); | ||
return b1.divide(b2, DEF_DIV_SCALE, java.math.BigDecimal.ROUND_HALF_UP).doubleValue(); | ||
} | ||
|
||
/** | ||
* division. e = 10^-scale | ||
* | ||
* @param v1 p1 | ||
* @param v2 p2 | ||
* @param scale scale | ||
* @return div | ||
*/ | ||
|
||
public static double div(double v1, double v2, int scale) { | ||
if (scale < 0) { | ||
throw new IllegalArgumentException("The scale must be a positive integer or zero"); | ||
} | ||
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1)); | ||
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2)); | ||
return b1.divide(b2, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue(); | ||
} | ||
|
||
/** | ||
* rounding | ||
* | ||
* @param v p | ||
* @param scale scale | ||
* @return result | ||
*/ | ||
|
||
public static double round(double v, int scale) { | ||
if (scale < 0) { | ||
throw new IllegalArgumentException("The scale must be a positive integer or zero"); | ||
} | ||
java.math.BigDecimal b = new java.math.BigDecimal(Double.toString(v)); | ||
java.math.BigDecimal one = new java.math.BigDecimal("1"); | ||
return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue(); | ||
} | ||
|
||
public static double round(String v, int scale) { | ||
if (scale < 0) { | ||
throw new IllegalArgumentException("The scale must be a positive integer or zero"); | ||
} | ||
java.math.BigDecimal b = new java.math.BigDecimal(v); | ||
java.math.BigDecimal one = new java.math.BigDecimal("1"); | ||
return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue(); | ||
} | ||
|
||
} |
Oops, something went wrong.