Question about problem 18

Back to General discussions forum

bmarkey     2015-11-06 00:45:08

My code works, but I'm off on a few of the results by like .00000000xx.
I just want to know if I am supposed to be going down the BigDecimal route with this or should I look elsewhere?
Currently using doubles and after asking over at SO I'm getting mixed answsers. Many say its just the inherent loss of precesion.

Thanks

Bruce

Rodion (admin)     2015-11-06 04:59:42
User avatar

Hi, Bruce! Thanks for your question!

I think you need not BigDecimal for this problem - double should be enough. I see you are formatting output with:

DecimalFormat df = new DecimalFormat("##.###");
...
System.out.print((df.format(r) + " "));

This seems to be too restricting. You only need to take care that no less than 7 digits after decimal dot are printed, so it could be just

System.out.println(r + " ");

(provided that r itself is correct, i.e. falls in the close enough range with the expected answer)

bmarkey     2015-11-06 11:34:53

Thank you for getting back to me so fast. So long story short I tried it again, same code, different data set and it was fine.

Thanks again.

Please login and solve 5 problems to be able to post at forum