Jump to content
Bochum_Boy

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 this post


Link to post
Share on other sites

Does CC65 float? Any transcendent functions?

I'd use a table.

Share this post


Link to post
Share on other sites
Posted (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 by 42bs

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 

Share this post


Link to post
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...