Re: Floating point: sine, cosine etc.

From: Jesus Cea <jcea_at_jcea.es>
Date: Thu, 13 Oct 2022 20:44:54 +0200
Message-ID: <25771584-e187-ddb3-949a-b65bfe91e946_at_jcea.es>
On 13/10/22 8:18, Baltissen, GJPAA (Ruud) wrote:
> An 
> for various functions like Sine, Cosine, ln, etc. but not for Tangens. 
> Can you?

Tan(x) is difficult because the asymptotes, You could do sin(x)/cos(x) 
and if you don't want to do taylor expansions twice, you could do cos(x) 
= sqrt(1-sin(x)^2).

Taylor expansion would be:

https://www.efunda.com/math/taylor_series/trig.cfm

tan(x) ~= x + x^3/3 + 2x^5/15 + 17x^7/315 + 69x^9/2835 + ...

If we check that expansion for tan(pi/4) = 1, we get 0.9994518388722274. 
You need extra terms for more precision, but tan(x) is a "difficult" 
function to approximate directly via Taylor. sin(x)/cos(x) will be quite 
more precise using less terms, and so faster.

And extra term would be 1382x^11/155925. Computing that extra term, you 
get tan(pi/4) ~= 1.0000735390411104. You can get more terms using
<https://www.wolframalpha.com/input/?i=taylor+series+tan+x> and pressing 
the "more terms" button.

Remember, thought, that "tan(x)" is difficult to approximate directly.

Checking the Commodore Basic ROM dump, 
<https://github.com/Project-64/reloaded/blob/master/c64/firmware/C64LD11.S>, 
we see that "tan()" is done as "sin()/cos()".

> I had a good look at it in Wikipedia. I liked the sentence "as the only 
> operations it requires are additions, subtractions, bitshift and lookup 
> tables. ". That would be ideal for a 6502 or Z80. But I had troubles 
> with the detailed explanation and even the example. But at the end the 
> article lead me to https://en.wikipedia.org/wiki/BKM_algorithm 
> <https://en.wikipedia.org/wiki/BKM_algorithm> which was easier to 
> understand.

Maybe you could check some other pages beside Wikipedia. There are tons 
of good explanations online, including source code you can understand. 
CORDIC is cool and you don't need multiplications. Quite well adapted 
for trigonometric and log functions in 8 bit CPUs.

> But for the moment I stick to polynomials because they have one 
> advantage: AFAIK all coefficients can be calculated (even those of 
> Tangens, but how?). This is quite slower than using tables but the 
> disadvantage of tables is that they need space. A good example: 
> Commodore BASIC only has stored some basic values but calculates the rest.

You only need a handful of values for Taylor expansion. Commodore uses 
5-6 terms only, 30 bytes total. Calculating the coefficients in real 
time would be slower and probably taking more than 30 bytes. CORDIC 
would require more memory (bigger tables) but it could be WAY faster. In 
fact CORDIC can calculate SIN(x) and COS(x) at the same time (with no 
multiplications, only adds/subs), useful for TAN(x) :-).

Maybe you could explain your constraints.

PS: cos(x) = sin(x + pi/2), so you only need taylor terms for sin(x) to 
calculate cos(x).

-- 
Jesús Cea Avión                         _/_/      _/_/_/        _/_/_/
jcea_at_jcea.es - https://www.jcea.es/    _/_/    _/_/  _/_/    _/_/  _/_/
Twitter: _at_jcea                        _/_/    _/_/          _/_/_/_/_/
jabber / xmpp:jcea_at_jabber.org  _/_/  _/_/    _/_/          _/_/  _/_/
"Things are not so easy"      _/_/  _/_/    _/_/  _/_/    _/_/  _/_/
"My name is Dump, Core Dump"   _/_/_/        _/_/_/      _/_/  _/_/
"El amor es poner tu felicidad en la felicidad de otro" - Leibniz
Received on 2022-10-13 21:00:33

Archive generated by hypermail 2.3.0.