Reputation: 137
i am now making a source code that calculates long integers, but I don't know why this calculation gives me wrong answer.
long l;
//variable l is where you input long, signed int
l *= 0x6869L;
if(l == 0xeaaeb43e477b8487L)
System.out.println("Correct!");
I did 0xeaaeb43e477b8487 / 0x6869 = 0xFFFFCBBB6D375815 but when I calculate 0xFFFFCBBB6D375815 * 0x6869 gives 0xEAAEB43E477BA89D.
Why is this thing happens? and what is the real answer of this math question?
Upvotes: 2
Views: 2444
Reputation: 136002
This is because when you divide 0xeaaeb43e477b8487L / 0x6869 you lose the remainder, which causes loss of precision.
0xeaaeb43e477b8487L % 0x6869= -9238
if we take it into account we will get
0xEAAEB43E477BA89DL -9238 = 0xeaaeb43e477b8487
this works
if ((l - 9238) == 0xeaaeb43e477b8487L)
System.out.println("Correct!");
Upvotes: 3