• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Math.pow question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Felix Ramirez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about BigDecimal.round(MathContext)?


 
Felix Ramirez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the help and the patience
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Liar, liar, pants on fire! refreshing plug:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic