Search | | Contact Us | Resource Center Login | Home|

If we have a field named "balance" that is declared as a d28, what is the most accurate way of multiplying its value by 95% and storing the result back into the same field?

a. balance = balance * 0.95
b. balance = (balance * 95) / 100
c. balance = (balance * 95)#2
d. balance = (double)balance * 0.95

The earliest versions of the DIBOL language didn't include types with fractional precision. The language never implied a decimal point, so the programmer had to do so instead. This didn't affect addition and subtraction, but the code had to make decimal point adjustments for multiplication and division. Even though Synergy/DE (then called DBL) added implied decimal arithmetic many years ago, old habits die hard and old code dies harder.

Answers (b) and (c) use this approach, which you'll find in a lot of older programs. However, (c) is more accurate than (b), because (c) uses the rounding operator (#), whereas (b) performs a decimal division that will truncate any remainder (even 99). Answer (c) still suffers from a vulnerability, though. Since balance is a d28 (the maximum size for decimal fields), if the two high-order digits contain anything other than zeroes, then the intermediate multiplication may cause a BIGNUM error ("Arithmetic operand exceeds maximum size").

To achieve optimum precision for all possible values of balance, we need to abandon the old ways. Answer (d) looks like how you would do it in most type-safe languages, but it has two problems. First, a decimal field can only be cast to double in Synergy .NET, so on other platforms you’d get an “Invalid cast” error. Even if you could cast it, double isn't precise enough for 28 digits.

We want to stick with a good old Synergy/DE decimal, but without shifting digits for precision. That's exactly what answer (a) does. The compiler presumes that 0.95 is implied decimal. When multiplying a decimal value by an implied decimal value, the result is implied decimal. Storing that back into the decimal field rounds the value (as long as you don't specify truncation via option #11 or the TRUNCATE routine option). Thus, (a) is the correct answer.

Besides, it even reads better!

More information about News and Events

Contact Synergex to learn more about  News and Events