# Commodore BASIC Question

## Recommended Posts

The commands TAN, SGN and and LOG make no sense to me from a programming perspective. What do they do and how are they implemented in commodore BASIC? What is their function for graphics or games design? Can I leave them out or are they imperative?

I RTFM but i need clarification.

##### Share on other sites

unless you are doing programming using trig or logarithms you shouldn’t need them. they are pretty slow in basic, usually it’s better to use lookup tables.

lookup table?

##### Share on other sites

instead of calling the sine function you create an array containing the preclalculated values

simple example would be a multiplication table for multiplying by 2

[2,4,6,8,10,12,14,16…]

so 1x2 would be in position 1, 2x2 would be in position 2, etc

not a lot of value for multiplication but valuable for complex math like sin, etc

##### Share on other sites
1 hour ago, evg2000 said:

instead of calling the sine function you create an array containing the preclalculated values

simple example would be a multiplication table for multiplying by 2

[2,4,6,8,10,12,14,16…]

so 1x2 would be in position 1, 2x2 would be in position 2, etc

not a lot of value for multiplication but valuable for complex math like sin, etc

This may be valuable..  Try ? 9^2   and ? 9*9

I don't have a 64 to try this on at the moment.  But I remember getting strange results with some simple math.

##### Share on other sites

As mentioned, TAN is a trigonometric function to calculate the tangent on a value given in radians. The inverse function is ATN for arctangent. If you do PRINT ATN(1)*4 you will get Pi. Lookup tables may be faster, but also takes more space to prepare and if you are working in BASIC anyway, the entire program already is slow as it is so unless you use a ton of sinus, cosinus, tangent etc it might not matter.

LOG calculates the linear logarithm of a value, the inverse of the EXP function. If you don't know what logarithms are, look it up or simply ignore those. I'll admit that outside of mathematical applications you rarely will have use of those.

SGN on other hand can be quite valuable sometimes since it tells if a value is negative, zero or positive. For instance if you have a direction variable DX that can accelerate so on each frame it moves 1, 2, 3, 4, 5 steps either to the left or right, or the object can stand still, it may have values between -5 and +5. Now if you want to know in which direction the object is moving, you use SGN(DX) to get -1, 0 or 1. If you want to know at what speed the object is moving, you use ABS(DX) to get the absolute value, i.e. 0, 1, 2, 3, 4, 5.

Many people get by without knowing every function or the behavior of every command, but the more you know, the more efficient programs you will be able to write.

##### Share on other sites

A logarithm would be pretty difficult to calculate without a LOG function.

So that one's definitely useful.

"tan(a)" seems to be "sin(a) / cos(a)", so that could be replaced. But it sure is convenient.

And if you had the bytes in the interpreter for it (having these bytes was a miracle in a way, because these interpreters did so much in so few KB), why not put it in?

Edited by Pokeypy

##### Share on other sites

For those curious, here are some C64 addresses. For VIC-20, subtract 3 bytes so e.g. \$E26B becomes \$E268 etc.

\$E264: Evaluate COS by adding Pi/2 to FAC, then...

\$E26B: Evaluate SIN

\$E2B4: Evaluate TAN

\$E2E0 - \$E30D: MFLPT constants for Pi/2, 2*Pi, 0.25 and SIN evaluation (46 bytes)

\$E30E: Evaluate ATN

\$E33E: MFLPT constants for ATN evaluation (61 bytes)

Some more constants:

\$AEA8 (VIC \$CEA8): Pi in MFLPT format (5 bytes)

\$B9C1 (VIC \$D9C1): Various other constants (41 bytes)

\$BFBF (VIC \$DFBF): MFLPT constants for EXP evaluation (46 bytes)

Thus the ROM has roughly 200 bytes of constants to help with those functions. It might not fully be the same as table based lookup, but certainly a lot smaller than if each routine was implemented entirely by algorithm.

##### Share on other sites

I was never that good at math (multiplication is somewhat difficult but i can grasp algebra somewhat, addition and subtraction are fine. Money and change not so much but i'm improving).

thanks for the help

Edited by Frozone212

##### Share on other sites

I am purely an amateur, but I have written code in BASIC, Pascal, and Delphi -- and I have never once had to use any of the more complex mathematical functions. There were many pages in the manual describing them that I just skipped-over.

Of course this also depends on what type of program you are creating. Almost all of my coding has been text-based games and database applications. At the same time, as others have mentioned, 8-bit BASIC is not really well suited to creating heavy number crunching applications.

##### Share on other sites

In regard to the use of ABS() and SGN(), I use these functions in a user input routine for TI BASIC/XB.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.