Frozone212 Posted October 13, 2021 Share Posted October 13, 2021 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. Quote Link to comment Share on other sites More sharing options...
+evg2000 Posted October 13, 2021 Share Posted October 13, 2021 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. https://www.quora.com/What-do-cos-sin-tan-and-log-mean 1 Quote Link to comment Share on other sites More sharing options...
Frozone212 Posted October 13, 2021 Author Share Posted October 13, 2021 lookup table? Quote Link to comment Share on other sites More sharing options...
+evg2000 Posted October 13, 2021 Share Posted October 13, 2021 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 Quote Link to comment Share on other sites More sharing options...
1980gamer Posted October 13, 2021 Share Posted October 13, 2021 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. Quote Link to comment Share on other sites More sharing options...
carlsson Posted October 13, 2021 Share Posted October 13, 2021 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. 2 Quote Link to comment Share on other sites More sharing options...
Pokeypy Posted October 13, 2021 Share Posted October 13, 2021 (edited) 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 October 13, 2021 by Pokeypy Quote Link to comment Share on other sites More sharing options...
carlsson Posted October 13, 2021 Share Posted October 13, 2021 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. Quote Link to comment Share on other sites More sharing options...
Frozone212 Posted October 13, 2021 Author Share Posted October 13, 2021 (edited) 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 October 13, 2021 by Frozone212 additional info Quote Link to comment Share on other sites More sharing options...
jhd Posted October 14, 2021 Share Posted October 14, 2021 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. Quote Link to comment Share on other sites More sharing options...
+OLD CS1 Posted October 14, 2021 Share Posted October 14, 2021 In regard to the use of ABS() and SGN(), I use these functions in a user input routine for TI BASIC/XB. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.