aspose file tools
The moose likes Java in General and the fly likes Math.pow question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Math.pow question" Watch "Math.pow question" New topic
Author

Math.pow question

Felix Ramirez
Greenhorn

Joined: Jun 22, 2008
Posts: 3
Why if i do in the windows calculator 1.0175 ^ 12 i got 1.2314393149447913669909357484341 and if i do it in java like this Math.pow(1.0175, 12) i got 1.2314393149447924, i mean this is not even the rounded value. How can i do in java to obtain the complete value (1.2314393149447913669909357484341) i got in the calculator?.
[ June 22, 2008: Message edited by: Felix Ramirez ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

If you need decimal calculations this precise (for instance when using with currencies) you should use java.math.BigDecimal instead. It may be slower, but it is quite precise. In fact, the following code will produce the following result:


As you can see it is even more precise!


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Felix Ramirez
Greenhorn

Joined: Jun 22, 2008
Posts: 3
Thanks, this is what i needed, another one, how can i round a number (big decimal) to a N places decimals, i have searched a lot but didn't get anything...
[ June 22, 2008: Message edited by: Felix Ramirez ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

How about BigDecimal.round(MathContext)?


Felix Ramirez
Greenhorn

Joined: Jun 22, 2008
Posts: 3
Thank you very much for the help and the patience
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Rob Prime:
If you need decimal calculations this precise


Be careful about "precise" and "accurate".

The precision is the number of digits shown in the answer.

A rough definition of accuracy is the number of digits in the answer that are correct.

However, if floating-point arithmetic (double or float) has been used, then some of the digits may not be correct; the accuracy is often less than the precision. If many floating-point calculations are chained together carelessly, then most or all of the digits could be wrong!

BigDecimal can achieve almost unlimited precision and total accuracy; that is, all the digits are always correct. However, it is much slower, and uses much more storage than floating-point.

Note that BigDecimal can only do pow() with integer powers. There's no sqrt(), either. That's how it guarantees total accuracy.

Finally, note that Math.pow() is generally a bad way to compute integer powers, even when you are using floating-point. Unless your integer power is very large, you will likely get a better answer using multiplication. For example, for your z=x^12, you might do y=x*x*x, z=y*y*y*y.
[ June 23, 2008: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Math.pow question
 
Similar Threads
Loss of precision worry when using Math.pow()
how to calculate e ^3 using Math.pow
Quadratic formula result problem
Java Math issue incorrect output
One more question, then thats it I promise :) ...