BigDecimal characters / brackets

I am trying to get BigDecimal to use characters (example instead of Decimal.multiply, do Decimal *) because the mass bracket is involved in this problem.

Can you guys tell if there is a way to use characters in BigDecimal without converting it to Double or something else? Or can you convert t to a format like term?

double t = ((1.00 / f1) * ((((4.00 / (ak1 + 1.00)) - (2.00 / (ak1 + 4.00))) - (1.00 / (ak1 + 5.00))) - (1 / (ak1 + 6.00)))); 

To format, for example

 term = ((one.divide(f,mc)).multiply(((((four.divide((ak.add(one)),mc)).subtract((two.divide((ak.add(four)),mc)))).subtract((one.divide((ak.add(five)),mc)))).subtract((one.divide((ak.add(six)),mc)))))); 

I tried to record it many times and spent almost 6 hours trying to figure out where I am going wrong in BigDecimal.

+4
source share
4 answers

Nope. Since there is no operator overload in Java.

To make things easier for yourself, write an equation for parts and identities of bits that are complex or repeated several times. Break your calculations into these parts and even write helper methods to help you calculate the sum. If you write all the calculations in parts, then each part is easier to test.

eg.

 import static java.math.BigDecimal.ONE; public class Sum { private static final TWO = new BigDecimal("2"); private static final FOUR = new BigDecimal("4"); private static final FIVE = new BigDecimal("5"); private static final SIX = new BigDecimal("6"); private BigDecimal f; private BigDecimal ak; private MathContext mc; public Sum(BigDecimal f, BigDecimal ak, MathContext mc) { this.f = f; this.ak = ak; this.mc = mc; } public BigDecimal calculate() { return inverse(f).multiply( firstSubtractRest( xOverYPlusZ(FOUR, ak, ONE), xOverYPlusZ(TWO, ak, FOUR), xOverYPlusZ(ONE, ak, FIVE), xOverYPlusZ(ONE, ak, SIX), )); } private BigDecimal inverse(BigDecimal x) { return ONE.divide(x, mc); } /* returns x / (y + z) */ private BigDecimal xOverYPlusZ(BigDecimal x, BigDecimal y, BigDecimal z) { BigDecimal divisor = y.add(z); return x.divide(divisor, mc); } private BigDecimal firstSubtractRest(BigDecimal... values) { BigDecimal value = values[0]; for (int i = 1; i < values.length; i++) { value = value.subtract(values[i]); } return value; } } 
+3
source

No. Unfortunately, Java does not support operator overloading. You have no choice but to use these methods on BigDecimal .

+4
source

You can write a simple parser to create an expression, and then evaluate it using Rhino or another scripting mechanism available inside Java code.

+2
source

Instead of inverting f1 first, you can simply express it by placing it last.

 double t = (4 / (ak1 + 1) - 2 / (ak1 + 4) - 1 / (ak1 + 5) - 1 / (ak1 + 6)) / f1; 

Are you sure you need BigDecimal precision. that is, you need more than 15 digits of accuracy.

 double f1 = 1; double ak1 = 1; double t = (4 / (ak1 + 1) - 2 / (ak1 + 4) - 1 / (ak1 + 5) - 1 / (ak1 + 6)) / f1; System.out.println(t); System.out.println(new Sum(BigDecimal.ONE, BigDecimal.ONE, MathContext.DECIMAL64).calculate()); System.out.println(new Sum(BigDecimal.ONE, BigDecimal.ONE, MathContext.DECIMAL128).calculate()); 

prints

 1.2904761904761906 1.2904761904761904 1.2904761904761904761904761904761904 

If you need more than 15 digits of precision, you need BigDecimal. But if you are trying to simplify your code and not need such high precision, I would just use double .

+1
source

Source: https://habr.com/ru/post/1382156/


All Articles