Back to General discussions forum
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
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)
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.