sin(x)^2+cos(x)^2 = 1 on different calcs

09242018, 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:
Gives 1 for all values except 1, 6, 7, 9 where it gives 0.999999999999 as the answer. Python Code:
Gives: Code:
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:
Code:
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 :) 

09242018, 05:16 PM
Post: #2




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09242018 02:05 PM)tcab Wrote: I’ve just been playing around with truism that sin(x)^2+cos(x)^2 = 1. Not really; it just LOOKS like 1. Output sin(x)^2+cos(x)^21 instead, and you'll see where it wasn't really 1, as well as the size of the error (which is extremely small). It also depends on which angle mode you're in. <0ɸ0> Joe 

09242018, 05:39 PM
Post: #3




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
Hi,
You understand the limits of floating point, but dissapointed that it's not 1 in all cases? Now you may say "but Free42 always gets 1" You don't state which version of Free42 you're using. Probably "Decimal", in which case it is quadruple precision decimal floatingpoint which gives 34 decimal digits of precision. Also, it keeps all 34 digits with each calculation to reduce rounding error. So for the 12 displayed digits, there are 22 further digits not displayed. Try the following: COS(2*PI/3) + 0.5 On the Decimal version you'll get 3E34 Now for interest, subtract 1 from each result in your previous experiment. . 

09242018, 05:56 PM
(This post was last modified: 09242018 05:57 PM by pier4r.)
Post: #4




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
a search with the search string : site:www.hpmuseum.org accuracy OR precision on your favorite search engine returns a lot of useful discussion since the accuracy topic is one of the most discussed (and less understood) that I can remember.
Wikis are great, Contribute :) 

09242018, 06:12 PM
(This post was last modified: 09242018 06:14 PM by Albert Chan.)
Post: #5




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09242018 05:16 PM)Joe Horn Wrote: Not really; it just LOOKS like 1. Output sin(x)^2+cos(x)^21 instead, and you'll see where it wasn't really 1, as well as the size of the error (which is extremely small). It also depends on which angle mode you're in. Agreed. The "1" may be just a rounded number. However, subtract 1 to peek at the errors might not work. Some (most?) calculator, trying to be "nice", will convert the tiny amount to zero. http://people.ku.edu/~wzhuang/CAMseminar...ecrets.pdf Better approach is get around the trigger, and subtract, say 0.999999999 

09242018, 07:09 PM
(This post was last modified: 09242018 07:11 PM by Dieter.)
Post: #6




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09242018 02:05 PM)tcab Wrote: I’ve just been playing around with truism that sin(x)^2+cos(x)^2 = 1. There is nothing to be disappointed about. On the contrary, you should be glad that the result is not (!) 1 in all cases. Consider the case sin(6°) and cos(6°). sin(6°) = 0,104528463267653... cos(6°) = 0,994521895368273... Any decent 12digit calculator should return 0,104528463268 and 0,994521895368. The square of the sine is not much of a problem here. But since the cosine is rounded down, its square is also slightly below the true value, and here it shows in the 12 digit: 0,994521895368^2 = 0,989073800366359... or 0,989073800366 (rounded to 12 digits) while the true value is 0,994521895368273...^2 = 0,989073800366902... or 0,989073800367 with 12digit precision. So there are cases where the errors due to rounding to a given number of places compensate each other, while in others they add up and so they show up in the result. It simply depends on how the first discarded digit rounds. Both in the sine and cosine as well as in their squares. If a calculator actually returned 1 exactly in all cases I'd be concerned about its accuracy. ;) Dieter 

09252018, 04:22 AM
Post: #7




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
HP 30S returns exactly 1 for input
sin(.1)^2+cos(.1)^2 

09252018, 05:19 AM
Post: #8




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09252018 04:22 AM)Gerald H Wrote: HP 30S returns exactly 1 for input Indeed, sin(x)^2+cos(x)^21 returns 0 for every x I've tried. However: (09242018 07:09 PM)Dieter Wrote: If a calculator actually returned 1 exactly in all cases [for sin(x)^2+cos(x)^2] I'd be concerned about its accuracy. ;) Dieter is right. Besides, the 30S isn't really an HP calculator. It's a rebranded knockoff of somebody else's calculator. Proof: do 1/3, then Ans×3, then Ans1. It returns zero, which proves it's cheating by fudging results. <0ɸ0> Joe 

09252018, 02:35 PM
Post: #9




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09252018 05:19 AM)Joe Horn Wrote: Dieter is right. Besides, the 30S isn't really an HP calculator. It's a rebranded knockoff of somebody else's calculator. Agreed. Many (most?) calculators cheat, to give "nice" answer. They call this "small differences set to zero feature" ... I hate it I am not sure about the proof though. IIRC, 30S use binary mathematics: 1/3 = 0.010101011 (binary, say 8 bits mantissa) 2/3 = 0.10101011 3/3 = 1.000000001 > halfway roundtoeven = 1.0 1/3 = 0.01010101 (binary, say 7 bits mantissa) 2/3 = 0.1010101 3/3 = 0.11111111 > halfway roundtoeven = 1.0 So, whatever bits 30S uses (odd or even), 1/3*3 = 1, no cheating (in this case) 

