Re: Floating point: sine, cosine etc.

From: Michal Pleban <lists_at_michau.name>
Date: Sat, 24 Sep 2022 14:35:53 +0200
Message-ID: <ee54a828-e59e-d766-eb16-97629a69c65e_at_michau.name>
Michal Pleban wrote on 23.09.2022 22:48:

> Basically, it amounts to something like this for SIN(X):
> 
> S = X  : REM RESULT
> Z = 1  : REM SIGN
> P = X  : REM POWER
> F = 1  : REM FACTORIAL
> FOR I = 3 TO [SOME_LIMIT] STEP 2
>    P = P * X * X
>    F = F * (I - 1) * I
>    Z = -Z
>    S = S + Z * P / F
> NEXT I

Just ran this program on my CBM-II. With the floating point precision 
that is used in Commodore BASIC, the number of iterations needed to 
converge on a result when X is in the range [-PI, PI] is 10 (so the 
LIMIT = 21). If you use bigger precision, you may need a few more steps. 
10 steps is not much, so you can get really a result in a reasonable CPU 
time.

For what it's worth, I also measured the speed of the SIN function in 
CBM BASIC, and it turns out it is about 11 times slower than a 
multiplication. Makes sense given the above.

Regards,
Michau.
Received on 2022-09-24 15:00:16

Archive generated by hypermail 2.3.0.