Check Machin-like formulas: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: aligned the input and output better, added a title that show how many decimal digits are bing used for the calculations.)
Line 1,869: Line 1,869:


=={{header|REXX}}==
=={{header|REXX}}==
Note:   REXX doesn't have many math functions,   so a few of them are included here.
Note:   REXX doesn't have many high─order math functions,   so a few of them are included here.


Noticed:   the test arguments specified for this Rosetta Code task need only '''nine''' decimal digits for verification,   '''eight''' decimal digits is   ''not''   enough to catch the "bad" equation.   With this in mind, the REXX's   ''decimal digit precision''   was increased to the number of decimal digits specified for the variable   '''pi'''       (which, for these cases, is a bit of overkill, but the difference in execution times were barely noticeable).
Noticed:   the test arguments specified for this Rosetta Code task need only '''nine''' decimal digits for verification,   '''eight''' decimal digits is   ''not''   enough to catch the "bad" equation.   With this in mind, the REXX's   ''decimal digit precision''   was increased to the number of decimal digits specified for the variable   '''pi'''       (which, for these cases, is a bit of overkill, but the difference in execution times were barely noticeable).
Line 1,875: Line 1,875:
An extra formula was added to stress test the near exactness of a value.
An extra formula was added to stress test the near exactness of a value.
<lang rexx>/*REXX program evaluates some Machin─like formulas and verifies their veracity. */
<lang rexx>/*REXX program evaluates some Machin─like formulas and verifies their veracity. */
@.=; pi= pi(); numeric digits( length(pi) ) - length(.); numeric fuzz 3
@.=; pi= pi(); numeric digits( length(pi) ) - length(.); numeric fuzz 3
say center(' computing with ' digits() " decimal digits ", 110, '═')
@.1 = 'pi/4 = atan(1/2) + atan(1/3)'
@.1 = 'pi/4 = atan(1/2) + atan(1/3)'
@.2 = 'pi/4 = 2*atan(1/3) + atan(1/7)'
@.2 = 'pi/4 = 2*atan(1/3) + atan(1/7)'
Line 1,891: Line 1,892:
@.14= 'pi/4 = 22*atan(1/38) + 17*atan(7/601) + 10*atan(7/8149)'
@.14= 'pi/4 = 22*atan(1/38) + 17*atan(7/601) + 10*atan(7/8149)'
@.15= 'pi/4 = 44*atan(1/57) + 7*atan(1/239) - 12*atan(1/682) + 24*atan(1/12943)'
@.15= 'pi/4 = 44*atan(1/57) + 7*atan(1/239) - 12*atan(1/682) + 24*atan(1/12943)'
@.16= 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12943)'
@.16= 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68 *atan(1/12943)'
@.17= 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12944)'
@.17= 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68 *atan(1/12944)'
@.18= 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 67.9999999994*atan(1/12943)'
@.18= 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 67.9999999994*atan(1/12943)'


Line 1,911: Line 1,912:
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; h=d+6; numeric form
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; h=d+6; numeric form
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</lang>
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
<pre>
════════════════════════════════════ computing with 76 decimal digits ════════════════════════════════════
OK: pi/4 = atan(1/2) + atan(1/3)
OK: pi/4 = atan(1/2) + atan(1/3)
OK: pi/4 = 2*atan(1/3) + atan(1/7)
OK: pi/4 = 2*atan(1/3) + atan(1/7)
Line 1,930: Line 1,932:
OK: pi/4 = 22*atan(1/38) + 17*atan(7/601) + 10*atan(7/8149)
OK: pi/4 = 22*atan(1/38) + 17*atan(7/601) + 10*atan(7/8149)
OK: pi/4 = 44*atan(1/57) + 7*atan(1/239) - 12*atan(1/682) + 24*atan(1/12943)
OK: pi/4 = 44*atan(1/57) + 7*atan(1/239) - 12*atan(1/682) + 24*atan(1/12943)
OK: pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12943)
OK: pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68 *atan(1/12943)
bad: pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12944)
bad: pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68 *atan(1/12944)
bad: pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 67.9999999994*atan(1/12943)
bad: pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 67.9999999994*atan(1/12943)
</pre>
</pre>