(The following article is another in the series Exploring SmartBASIC, by Guy Cousineau and was provided by Ron Mitchell, president of AUFG, via ANN. It originally appeared in the Jul/Aug ’91 ADVISA.)

In several of the previous articles on SMARTBASIC, I have mentioned variable commands. Although most of them are mathematical functions, a few are not; notably FRE and USR. Variable commands are those commands which pass a parameter within brackets: eg INT(123.45). The parameter is evaluated by the function in order to determine the result.

Presumably to save on interpretation and parsing code, the designers of SMARTBASIC adopted a complicated technique, which dynamically relocates these variable commands based on the LOMEM setting. Each of the variables is defined as an array and the array simply points back to the execution routine for each function.

When you are playing around with memory and accidently write where you should not, the variable commands are invariably the first ones to suffer. When they start misbehaving, the best thing to do is reboot.

In this article, we will briefly cover the use of arithmetic and algebraic functions. I will not attempt to describe the calculation method, for even if I understood it completely, it would take several pages to explain. The purpose of this article is to remind you that these functions are there and clarify their use as required.

INT does just what you might expect; it extracts the integer value of a real variable. Since it truncates rather than round off, statistical calculations will be more precise if you use INT(x+.5). You will also find that certain numbers truncate in a strange fashion. I have never noted the exact numbers, but the floating point has difficulty handling numbers like .001. For this reason, I often use INT(x+.50001). This helps to avoid those nasty INTEGER values which wind up being 37.9999997.

ABS takes the absolute value of a number by removing its sign thus ABS(-12) will yield 12. You can use ABS to make a number negative with something like -1*ABS(x).
SGN will report on the sign of a variable. SGN will return 0 if the variable is 0, -1 for negative values, and +1 for positive values. SGN can be used in conjunction with ON GOTO in the following fashion:

999 REM make decision on sign of x
1000 sx=SGN(x)+2: REM make result 1,2,3
1010 ON sx GOTO 2000,3000,4000
2000 REM handle negative
3000 REM handle zero
4000 REM handle positive

LOG takes the natural LOG (base e) of a number. If you are curious, the value of e is 2.718281828....You can come close to this value by asking SMARTBASIC for the LOG(10). If you want to take use 10 LOGS, just divide the LOG value by LOG(10):

999 REM subroutine to take base 10 log of x
1000 y=LOG(x)/LOG(10)

EXP is the complementary function which raises a to the power of the argument. Thus EXP(2) is the same as e^2. This function is redundant for powers of 10 since you can use 10^2 or 1.0E+2. It would be tedious, however to write 2.718281828^2.

SQR extracts the square root. Thus the Pythagorean theorem would be calculated by hyp = SQR(s1^2 + s2^2). The square root can also be expressed with hyp = (s1^2 + s2^2) ^ (1/2), but SQR is more convenient.

Before discussing the TRIG functions, a bit about RADIANS. Computers insist on working with radians rather than degrees. If you remember your high school trigonometry, there are ‘pi’ radians in 180 degrees or about 57.3 degrees per radian. ‘pi’ (despite what textbooks might say) has the value 3.141592657 and you can define RAD=180/3.141592657 to use as a conversion from degrees to radians. This will become clearer in a moment.

SIN takes the sine of the specified radian. If you would rather work in degrees, use something like SIN(45/RAD) to evaluate the sine of 45 degrees.

COS takes the cosine of the specified radian. Again, if you remember your high school math, COS(x)=SIN(90-x). Thus COS(45) should be the same as SIN(45). Define RAD as outlined above and print SIN(45/RAD) and COS(45/RAD).

If you change the value of ‘pi’ in the equation to a different value like 3.141592655, you will see that the values are not the same. Thus the value given above is the CORRECT one for working with ADAM’s floating point accumulator.

TAN takes the tangent and ATN takes the arc-tangent; the latter function is difficult to calculate manually.

All other TRIG functions can be evaluated using the 4 functions above; you just have to remember how it’s done. I must admit I have forgotten.


INT executes at 10672(29B0).
It verifies that the number is in floating point format. It then checks if the number is less than 1 in which case 0 is returned. It then juggles the number around to drop the decimal portion.

ABS executes at 2276(08E4).
It simply resets the sign bit in the floating point accumulator. This is why ABS cannot be used with INTEGER variables.

SGN executes at 2285(08ED). It starts by checking the value of the exponent in the FPA. If zero it simply returns which yields a zero value. It then sets the value in the FPA to +1 or -1 depending on the original sign of the number.

LOG executes at 3604(0E14).
It checks for zero and negative values and then calls a power series calculator to do a recursive calculation.

EXP executes at 3816(0EEB).
It uses a power series calculator to approximate the required value.

SQR executes at 3678(0E5E).
It does the calculation the way we learned it when we learned about logs. It takes the LOG of the value, divides it by 2 and recalculates the exponent. This is like doing e^(log(x)/2).

SIN executes at 3954(0F72).
This is the workhorse which uses a power series calculator to approximate the required value.

COS executes at 3946(0F6A).
It calculates the value from the formula COS(x)=SIN(x+pi/2), in radians, of course.

TAN executes at 3912(OF48).
It calculates the value from the formula TAN=SIN/COS.

ATN executes at 4180(1054). It also uses a power series calculator.

The following is a list of routines used by the MATH functions. They are very complex and should likely not be fiddled with. The addresses are included for information purposes only.

It takes a value from a table pointed to by HL. Copies FPA1 to FPA2, multiplies the first value at (HL) by FPA1 and multiplies the original result by the next table value. It is called by the controlling routine as long as there are values in the table.

Depending on the entry point, these routines add or subtract pi/2 or pi/4 to the current value in the floating point accumulator.

This one basically multiplies the 2 numbers in the FPA’s, then multiplies each component by values from the power table and finally adds the 2 halves together.

4497-4695 CONSTANTS
This is a set of values used by the various math routines when calling the power series calculators. Some are in floating point and others in integer format. You will find values corresponding to LOG(2) 1/LOG(2) pi/2 2/pi pi/4 1/11 1/9 1/7 1/5 1/3 but NOT pi or e.

That is the extent of my coverage of MATH functions.

Next time, random numbers...

Guy Cousineau
1059 Hindley Street


Back to Top