Trigonometric/transcendental function accuracy

General discussion about calculators, SwissMicros or otherwise
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Trigonometric/transcendental function accuracy

Post by dm319 »

Out of curiosity I calculated cos(1.57079632) on two Casio calculators in radians.
PXL_20230330_111920604.jpg
PXL_20230330_111920604.jpg (180.53 KiB) Viewed 1361 times
fx-6300G : 6.8 x 10-9
fx-115W : 6.795 x 10-9

Surprised at these results, both of these were quite highly regarded scientific calculators, especially the first one.
SN:09075
User avatar
Walter
Posts: 3070
Joined: Tue May 02, 2017 11:13 am
Location: On a mission close to DRS, Germany

Re: Trigonometric/transcendental function accuracy

Post by Walter »

Just for comparison: RAD 1.57079632 cos on the WP43 returns 6.794 896 619 231 321 639 352 319 743 005 672 × 10^-9 which matches WolframAlpha's result 6.794 896 619 231 321 639 352 319 743 005 671 63... × 10^-9.
WP43 SN00000, 34S, and 31S for obvious reasons; HP-35, 45, ..., 35S, 15CE, DM16L S/N# 00093, DM42β SN:00041
Thomas_ER
Posts: 192
Joined: Mon Jul 24, 2017 3:19 pm
Location: Germany

Re: Trigonometric/transcendental function accuracy

Post by Thomas_ER »

dm319 wrote:
Wed Mar 29, 2023 8:56 pm
rudi wrote:
Mon Mar 27, 2023 11:56 am
Which is also why I remember \(\sqrt{13}\)
...
PS have you come across Ken Sheriff's blog? He acid-removes 8086 and similar processors to look at how they worked.
...
There's also a german site with many many "insides", also describing the tools etc.
https://www.richis-lab.de/, e.g. https://www.richis-lab.de/cpu.htm,
Controllers from Calculators: https://www.richis-lab.de/calc.htm
Howto: https://www.richis-lab.de/Howto.htm
[ HP48/49/50/42S/WP34/HP Prime/ DM42 (#00185+00318) ]
User avatar
rudi
Posts: 415
Joined: Wed Nov 03, 2021 9:03 am
Location: Denmark
Contact:

Re: Trigonometric/transcendental function accuracy

Post by rudi »

dm319 wrote:
Wed Mar 29, 2023 8:56 pm
rudi wrote:
Mon Mar 27, 2023 11:56 am
Which is also why I remember \(\sqrt{13}\)
Can you tell me why you need to know this? Or else I will be forced to google notorious quake algorithm :)
it’s got nothing to do with the Quake algorithm.
I was coding assembler in the 80’ies, on paper (!!!). So I was manually executing the program on paper instruction by instruction, using 13 to test. That’s why I will never forget 3,605551275 ;-)

Quake magic/woodo algorithm, see this:
https://youtu.be/p8u_k2LIZyo
/Rudi

DM-42 (s/n 06999), HP-42S, HP-35s, HP-11c, HP-32SII (ex HP-41CV, ex HP-75C, ex HP-48G + a lot, really lot of a accessories)
Denmark
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Trigonometric/transcendental function accuracy

Post by dm319 »

rudi wrote:
Thu Mar 30, 2023 5:22 pm
I was coding assembler in the 80’ies, on paper (!!!). So I was manually executing the program on paper instruction by instruction, using 13 to test. That’s why I will never forget 3,605551275 ;-)
That's fascinating, do you have your algorithm still? I have utterly no idea how you would go about this. Did your processor have instructions for multiply and divide?
SN:09075
User avatar
rudi
Posts: 415
Joined: Wed Nov 03, 2021 9:03 am
Location: Denmark
Contact:

Re: Trigonometric/transcendental function accuracy

Post by rudi »

