You need to sign in to do that
Don't have an account?

Apex Math Bug?
I've run into a disconcerting issue while writing a test case: there seems to be at least one case of decimal division that doesn't produce an accurate result. Since we deal with a lot financial/currency calculations, this is a little scary to me. Has anyone seen anything like this before or can anyone tell me if I'm doing something wrong here:
Decimal num = 2.10;Decimal denom = 2.0;Decimal result = num / denom;System.debug('Result=' + result.setScale(2).toPlainString());if (result != 1.05) System.debug('Not equal!');
If I run this code in an anonymous block, it displays the following:
20090626192509.249:AnonymousBlock.appirio_sm1: line 4, column 1: Result=1.00
20090626192509.249:AnonymousBlock.appirio_sm1: line 6, column 2: Not equal!
Thoughts?
If you use the decimal divide method, with a scale set, the result is correct:
Decimal num = 2.10;
Decimal denom = 2.0;
Decimal result = num / denom;
system.debug('\n\nresult='+result);
Decimal resultdiv =num.divide(denom,2);
system.debug('\n\ndivtest'+resultdiv);
System.debug('Result2=' + result.setScale(2).toPlainString());
system.debug('\n\n');
if (result != 1.05){
System.debug('Not equal!');
}
if (resultdiv != 1.05){
System.debug('Divide Method Not equal!');
}
Additionally, the documention indicates the "/" symbol is used on an integer or a double. If you do your calculation with a double, the answer also comes out correct.
All Answers
If you use the decimal divide method, with a scale set, the result is correct:
Decimal num = 2.10;
Decimal denom = 2.0;
Decimal result = num / denom;
system.debug('\n\nresult='+result);
Decimal resultdiv =num.divide(denom,2);
system.debug('\n\ndivtest'+resultdiv);
System.debug('Result2=' + result.setScale(2).toPlainString());
system.debug('\n\n');
if (result != 1.05){
System.debug('Not equal!');
}
if (resultdiv != 1.05){
System.debug('Divide Method Not equal!');
}
Additionally, the documention indicates the "/" symbol is used on an integer or a double. If you do your calculation with a double, the answer also comes out correct.