Before we finish discussing numbers, it's time to add something to our "SQL library". To be worthy of addition to the SQL library, a routine must (a) be good clean SQL, (b) be callable from C and Delphi, (c) be actually useful in C and Delphi because it does something that those languages can't and (d) have nothing at all do with "databases" -- it should be available for use just like any general function library.

Our addition to the SQL library for this chapter will be a calculator. It won't match C and Delphi for floating-point arithmetic, but it will give more exact answers. Here it is.

Function:
SQL_calculator (lp_calculation, lp_result, lp_error)
Pass:
An arithmetic expression in the string lp_calculation. The string may contain any combination of numeric <literal>s (in valid SQL form), the operators * + * / MOD ABS and parentheses.
Return:
lp_result: Result of expression (a string containing a number)
lp_error: SQLSTATE and error message, if expression was invalid.
Example:
Try passing the expression: (1.000001 + 1.999990) * 11000 to our calculator. Our proc gives the correct result: "33000.0000000". The compilers we tested gave the wrong result: "32999.9901000". (Remember that in SQL all the <literal>s in this expression are DECIMAL, not floating-point, <literal>s.)

Note:

Portions of the text in this entry are Copyright © 1999 by Ocelot Computer Services Incorporated. Used by permission.

Comments

Comments loading...