You need to sign in to do that
Don't have an account?
Admin Factor
Long variable misses a digit in division
I am writing a validation code that checks if the module and division of the module of another meet some criteria:
Could you help me?
Thank you very much.
Long decVal = Long.ValueOf(tmp); Integer reminder = Modulo529(decVal); Integer cociente = reminder/23; Integer residuo = math.mod(reminder,23); public Integer Modulo529(Long x){ Long result; Long div = 529; result = (x/div); result = x - (529 * result); return Integer.valueOf(result); }My problem is that given some number (0987543210987654) the result of the module is wrong, the simple division of that number should be 1866811362925, instead it is 186681136292, it loses the last digit (5), it should be inside the range of 2^63 - 1.
Could you help me?
Thank you very much.
The Problem is not with Long, but you are retrieving wrong value and you didn't identify it:-
See line No.2 I have changed it to (2,18)
In your case the division was happening with 098754321098765 not with 0987543210987654 (ending with 4).
The division of 098754321098765 / 529 = 186681136292 (So the value was coming correct).
while the division of 0987543210987654 / 529 = 1866811362925.
Just print your tmp value before - Long decVal = Long.ValueOf(tmp);
System.debug(tmp);
you will come to know that the value you were passing was 098754321098765 not 0987543210987654
Hope it explains.
All Answers
It is working fine for me.
Just pass the value as 0987543210987654L otherwise it will treat your value as Integer.(As we does in Java :) )
Mark it as answer if it solves your problem.
The Problem is not with Long, but you are retrieving wrong value and you didn't identify it:-
See line No.2 I have changed it to (2,18)
In your case the division was happening with 098754321098765 not with 0987543210987654 (ending with 4).
The division of 098754321098765 / 529 = 186681136292 (So the value was coming correct).
while the division of 0987543210987654 / 529 = 1866811362925.
Just print your tmp value before - Long decVal = Long.ValueOf(tmp);
System.debug(tmp);
you will come to know that the value you were passing was 098754321098765 not 0987543210987654
Hope it explains.