Precision of Java math functions - Stack Overflow
eclipse - How to get the rest of java Math funtions? - Stack Overflow
How precise is Java's Math class?
Java math libraries?
Videos
float has about 7.2 significative decimal digits.
double has about 15.9 significative decimal digits.
Your examples have the same 16 first significative decimal digits. This means that both pieces of code are getting exactly the same binary result. Assuming that your C compiler uses the same IEEE standard for 64 bit floats as java which is likely.
The difference you see after those 16 digits does not come from the way the math operation is performed and does not come from rounding error but from the way the different print functions deal with converting from binary double to decimal text.
The quickest way to get a correct answer is to use Wolfram alpha pi^10 this give the value of 93648.04747608302097371669018491934563599815727551469412705244 more digits could probably be obtained if needed. We see the cygwin C code is only correct to 15 digits
93648.0474760829820297658443 C
93648.04747608298 Java
93648.04747608302097371669018491934563599815727551469412705244
93648.047476082984468098606014523496270846023460034084392213341627
so you have exactly the same precision in both systems. You would expect to get same precision as both will likely use the IEEE 754 double precision floating point. You could say that java answer is better as it is not giving a false sense of accuracy by displaying more digits.
Unless you are specifically interested in calculating digits of pi or other number theory related task 16 digit accuracy will satisfy your needs. I've never seen an application where BigDecimal has proved to be useful and it requires a lot of work to get right.
A BigDecimal solution would be
MathContext mc = MathContext.DECIMAL128;
BigDecimal pi = new BigDecimal("3.141592653589793238462643383279503",mc);
BigDecimal res = pi.pow(10, mc);
out.println(pi);
out.println(res);
This uses a specific MathContext, the most accurate pre-defined one. If the numbers are approximate, as pi is, then its better to specify a MathContext. The only time you really want to use BigDecimal without a MathContext is if your values are exact, I not come across a time when you want to use this.
We use the string constructor with a value obtained from Wolfram alpha and the MathContext to fix the precision. We also use the same MathContext when calculating the power. The result of this is
3.141592653589793238462643383279503
93648.04747608302097371669018491938
if we compare this with the actual result which ends in 934 we see the result has an error in the last digit. Generally you expect most mathematics algorithms to be correct to within one unit of the last place, pow is a bit worse with a 2 ulp error. Using a MathContext mean we do not display spurious incorrect digits.
Looks like codename1 overrides some of the java math functions.They can be accessed with codename1's MathUtil class. Thank you all for your time.
Those functions are available from the java.lang.Math class. You could statically import them all - import static java.lang.Math.*;.