Skip to content

Commit 0e9dbea

Browse files
committed
Merge branch 'master' of github.com:eobermuhlner/big-math
2 parents 87a051c + 29184a7 commit 0e9dbea

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,32 @@ When calculating these functions you need to specify the number of digits you ne
162162

163163
The `MathContext` contains a precision and information on how to round the last digits, so it is an obvious choice to specify the desired precision of mathematical functions.
164164

165+
#### Why is there no `setMathContext()` so I do not have to pass `MathContext` all the time?
166+
167+
Maintaining a static default `MathContext` would simplify calling the mathematical functions but it would make the static class stateful
168+
which is a terrible thing. Imagine two parts of your programming setting the precision differently and the debugging mess you have afterwards, especially if they run in parallel threads.
169+
170+
But there is a trivial solution that might work perfectly fine for your project.
171+
172+
Write your own little wrapper class with your own hardcoded precision.
173+
This is stable, simple, thread-safe and self-documenting.
174+
175+
```java
176+
public class HighPrecisionMath {
177+
public static MathContext MATH_CONTEXT = new MathContext(100);
178+
179+
public static BigDecimal PI = BigDecimalMath.pi(MATH_CONTEXT);
180+
181+
public static BigDecimal pow(BigDecimal x, BigDecimal y) {
182+
return BigDecimalMath.pow(x, y, MATH_CONTEXT);
183+
}
184+
185+
public static BigDecimal sin(BigDecimal x) {
186+
return BigDecimalMath.sin(x, MATH_CONTEXT);
187+
}
188+
}
189+
```
190+
165191
#### I specified a precision of `n` digits, but the results have completely different number of digits after the decimal point. Why?
166192

167193
It is a common misconception that the precision defines the number of digits after the decimal point.

0 commit comments

Comments
 (0)