Post Reply 
HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
09-03-2022, 01:21 PM (This post was last modified: 09-03-2022 01:22 PM by Gil.)
Post: #1
HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
When executing loops for tan(x) on the emu48 with an android phone, we see that the average execution time for tan(x) on HP50G is 1.5 faster than for sin(x) or cos(x).

Shouldn't it be about the same speed (±10% instead of +50%)?

Thanks for your insight.

Gil
Find all posts by this user
Quote this message in a reply
09-03-2022, 07:44 PM
Post: #2
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
On the physical calculator, SIN and COS take about 9.9 ms, and TAN takes about 6.1 ms which is a 62% difference. There are some operations where the speed ratio of EMU48 is higher or lower than average but the differences are relatively small.

With EMU48 on a phone, we have an ARM processor emulating a Windows PC emulating an older ARM emulating the Saturn processor! I't's amazing it works at all, never mind as well as it does.
Find all posts by this user
Quote this message in a reply
09-03-2022, 07:51 PM
Post: #3
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
But my question was rather:

why the big time difference between tan(x) and cos(x) or sin(x), independently if using the real calculator or emu48?
Find all posts by this user
Quote this message in a reply
09-03-2022, 09:28 PM (This post was last modified: 09-03-2022 09:30 PM by Gil.)
Post: #4
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
Other question on the same theme:

Why is ln(x) about 2.8 faster than sin(x), but log(x) only 2.1 faster than sin(x)?

In other words, what are the algorithms underlying all those functions that might explain the above mentioned calculation speeds?

Thanks for your insight.

Gil
Find all posts by this user
Quote this message in a reply
09-04-2022, 09:31 AM
Post: #5
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
(09-03-2022 09:28 PM)Gil Wrote:  Why is ln(x) about 2.8 faster than sin(x), but log(x) only 2.1 faster than sin(x)?

Not sure if this applies fully to HP calculators but one thing I do know is that CORDIC algorithms provide ln(x) directly. It could well be that the calculator is taking ln(mantissa) and dividing it by ln(10) to get log(mantissa) before adding the exponent of 10 of the original number. There is therefore an extra calculation (division by ln(10)) involved in getting log(x).

Current daily driver: HP 71B w/FRAM 71B
Find all posts by this user
Quote this message in a reply
09-04-2022, 09:50 AM
Post: #6
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
My experience with the 34S was that LN was **much** more difficult to compute than SIN or COS. On the 34S, TAN is computed as SIN / COS. However, SIN & COS are computed simultaneously and TAN is a single division overhead.
Find all posts by this user
Quote this message in a reply
09-04-2022, 09:53 AM
Post: #7
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
Interesting possibility.