09252018, 04:10 PM
(This post was last modified: 09262018 07:03 AM by JF Garnier.)
Post: #10




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09252018 02:35 PM)Albert Chan Wrote: I am not sure about the proof though. IIRC, 30S use binary mathematics: Yes, the 1/3*31 test doesn't work on binary machines. In this case, I'm using the test 1/3+111/3 to estimate the mantissa resolution (number of bits). BTW, The MS excel is using the "small differences set to zero feature" , but you can defeat it easily by using parentheses: e.g. =1/3+111/3 > 0 but =(1/3+111/3) > 5.55112E17 (2^54) JF [edited: corrected 2^54] 

09252018, 05:06 PM
Post: #11




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
Thanks, Albert and JF,! I didn't know that fact about binary machines. Makes perfect sense. Awesome!
<0ɸ0> Joe 

09262018, 05:27 AM
Post: #12




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
Hello,
In excel =10099.990.01 > 5.1157E15 shows that it does not "move small differences to 0.... However, I have NO clue what might be going on with =1/3+111/3 and =(1/3+111/3) This is a very interesting case! and I really would like to know what is happening behind the scenes! Cyrille Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP. 

09262018, 06:37 AM
Post: #13




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
Cyrille:
Same for =10.990.01 vs. =(10.990.01) It shows Excel does cheat, but that can never be done consistently. They should've adopted decimal floating point a long time ago. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE 

09262018, 07:37 AM
Post: #14




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09262018 05:27 AM)cyrille de brébisson Wrote: However, I have NO clue what might be going on with 1/3 = 0.0101010101... (binary) 1/3+1 = 1.01010101... (shift the mantissa right by 2 bits, loosing one bit at the mantissa end) 1/3+11 = 0.01010101... ( one bit is 0 at mantissa end) so the final difference 1/3+111/3 is one bit. Quote:=10099.990.01 > 5.1157E15This case is more complex, because 99.99 and 0.01 doesn't have simple binary representations. The difference 5.1157E15 has a mantissa with several bits set (more than two). (09262018 06:37 AM)Werner Wrote: Cyrille: =(10.990.01) > 8.67362E18 = 2^57+2^59 Maybe excel is cheating when the mantissa has up to 2 bits close to the end. And I'm happy that excel is using binary arithmetic, I never encountered such situations in real life but had to deal with very large and complex spreadsheets to do system simulations, and speed was the main point. JF 

09262018, 09:58 AM
Post: #15




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09262018 07:37 AM)JF Garnier Wrote: And I'm happy that excel is using binary arithmetic, I never encountered such situations in real life but had to deal with very large and complex spreadsheets to do system simulations, and speed was the main point. ...but when calculating with money, decimal fractions really should be exact, and spreadsheets are very commonly used for calculations involving money (which, presumably, is why Excel tries to cheat). 

09262018, 10:31 AM
Post: #16




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09262018 09:58 AM)Thomas Okken Wrote: ...but when calculating with money, decimal fractions really should be exact, and spreadsheets are very commonly used for calculations involving money (which, presumably, is why Excel tries to cheat). My reasoning exactly, Thomas. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE 

09262018, 06:27 PM
Post: #17




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
(09262018 05:27 AM)cyrille de brébisson Wrote: =10099.990.01 > 5.1157E15 The difference is not small, if think in ULPs (above case, 1 ULP = 2^59): 100  float(99.99) = 0x147ae147ae2000 ULP float(0.01) = 0x147ae147ae147b ULP Difference = 0x2000  0x147b = 2949 ULP Quote:However, I have NO clue what might be going on with I am using *old* MS Excel 95. It does not have settozero "feature". So, both expressions return the same 5.6E17 

09272018, 12:20 PM
(This post was last modified: 09272018 12:21 PM by Csaba Tizedes.)
Post: #18




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
I dunno how its relevant: on my CASIO fx991ES SUM(SIN(X)^2+COS(X)^21,0,90) gives 0, SUM(SIN(X)^2+COS(X)^2,0,90)91 also 0, SUM(SIN(X)^2,0,90)+SUM(COS(X)^2,0,90)91 same (0). After then I used integration and the result is also zero! Wow! My CASIO is perfect.


09272018, 03:34 PM
(This post was last modified: 09272018 06:26 PM by Albert Chan.)
Post: #19




RE: sin(x)^2+cos(x)^2 = 1 on different calcs
Hi, Csaba Tizedes
You got a big 0 for sums because of another Casio "feature". For example, below test from my CasioFX115MS, which claimed 12 internal digits. 9999999999 >M (M has full 10 digits) M + 0.94  M ==> 0.94 M + 0.95  M ==> 1 ??? M + 0.10  M ==> 0.10 M + 0.09  M ==> 0 ??? So, sin(X)^2 + cos(X)^2 get rounded to exactly 1 (Almost always) This, on top of small difference settozero "feature" Edit: roundup feature only for M with all 9s. Do not know why ... 

09282018, 07:16 AM
Post: #20




RE: sin(x)^2+cos(x)^2 = 1 on different calcs  
« Next Oldest  Next Newest »

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