• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incorrect calculation result

 
Sunny Jaiswal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a samll program that calculates (3 + sqroot(5))^22, but the result is wrong!!!



Can someone please tell me what is the problem in the code, thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a "double" will give you only about 14 significant digits. Seeing that both results are identical in the first 14 digits, that's as good as it's going to get. See #20 in the http://faq.javaranch.com/java/JavaBeginnersFaq for some background of why this is so.

For more precision, use the BigDecimal class. Various implementations of the square root for BigDecimal are available on the net.
[ July 26, 2008: Message edited by: Ulf Dittmer ]
 
Sunny Jaiswal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that's a lot of information, thanks...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic