Talk:Trigonometric functions: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Notes on precise sin implemented in Rexx: added comments about the REXX code last digit accuracy. -- ~~~~)
m (→‎Notes on precise sin implemented in Rexx: added the word ''decimal'' in front of digits (for precision) to help clarify what kind of digits. -- ~~~~)
Line 58: Line 58:


: That's what the REXX program shows. It adds 10 digits to the number of digits (precision) you want to see in the output.
: That's what the REXX program shows. It adds 10 digits to the number of digits (precision) you want to see in the output.
: Also, I long ago discovered that most trig and hyperbolic functions of this type usually require at least four extra digits to be used (but only for some values that are near asymptotic points and others near multiples or fractions of pi. So I added one more digits to be on the safe side. Since then, I made the addition to an even five for a few hyperbolic functions and then did the same to the trig functions, but used ten here (on Rosetta Code) for safety's sake. -- [[User:Gerard Schildberger|Gerard Schildberger]] 20:55, 22 June 2012 (UTC)
: Also, I long ago discovered that most trig and hyperbolic functions of this type usually require at least four extra (decimal) digits to be used (but only for some values that are near asymptotic points and others near multiples or fractions of pi. So I added one more digits to be on the safe side. Since then, I made the addition to an even five for a few hyperbolic functions and then did the same to the trig functions, but used ten here (on Rosetta Code) for safety's sake. -- [[User:Gerard Schildberger|Gerard Schildberger]] 20:55, 22 June 2012 (UTC)


I discussed this topic 10 years ago on Vladimir Zabrovsky's Album of (Rexx) algorithms which contains a wealth of code.
I discussed this topic 10 years ago on Vladimir Zabrovsky's Album of (Rexx) algorithms which contains a wealth of code.

Revision as of 20:59, 22 June 2012

Why are the last four pairs of numbers different for the ada example? The program was to use the same angle in degrees and radians so as long as the function is set for degrees or radians it should give the same number. --Mwn3d 07:14, 8 January 2008 (MST)

There is no error. 45 degrees is the same angle as 0.7854 (Pi/4) radians. A function set for radians cannot return degrees, and vice versa.--Waldorf 21:31, 8 January 2008 (MST)
Sorry. I think I was confused because the last two (arccot) seem to be exactly the same line of code. Am I crazy? --Mwn3d 21:36, 8 January 2008 (MST)
Theyŕe not exactly the same, but youŕe not crazy either. I think the line before New_Line; was intended to be
<code.Put(Item => Arccot(X => Cot(Angle_Radians, Radians_Cycle), Cycle => Radians_Cycle), Aft => 5, Exp => 0);
but what exists at present is not that. --TBH 22:16, 8 January 2008 (MST)

The Java example also seems to not fit the requirements for arcsin, arccos, and arctan. --TBH 11:16, 9 January 2008 (MST)

When I made the task I forgot that the arc functions return angles. Since they don't take an angle as an argument, I changed the Java example recently so that each function would print its answer in radians and degrees (even though they all start with different angles to get their arguments). I tried to make it more like the Ada example.--Mwn3d 11:38, 9 January 2008 (MST)
Ah! I had not noticed the clarified requirements. Now that I understand the requirements, I see that the answers are good for both the Ada and Java examples. The Perl code still needs fixing. The Ada code still has what seems to be an error, as I mentioned yesterday, but it does not affect the output. I've corrected the J code. --TBH 19:30, 9 January 2008 (MST)

odd formatting

I was trying to add code for IDL, but the "pre" tag doesn't seem to work quite like I expected. Can someone clarify?

Mediawiki's way of handling "pre" is broken. Try surrounding the "pre" block with <nowiki>/</nowiki>. --Short Circuit 01:00, 2 May 2008 (MDT)


Notes on precise sin implemented in Rexx

One of THE features of Rexx is the virtually unlimited numeric precision. Now if you want to get sin(0.1) with 30 precise digits (for whatever reason)

With Numeric Digits 30 sin as implemented in REXX trigonometric functions yields

  0.0998334166468281523068141984103

The implementation of sin in ooRexx' rxmath function yields

  0.09983341664682816   (this is restricted to 16 digits!)

when the precise value (50 digits) is

  0.099833416646828152306814198410622026989915388017978

which should be rounded to

  0.0998334166468281523068141984106

so both implementations above have an incorrect last digit

A possible remedy is to compute the function with a higher precision and then round the result to the desired one

ps=0.09983341664682815230681419841062199
   0.0998334166468281523068141984106

These lines come from

pSIN: procedure
/* Calculate sin(x) to the specified precision */
parse arg X, P
Numeric Digits (p+2)
ps=sin(x)
Say 'ps='ps
Numeric Digits p
Say '   '||(ps+0)
Return ps+0

--Walterpachl 19:43, 22 June 2012 (UTC)

Of course the Rexx implementation of sin could compute tthis higher precision and round it upon returning the value

That's what the REXX program shows. It adds 10 digits to the number of digits (precision) you want to see in the output.
Also, I long ago discovered that most trig and hyperbolic functions of this type usually require at least four extra (decimal) digits to be used (but only for some values that are near asymptotic points and others near multiples or fractions of pi. So I added one more digits to be on the safe side. Since then, I made the addition to an even five for a few hyperbolic functions and then did the same to the trig functions, but used ten here (on Rosetta Code) for safety's sake. -- Gerard Schildberger 20:55, 22 June 2012 (UTC)

I discussed this topic 10 years ago on Vladimir Zabrovsky's Album of (Rexx) algorithms which contains a wealth of code.

Did you change the REXX variable SHOWDIGS to 30? If so, I don't receive the same results you post. I assume you've must have cut and pasted from your own copy of the REXX program as the output format is much different. I would like to know what your results are for the REXX example as shown in Rosetta Code.


I get (for SHOWDIGS=30):

              rads= 0.1                              sin= 0.099833416646828152306814198    cos= 0.995004165278025766095561987    tan= 0.100334672085450545058080045

and the output (for SHOWDIGS=35):


              rads= 0.1                                   sin= 0.09983341664682815230681419841062    cos= 0.99500416527802576609556198780387    tan= 0.10033467208545054505808004578111

both of which required a modification to the REXX program; the code changes are: <lang rexx>/*REXX program demonstrates some common trig functions (30 digits shown)*/ showdigs=30 /*show only ten digits of number.*/ numeric digits showdigs+10 /*DIGITS default is 9, but use */

                                      /*extra digs to prevent rounding.*/

j=.1 /*just do 1/10 of a radian. for a special run. */

 stuff = '    '           '         rads='show(    (j)),
                                 '   sin='show(sin(j)),
                                 '   cos='show(cos(J))
                                           /*don't let  TAN  go postal.*/
 if abs(j)\==90 then stuff=stuff '   tan='show(tan(j))
 say stuff
.
.
.</lang>

This isn't probably the best forum to discuss these types of code (result) minutia when code is taken out of context or the modified code or driver code isn't shown to verify your results. I can be contacted via E-mail to iron out these details before large amounts of chatter are posted. -- Gerard Schildberger 20:55, 22 June 2012 (UTC)