Bochum_Boy #1 Posted July 16, 2019 Is there a known example how to calculate a tan() function with a Lynx? The CC65 seems not to ship with Math.h. Bests, Quote Share this post Link to post Share on other sites
42bs #2 Posted July 16, 2019 Does CC65 float? Any transcendent functions? I'd use a table. Quote Share this post Link to post Share on other sites
Bochum_Boy #3 Posted July 16, 2019 No, it does not support floats only doubles, afaik. Quote Share this post Link to post Share on other sites
42bs #4 Posted July 16, 2019 (edited) 1 hour ago, Bochum_Boy said: No, it does not support floats only doubles, afaik. So, why ask for tan() in the first place? Or differently asked: What do you want to achieve? Edited July 16, 2019 by 42bs Quote Share this post Link to post Share on other sites
Bochum_Boy #5 Posted July 16, 2019 Because I want to calculate an angle to determine a direction a speed of an object. Will work now with a 180 grad integer. Quote Share this post Link to post Share on other sites
+karri #6 Posted July 16, 2019 There is actually a little bit trigonometry in cc65. int __fastcall__ cc65_sin (unsigned x); /* Return the sine of the argument, which must be in range 0..360. The result * is in 8.8 fixed point format, which means that 1.0 = $100 and -1.0 = $FF00. */ int __fastcall__ cc65_cos (unsigned x); /* Return the cosine of the argument, which must be in range 0..360. The result * is in 8.8 fixed point format, which means that 1.0 = $100 and -1.0 = $FF00. */ But no tan I am afraid. Quote Share this post Link to post Share on other sites
VladR #7 Posted July 16, 2019 CC65 is very convenient because it automatically handles all the signed formats for you without you doing anything (which, in ASM, is a lot of work and it's not pretty). This kind of prototyping is best done in something with debugger like Visual Studio: 1. Create function that computes tan () for the full range of values that you can encounter 2. Convert to short int and print it out 3. Copy paste the printout into CC65 as a table I would reckon that 16 bits per value should be really enough, given the low resolution of Lynx. If you need to save couple hundred bytes, just halve the array and invert it. Of course, only you know the input range, so you might be able to get away with just 8 bits of precision, especially if you store only 50% of the range. At a slight complication, you might store double precision for a most common sub-range of the main range and keep the precision lower for the rest. But, really, 360 Bytes is nothing... Quote Share this post Link to post Share on other sites
+karri #8 Posted July 16, 2019 Actually the cc65 sincos table is just 87 bytes due to the format being 8.8. The sin of 87..90 is 1.0. So it is essentially a byte table of sin values for 0..90 Quote Share this post Link to post Share on other sites
rhcocker #9 Posted July 16, 2019 I don't program for retro consoles so this might be a naive suggestion, but could you just approximate it with its 5th degree taylor expansion? if x is between 0 to pi/2 use tan x = x + 1/3 x3 + 2/15 x5 if x is between pi/2 to pi use tan x = x-pi + 1/3 (x-pi)3 + 2/15 (x-pi)5 (= is approximately here) any error should only be noticeable very close to pi/2 Quote Share this post Link to post Share on other sites