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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Incorrect calculation result" Watch "Incorrect calculation result" New topic

Incorrect calculation result

Sunny Jaiswal

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

Joined: Mar 22, 2005
Posts: 41034
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 - my free Android networking tools app
Sunny Jaiswal

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