The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
sin(x)^2+cos(x)^2 = 1 on different calcs
09-24-2018, 02:05 PM
Post: #1
sin(x)^2+cos(x)^2 = 1 on different calcs
I’ve just been playing around with truism that sin(x)^2+cos(x)^2 = 1.

I ran a loop checking this for different values of x on different calcs, incl Python. Let’s say x ranges from 0..20.

I expected the result to be 1 in all cases but of course such is the nature of floating point that some results are 1 or 0.999999999999 or 1.0000000000000002 thus disappointingly, not exactly 1 in all cases.

The interesting thing is that Python, Prime and Free42 give different results, and the ‘errors’ occur at different points, presumably due to the different math/floating point engines. The only calc that had no errors was Free42, which gave 1 in all cases.

Hp Prime
Code:

SINCOS(X)
BEGIN
  RETURN SIN(X)^2+COS(X)^2;
END;

EXPORT SINCOSLOOP()
BEGIN
LOCAL I;
FOR I FROM 0 TO 20 DO
  PRINT(I + " " + SINCOS(I));
END;
END;

Gives 1 for all values except 1, 6, 7, 9 where it gives 0.999999999999 as the answer.

Python
Code:

import math
def sincos(x):
    res = math.sin(x)**2 + math.cos(x)**2
    return res
    
for i in range(20):
    print(i, sincos(i))

Gives:
Code:

0 1.0
1 1.0
2 1.0
3 0.9999999999999999
4 1.0
5 0.9999999999999999
6 0.9999999999999999
7 0.9999999999999999
8 1.0
9 0.9999999999999999
10 1.0
11 1.0
12 1.0
13 1.0
14 1.0
15 1.0
16 1.0
17 0.9999999999999999
18 1.0
19 0.9999999999999999

Reassuringly, Free42 gives 1 in all cases. I used my python to rpn converter https://pyrpn.herokuapp.com to create the Free42 program, since I already had the Python code worked out. I just had to move the for loop before the sincos function, and change math.sin to SIN.

Code:

LBL("sincosloop")
for i in range(20):
    print(i, sincos(i))
def sincos(x):
    res = SIN(x)**2 + COS(x)**2
    return res
which converted into the HP 42S RPN of
Code:

01 LBL "sincosl"
02 -1.19
03 STO 00
04 LBL 00
05 ISG 00
06 GTO 01
07 GTO 02
08 LBL 01
09 CLA
10 RCL 00
11 IP
12 ARCL ST X
13 ├" "
14 RCL 00
15 IP
16 XEQ A
17 ARCL ST X
18 AVIEW
19 GTO 00
20 LBL 02
21 RTN
22 LBL A
23 STO 01
24 RDN
25 RCL 01
26 SIN
28 X↑2
29 RCL 01
30 COS
32 X↑2
33 +
34 STO 02
35 RCL 02
36 RTN

which when run, shows 1 in all cases, and if virtual printing is on with PON you get to see the list of values on the virtual printer tape, confirming this.

An interesting evening’s research :-)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
sin(x)^2+cos(x)^2 = 1 on different calcs - tcab - 09-24-2018 02:05 PM



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