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
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: 42965
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 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

Joined: Feb 01, 2008
Posts: 14
Wow, that's a lot of information, thanks...
I agree. Here's the link:
subject: Incorrect calculation result
jQuery in Action, 3rd edition