Post Reply 
HP-35: Sin and cos function formulas, another point of view
08-14-2018, 09:21 PM (This post was last modified: 08-15-2018 09:18 AM by sasa.)
Post: #1
HP-35: Sin and cos function formulas, another point of view
As the most of members here are aware, the first pocket scientific calculator on the world uses the following formulas to calculate sin and cos:
\[ \sin (x) = \frac{\tan (x)}{\sqrt{1+{\tan^2(x)}}} \]
\[ \cos (x) = \frac{\cot (x)}{\sqrt{1+{\cot^2(x)}}} \]

As tan function is calculated using CORDIC, from my point of view, a bit faster and easier formulas could be some of follows for a half angle, avoiding using sqrt:

\[ \sin (2x) = \frac{2 \cdot \tan (x)}{1+{{\tan^2(x)}}}\]
\[ \sin (2x) = \frac{2} {\frac{1}{\tan(x)} + \tan(x)} \]
\[ \cos (2x) = \frac{1-\tan^2 (x)}{1+ \tan^2(x)} \]
\[ \cos (2x) = 1 - \frac{2 \cdot \tan^2 (x)}{1+{{\tan^2(x)}}} \]
\[ \cos (2x) = \frac{2}{1+\tan^2(x)} - 1 \]

I doubt HP wasn't aware of it when designing HP-35. It is interesting, however, what is possible reason not to use them. One probably reason I can think of is symmetrical form in formulas with sqrt, which may save some valuable space. The second, probably may be accuracy, as last significant digit may be inaccurate in most of upper functions, without proper round off mechanism lack in HP-35 due limited space.

One day I will probably try to modify the firmware regarding upper and test speed and accuracy difference. The most problematic here is that early MOSTEK chip made for HP-35 missing better handling when switch ROM and many jump instructions make a nightmare to do even smallest change. I would probably use extended instructions for ROM switches, used in later MOSTEK chips.

And this perhaps may be as well an interesting challenge for other members here, familiar with early microcode.
Find all posts by this user
Quote this message in a reply
08-15-2018, 01:46 AM
Post: #2
RE: HP-35: Sin and cos function formulas, another point of view
I tried it on Mathematica. Both set of formulas about equally accurate.

My revised MAPM arbitrary precision C library go even furthur, doing 1/5 angle

sin(5x) = sin(x) (16 sin(x)^4 - 20 sin(x)^2 + 5)
cos(5x) = cos(x) (16 cos(x)^4 - 20 cos(x)^2 + 5)

Both have the same polynomial form, so i define f(x), so that

sin(5x) = f(sin(x))
cos(5x) = f(cos(x))

To speed up convergence, apply f() 4 times:

sin(625x) = f(f(f(f(sin(x)))))
cos(625x) = f(f(f(f(cos(x)))))
Find all posts by this user
Quote this message in a reply
08-15-2018, 03:57 AM
Post: #3
RE: HP-35: Sin and cos function formulas, another point of view
this is my guess of why they pick the first set of equations. It is not as slow as we think.

say, we want cos(0.1)

N = 1 + tan(0.1)^2 = 1.010067046
cos(x) = 1/sqrt(N)

Newton's method for 1/sqrt(N) avoided the expensive division

1/sqrt(N) => x += 0.5 x * (1 - N x^2), until converge

if guess = 1, next iteration = 1.5 - N/2, so use that for x0

x0 = 0.994966476 -- guess
x1 = 0.995004163
x2 = 0.995004165
x3 = 0.995004165 -- value of cos(0.1)
Find all posts by this user
Quote this message in a reply
08-16-2018, 08:46 AM (This post was last modified: 08-16-2018 09:00 AM by sasa.)
Post: #4
RE: HP-35: Sin and cos function formulas, another point of view
(08-15-2018 03:57 AM)Albert Chan Wrote:  Newton's method for 1/sqrt(N) avoided the expensive division
...

Due extreme space limitation, I'm almost certain it is used implemented sqrt and reciprocal routine.

https://archived.hpcalc.org/laporte/Trigonometry.htm

Trace log for cos(x):
https://archived.hpcalc.org/laporte/cos_run.txt

Complete ROM listing:
https://archived.hpcalc.org/laporte/HP35%20ROM.htm
https://archived.hpcalc.org/laporte/hp35.asm
Find all posts by this user
Quote this message in a reply
08-16-2018, 09:58 AM
Post: #5
RE: HP-35: Sin and cos function formulas, another point of view
(08-16-2018 08:46 AM)sasa Wrote:  Due extreme space limitation, I'm almost certain it is used implemented sqrt and reciprocal routine.

Not only this. Since digit by digit methods are used for CORDIC, multiplication, division and square root all of them take roughly the same time.
This time depends on the sum of the digits but on average it's constant.

Thus each of these steps takes about the same time:
  • \(\tan(x)\) (CORDIC)
  • \(\cot(x)\) (division)
  • \(\cot^2(x)\) (multiplication)
  • \(\sqrt{1+\cot^2(x)}\) (square root)
  • \(\frac{\cot(x)}{\sqrt{1+\cot^2(x)}}\) (division)

Newton's method uses a multiplication in each step which is expensive on this CPU.
Thus we wouldn't benefit much, maybe even lose in some cases.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
08-16-2018, 03:26 PM
Post: #6
RE: HP-35: Sin and cos function formulas, another point of view
When I did the rough error test with Mathematica, the float is done in IEEE double.
Thus, multiply by 2 and divide by 2 is exact.

Not the case with decimal digits setup. (e.g. calculator with BCD digits)

Say, a calculator had 12 decimal digits internal precision,

x = 567812345678
2x = 1135624691356, internally round-up to 1135624691360

My guess is calculator will be less accurate with half-angle formula.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)