0

When I check some code, I found a miss calculation regarding bit operation ( |0).

First, I wonder that there are any special reasons why is '|0' used after calculating two numbers like (a*b)|0

Secondly, in the below case, I expect to get a result is 220050 but the result is 220049. Do you guys know why I get this result?

importPackage(java.io); importPackage(java.lang);

var a = 164.700;
var b = 1500;

var result = (a*b)|0;


System.out.println(result);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jun
  • 9
  • 1

1 Answers1

1

the |0 converts the value to a 32-bit integer by rounding towards zero. the result of the multiplication is slightly less than an integer because the numbers are internally stored in binary, and you can't represent most fractional numbers exactly in binary for the same reason 1/3 can't be written exactly in decimal with a finite number of digits after the decimal point. the 164.7 comes out as 164.69999999999998863131622783839702606201171875.

programmerjake
  • 1,794
  • 11
  • 15