dm319 wrote:
Sun Apr 09, 2023 4:05 pm
rudi wrote:
Thu Mar 30, 2023 5:22 pm
I was coding assembler in the 80’ies, on paper (!!!). So I was manually executing the program on paper instruction by instruction, using 13 to test. That’s why I will never forget 3,605551275 ;-)
That's fascinating, do you have your algorithm still? I have utterly no idea how you would go about this. Did your processor have instructions for multiply and divide?
Im affraid my notes are lost. I made the assembler code, but never actually got it implemented. The processor had MULT and DIV, but only integers.
I don’t quite remember the algorithm I was implementing, but I think I got it from “Mathematics for the Millions” by Lancelot Hogben. I’ll try digging it out, next time a dare to go on a mission to our attic ;-)
/Rudi

DM-42 (s/n 06999), HP-42S, HP-35s, HP-11c, HP-32SII (ex HP-41CV, ex HP-75C, ex HP-48G + a lot, really lot of a accessories)
Denmark
User avatar
pauli
Posts: 252
Joined: Tue May 02, 2017 10:11 am
Location: Australia

Re: Trigonometric/transcendental function accuracy

Post by pauli »

The usual approach to computing square root is to get a rough approximation (often the initial number is used) and then apply Newton iterations to refine it. Each iteration of Newtons is fairly quick (one addition and a division/reciprocal from memory) and convergence is quadratic (i.e. it doubles the number of correct digits each step).

The only concern is that it doesn't always converge to a fixed value (i.e. the result can oscillate between iterations) so the convergence check needs to account for this. This is common for many numeric algorithms so it's not really a problem.

The actual iteration used is:

` x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} = x_n - \frac{x_n^2-x_0}{2x_n} = x_n - \frac{1}{2} ( x_n - \frac{x_0}{x_n} )`

instead of `x_0` here, it is okay to use 1 to change the division into a reciprocal. The iteration still needs to start at `x_0` if this is done.
User avatar
rudi
Posts: 415
Joined: Wed Nov 03, 2021 9:03 am
Location: Denmark
Contact:

Re: Trigonometric/transcendental function accuracy

Post by rudi »

pauli wrote:
Mon Apr 10, 2023 3:51 am
The usual approach to computing square root is to get a rough approximation (often the initial number is used) and then apply Newton iterations to refine it. Each iteration of Newtons is fairly quick (one addition and a division/reciprocal from memory) and convergence is quadratic (i.e. it doubles the number of correct digits each step).

The only concern is that it doesn't always converge to a fixed value (i.e. the result can oscillate between iterations) so the convergence check needs to account for this. This is common for many numeric algorithms so it's not really a problem.

The actual iteration used is:

` x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} = x_n - \frac{x_n^2-x_0}{2x_n} = x_n - \frac{1}{2} ( x_n - \frac{x_0}{x_n} )`

instead of `x_0` here, it is okay to use 1 to change the division into a reciprocal. The iteration still needs to start at `x_0` if this is done.
I think my approach was much simpler, probably using a Newton-Raphson approach
\(x_{i+1}=\dfrac{x_i + \dfrac{n}{x_i} }{2}\)

n = 13
x0 = 3 =>

3,666666667
3,606060606
3,605551311
3,605551275

Edit:
Wait a minute, I think it’s the exact same iteration as you show ;-)
/Rudi

DM-42 (s/n 06999), HP-42S, HP-35s, HP-11c, HP-32SII (ex HP-41CV, ex HP-75C, ex HP-48G + a lot, really lot of a accessories)
Denmark
User avatar
pauli
Posts: 252
Joined: Tue May 02, 2017 10:11 am
Location: Australia

Re: Trigonometric/transcendental function accuracy

Post by pauli »

Yes, it's the same :)
It's quick and easy and fast to converge. With a decent initial estimate, a small fixed number of iterations can be used.

The algorithm used in Quake uses a good first estimate and a single Newton-Raphson iteration from memory.
Pekis
Posts: 4
Joined: Mon May 29, 2017 2:45 pm

Re: Trigonometric/transcendental function accuracy

Post by Pekis »

Hello,

I remember coding in Z80 assembly the algorithm of the written square root, analog to a written division ... and no need of approximation series :)

Pekis
Post Reply