wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Incorrect calculation result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Incorrect calculation result" Watch "Incorrect calculation result" New topic
Author

Incorrect calculation result

Sunny Jaiswal
Greenhorn

Joined: Feb 01, 2008
Posts: 14
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.


Preparing for SCJP...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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 ]

Ping & DNS - updated with new look and Ping home screen widget
Sunny Jaiswal
Greenhorn

Joined: Feb 01, 2008
Posts: 14
Wow, that's a lot of information, thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incorrect calculation result
 
Similar Threads
simple "if"...
How to make combination function generic
New java questions
Please help me with the logic of the code!