BCD Operations and Procedures

Top  Previous  Next

Clarion has a Binary Coded Decimal (BCD) library of operations and procedures that execute in a manner similar to the manner in which decimal arithmetic is performed on paper. These operations use internal intermediate values with 31 digits accuracy on both sides of the decimal point.

The big advantage of the BCD operations is that it is very easy to "see" what is happening because they execute just as you would with pencil and paper. Simply imagine doing the computation long hand and throwing away numbers that go off the end of the page (rounding to the right).

Having 31 fixed decimal places either side of the decimal point there are numbers that cannot be represented in a BCD system which can be represented by a REAL. Therefore, understanding what is going on is useful.

Generally, the only cases where underflow will affect you is in division operations, usually when dividing by a multiple of 3. For example:

100000/3 = 33333.3333333333333333333333333333333

(100000/3)-INT(100000/3)*100000 = 33333.3333333333333333333333333300000

BCD computation times are very data sensitive; the time taken is proportional to how long the computation would take you by hand. Therefore, the longer the numbers involved, the longer the execution times. However, standard "tricks of the trade" (such as multiplying by a power of ten by shifting the decimal point) are spotted, making the BCD libraries fast in real world applications.

The following operations may execute as BCD operations:

Addition (+), Subtraction (-), Multiplication (*)

Performed as a BCD operation when neither operand has a REAL Base Type (both are LONG or DECIMAL) and one has the DECIMAL Base Type. Any digits appearing to the right of 1^31 disappear (wrap), and any to the left of 1^-30 are rounded up.

Division (/)

Performed as a BCD operation when neither operand has a REAL Base Type (both are LONG or DECIMAL). Any digits appearing to the right of 1^31 disappear (wrap), and any to the left of 1^-30 are rounded up.

Exponentiation (^)

Performed as a BCD operation when the first operand is a DECIMAL or LONG Base Type and the second operand is a LONG Base Type. Any digits appearing to the right of 1^31 disappear (wrap), and any to the left of 1^-30 are rounded.

ABS()

Removes the sign from a DECIMAL variable or intermediate value and returns the DECIMAL value.

INT()

Truncates a DECIMAL intermediate value and returns a DECIMAL value.

ROUND()

If the second parameter is a LONG or DECIMAL Base Type, then rounding is performed as a BCD operation which returns a DECIMAL value. ROUND is very efficient as a BCD operation and should be used to compare REALs to DECIMALs at decimal width.