The fastest way to get the correct answer is to use Wolfram alpha pi ^ 10 , this will give the value 93648.04747608302097371669018491934563599815727551469412705244, probably more numbers could be obtained if necessary. We see that cygwin C code is correct for only 15 digits
93648.0474760829820297658443 C 93648.04747608298 Java 93648.04747608302097371669018491934563599815727551469412705244 93648.047476082984468098606014523496270846023460034084392213341627
therefore, you have exactly the same accuracy on both systems. You expect the same accuracy as the likelihood of using the IEEE 754 double precision floating point. You could say that the answer to Java is better, as it does not give a false sense of precision by displaying more numbers.
If you are not specifically interested in calculating the digits of the pi task or other number theory, then an accuracy of 16 digits will satisfy your needs. I have never seen an application where BigDecimal has proven useful, and it takes a lot of work.
BigDecimal solution will be
MathContext mc = MathContext.DECIMAL128; BigDecimal pi = new BigDecimal("3.141592653589793238462643383279503",mc); BigDecimal res = pi.pow(10, mc); out.println(pi); out.println(res);
In this case, a specific MathContext is used, the most accurate predefined one. If numbers are approximate, like pi, it is better to specify a MathContext. The only time you really want to use BigDecimal without a MathContext is that if your values ββare accurate, I don't encounter the time when you want to use this.
We use a string constructor with a value obtained from Wolfram alpha and MathContext to capture precision. We also use the same MathContext when calculating power. The result of this is
3.141592653589793238462643383279503 93648.04747608302097371669018491938
if we compare this with the actual result, which ends in 934, we see that the result has an error in the last digit. Typically, you expect most math algorithms to be correct within the same unit of last place, pow is a little worse with a 2 ulp error. Using MathContext means that we do not display false incorrect numbers.