# Math.h functions for Lynx

## Recommended Posts

Is there a known example how to calculate a tan() function with a Lynx? The CC65 seems not to ship with Math.h.

Bests,

##### Share on other sites

Does CC65 float? Any transcendent functions?

I'd use a table.

##### Share on other sites

No, it does not support floats only doubles, afaik.

##### Share on other sites
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 by 42bs

##### Share on other sites

Because I want to calculate an angle to determine a direction a speed of an object. Will work now with a 180 grad integer.

##### Share on other sites

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.

##### Share on other sites

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...

##### Share on other sites

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

##### Share on other sites

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

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   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.