But using "your" formula for log(x), ie dividing ln(x) by 2.30258509299 [ln(10], the elapsed time is much longer than the direct use of the log command.

By your suggestion, we might say that
tan(x) = sin/cos
tan² = sin²/cos²
tan² = (1-cos²)/cos² = 1/cos² - 1
1+tan² = 1/cos²
—> cos =±sqrt 1/(1+tan²) *

But, again, executing * requires (in a program) a much longer
execution time than the direct cos command.

Regards,
Gil
Find all posts by this user
Quote this message in a reply
09-04-2022, 10:07 AM
Post: #8
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
(09-04-2022 09:53 AM)Gil Wrote:  But using "your" formula for log(x), ie dividing ln(x) by 2.30258509299 [ln(10], the elapsed time is much longer than the direct use of the log command.

You're doing that by storing ln(10) as a constant, right? If you have to evaluate ln(10) each time then yes, of course, it's going to be much slower. Also don't forget that the division, as performed by the calculator internally, does not involve all the idiot filtering that constrains a keystroke RPN program.

Current daily driver: HP 71B w/FRAM 71B
Find all posts by this user
Quote this message in a reply
09-04-2022, 10:18 AM
Post: #9
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
(09-04-2022 09:50 AM)Paul Dale Wrote:  My experience with the 34S was that LN was **much** more difficult to compute than SIN or COS. On the 34S, TAN is computed as SIN / COS. However, SIN & COS are computed simultaneously and TAN is a single division overhead.

Even SIN and COS require division by that hypotenuse length that accumulates while rotating the vector!

Current daily driver: HP 71B w/FRAM 71B
Find all posts by this user
Quote this message in a reply
09-04-2022, 11:33 AM (This post was last modified: 09-04-2022 11:33 AM by Gil.)
Post: #10
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
Cos evaluation

Indirect formulae A:
« TIME 1 10000
START 3. TAN SQ 1 + ƒ INV DROP
NEXT TIME SWAP HMS- HMS 3600 * "s" TAG
»
Time
s: 3.207031

Direct from the calculator command: B:
« TIME 1 10000
START 3. COS DROP
NEXT TIME SWAP HMS- HMS 3600. * "s" TAG
»
Time
s: .715088000002 —> more than 4 times faster

It's clear from the above that "internal" calculation B reduces, logically, all the program manipulations shown in A.
Find all posts by this user
Quote this message in a reply
09-04-2022, 01:58 PM (This post was last modified: 09-04-2022 01:59 PM by John Keith.)
Post: #11
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
Similar differences in execution time apply to the 71B, 28 and 48 series which all use the Saturn processor.
I would guess that the same is true in Home mode on the Prime as its math functions are based on those of the Saturn-based calculators.

I am not familiar with the internal algorithms used for the various math functions but I imagine that there are some folks here that have that knowledge and hopefully they will chime in. The intrepidly curious could use Nosy to examine the internal functions although I get the impression that the code is anything but straightforward.
Find all posts by this user
Quote this message in a reply
09-04-2022, 02:20 PM
Post: #12
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
The algorithm for SIN/COS/TAN in the 48/49/50 is the following:

Code:

The absolute value of the argument is dbl word reduced 
by 2*Pi (or 360), then by Pi/2, and Pi/4 to obtain 
0<=Phi<=Pi/4.  A pseudo divide produces (X,Y) with 
0<=Y<=X and TAN(Phi) = Y/X .  Formulas;
       TAN(Phi) = Y/X
       SIN(Phi) = 1/SQRT(1+(X/Y)^2)
       COS(Phi) = 1/SQRT(1+(Y/X)^2)
Visit this user's website Find all posts by this user
Quote this message in a reply
09-04-2022, 02:25 PM (This post was last modified: 09-04-2022 02:42 PM by J-F Garnier.)
Post: #13
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
(09-03-2022 01:21 PM)Gil Wrote:  When executing loops for tan(x) on the emu48 with an android phone, we see that the average execution time for tan(x) on HP50G is 1.5 faster than for sin(x) or cos(x).

Shouldn't it be about the same speed (±10% instead of +50%)?

From the HP Saturn math source code (released HP-71B version):

** SINE, COSINE, & TANGENT
** [...] a pseudo divide produces (X,Y) with
** 0<=Y<=X and TAN(Phi) = Y/X. Formulas;
** TAN(Phi) = Y/X
** SIN(Phi) = 1/SQRT(1+(X/Y)^2)
** COS(Phi) = 1/SQRT(1+(Y/X)^2)



(09-03-2022 09:28 PM)Gil Wrote:  Other question on the same theme:
Why is ln(x) about 2.8 faster than sin(x), but log(x) only 2.1 faster than sin(x)?
This one is easier: because log(x) is calculated by log(x)=ln(x)/ln(10) i.e. one division more.

If you have an interest in the HP math algorithms, you can refer to the HP-71B IDS documents, where the source code is quite well commented (contrary to the HP-41 math in the VASM). The same algorithms were mainly unchanged in the next Saturn machines up to the series 48, 49 and 50, although the implementation may slightly varies.

Also you can refer to the historic article series about math algorithms in the HP Journals, for instance:
"Personal Calculator Algorithms IV: Logarithm Functions" in HP Journal, April 1978, p.29

J-F
(Eric beat me, apparently one of the few that have access to the series 48 source code :-)
Visit this user's website Find all posts by this user
Quote this message in a reply
09-04-2022, 06:41 PM
Post: #14
RE: HP49-50G : tan(x) 50% faster than sin(x) or cos(x)
Thanks all for your answers and references.
Regards,
Gil
Find all posts by this user
Quote this message in a reply
Post Reply 




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