-2

I have code as below,

int x5K = 5000, x50K = 50000;

The execution of x5K * 166.2 (ie 5000*166.2) is 831000.0 & very much equal to calculators multiplication.

But the execution of x50K * 166.2 (ie 50000*166.2) is 8309999.999999.... very much different from calculators multiplication. According to calculator the multiplication should be 8,310,000 which is huge difference.

Rounding off is also not a solution as many other multiplication statements are accurate then why 50000*166.2 is executing weirdly.

Can anyone help me out on this. Thanks

kalpesh patil
  • 77
  • 1
  • 5
  • 0.00001 is a huge difference? – Fred Larson Jun 03 '22 at 13:33
  • @FredLarson Displaying `8,310,000` vs `8309999.999999` on UI is huge difference for end user. My intention to post the question is to understand why javas engine & calculator are giving two different answer & is their any workaround for this? – kalpesh patil Jun 03 '22 at 13:40
  • 1
    Of course it's a big difference on the UI, which is why floating points always have to be rounded for display. – Fred Larson Jun 03 '22 at 13:50
  • "huge difference" - mathematically there is no difference between `8310000` and `8309999.9...` (in other values `0.999...` (infinite `9`s) is `1.0`) - see [Wikipedia](https://en.wikipedia.org/wiki/0.999...) or https://www.youtube.com/watch?v=G_gUE74YVos or https://www.youtube.com/watch?v=-tlIPvK9gmM (sure, floating point numbers in Java cannot represent infinite digits) – user16320675 Jun 03 '22 at 14:07

0 Answers0