Check Machin-like formulas: Difference between revisions

m
(added FreeBASIC)
m (→‎{{header|Wren}}: Minor tidy)
 
(29 intermediate revisions by 16 users not shown)
Line 24:
: <math>{\pi\over4} = 88 \arctan{1\over172} + 51 \arctan{1\over239} + 32 \arctan{1\over682} + 44 \arctan{1\over5357} + 68 \arctan{1\over12943}</math>
 
and confirm that the following formula is ''incorrect'' by showing &nbsp; '''tan''' &nbsp; (''right hand side)'' &nbsp; is ''not'' &nbsp; '''1''':
 
: <math>{\pi\over4} = 88 \arctan{1\over172} + 51 \arctan{1\over239} + 32 \arctan{1\over682} + 44 \arctan{1\over5357} + 68 \arctan{1\over12944}</math>
Line 41:
 
<br><br>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
{{libheader|GMP}}
<syntaxhighlight lang="freebasic">' version 07-04-2018
' compile with: fbc -s console
 
#Include "gmp.bi"
 
#Define _a(Q) (@(Q)->_mp_num) 'a
#Define _b(Q) (@(Q)->_mp_den) 'b
 
Data "[1, 1, 2] [1, 1, 3]"
Data "[2, 1, 3] [1, 1, 7]"
Data "[4, 1, 5] [-1, 1, 239]"
Data "[5, 1, 7] [2, 3, 79]"
Data "[1, 1, 2] [1, 1, 5] [1, 1, 8]"
Data "[4, 1, 5] [-1, 1, 70] [1, 1, 99]"
Data "[5, 1, 7] [4, 1, 53] [2, 1, 4443]"
Data "[6, 1, 8] [2, 1, 57] [1, 1, 239]"
Data "[8, 1, 10] [-1, 1, 239] [-4, 1, 515]"
Data "[12, 1, 18] [8, 1, 57] [-5, 1, 239]"
Data "[16, 1, 21] [3, 1, 239] [4, 3, 1042]"
Data "[22, 1, 28] [2, 1, 443] [-5, 1, 1393] [-10, 1, 11018]"
Data "[22, 1, 38] [17, 7, 601] [10, 7, 8149]"
Data "[44, 1, 57] [7, 1, 239] [-12, 1, 682] [24, 1, 12943]"
Data "[88, 1, 172] [51, 1, 239] [32, 1, 682] [44, 1, 5357] [68, 1, 12943]"
Data "[88, 1, 172] [51, 1, 239] [32, 1, 682] [44, 1, 5357] [68, 1, 12944]"
Data ""
 
Sub work2do (ByRef a As LongInt, f1 As mpq_ptr)
 
Dim As LongInt flag = -1
 
Dim As Mpq_ptr x, y, z
x = Allocate(Len(__mpq_struct)) : Mpq_init(x)
y = Allocate(Len(__mpq_struct)) : Mpq_init(y)
z = Allocate(Len(__mpq_struct)) : Mpq_init(z)
 
Dim As Mpz_ptr temp1, temp2
temp1 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp1)
temp2 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp2)
 
mpq_set(y, f1)
 
While a > 0
If (a And 1) = 1 Then
If flag = -1 Then
mpq_set(x, y)
flag = 0
Else
Mpz_mul(temp1, _a(x), _b(y))
Mpz_mul(temp2, _b(x), _a(y))
Mpz_add(_a(z), temp1, temp2)
Mpz_mul(temp1, _b(x), _b(y))
Mpz_mul(temp2, _a(x), _a(y))
Mpz_sub(_b(z), temp1, temp2)
mpq_canonicalize(z)
mpq_set(x, z)
End If
End If
 
Mpz_mul(temp1, _a(y), _b(y))
Mpz_mul(temp2, _b(y), _a(y))
Mpz_add(_a(z), temp1, temp2)
Mpz_mul(temp1, _b(y), _b(y))
Mpz_mul(temp2, _a(y), _a(y))
Mpz_sub(_b(z), temp1, temp2)
mpq_canonicalize(z)
mpq_set(y, z)
 
a = a Shr 1
Wend
 
mpq_set(f1, x)
 
End Sub
 
' ------=< MAIN >=------
 
Dim As Mpq_ptr f1, f2, f3
f1 = Allocate(Len(__mpq_struct)) : Mpq_init(f1)
f2 = Allocate(Len(__mpq_struct)) : Mpq_init(f2)
f3 = Allocate(Len(__mpq_struct)) : Mpq_init(f3)
 
Dim As Mpz_ptr temp1, temp2
temp1 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp1)
temp2 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp2)
 
Dim As mpf_ptr float
float = Allocate(Len(__mpf_struct)) : Mpf_init(float)
 
Dim As LongInt m1, a1, b1, flag, t1, t2, t3, t4
Dim As String s, s1, s2, s3, sign
Dim As ZString Ptr zstr
 
Do
 
Read s
If s = "" Then Exit Do
flag = -1
 
While s <> ""
t1 = InStr(s, "[") +1
t2 = InStr(t1, s, ",") +1
t3 = InStr(t2, s, ",") +1
t4 = InStr(t3, s, "]")
s1 = Trim(Mid(s, t1, t2 - t1 -1))
s2 = Trim(Mid(s, t2, t3 - t2 -1))
s3 = Trim(Mid(s, t3, t4 - t3))
m1 = Val(s1)
a1 = Val(s2)
b1 = Val(s3)
 
sign = IIf(m1 < 0, " - ", " + ")
If m1 < 0 Then a1 = -a1 : m1 = Abs(m1)
s = Mid(s, t4 +1)
Print IIf(flag = 0, sign, ""); IIf(m1 = 1, "", Str(m1));
Print "Atn("; s2; "/" ;s3; ")";
 
If flag = -1 Then
flag = 0
Mpz_set_si(_a(f1), a1)
Mpz_set_si(_b(f1), b1)
If m1 > 1 Then work2do(m1, f1)
Continue While
End If
 
Mpz_set_si(_a(f2), a1)
Mpz_set_si(_b(f2), b1)
If m1 > 1 Then work2do(m1, f2)
 
Mpz_mul(temp1, _a(f1), _b(f2))
Mpz_mul(temp2, _b(f1), _a(f2))
Mpz_add(_a(f3), temp1, temp2)
Mpz_mul(temp1, _b(f1), _b(f2))
Mpz_mul(temp2, _a(f1), _a(f2))
Mpz_sub(_b(f3), temp1, temp2)
mpq_canonicalize(f3)
mpq_set(f1, f3)
 
Wend
 
If Mpz_cmp_ui(_b(f1), 1) = 0 AndAlso Mpz_cmp(_a(f1), _b(f1)) = 0 Then
Print " = 1"
Else
Mpf_set_q(float, f1)
gmp_printf(!" = %.*Ff\n", 15, float)
End If
 
Loop
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>Atn(1/2) + Atn(1/3) = 1
2Atn(1/3) + Atn(1/7) = 1
4Atn(1/5) - Atn(1/239) = 1
5Atn(1/7) + 2Atn(3/79) = 1
Atn(1/2) + Atn(1/5) + Atn(1/8) = 1
4Atn(1/5) - Atn(1/70) + Atn(1/99) = 1
5Atn(1/7) + 4Atn(1/53) + 2Atn(1/4443) = 1
6Atn(1/8) + 2Atn(1/57) + Atn(1/239) = 1
8Atn(1/10) - Atn(1/239) - 4Atn(1/515) = 1
12Atn(1/18) + 8Atn(1/57) - 5Atn(1/239) = 1
16Atn(1/21) + 3Atn(1/239) + 4Atn(3/1042) = 1
22Atn(1/28) + 2Atn(1/443) - 5Atn(1/1393) - 10Atn(1/11018) = 1
22Atn(1/38) + 17Atn(7/601) + 10Atn(7/8149) = 1
44Atn(1/57) + 7Atn(1/239) - 12Atn(1/682) + 24Atn(1/12943) = 1
88Atn(1/172) + 51Atn(1/239) + 32Atn(1/682) + 44Atn(1/5357) + 68Atn(1/12943) = 1
88Atn(1/172) + 51Atn(1/239) + 32Atn(1/682) + 44Atn(1/5357) + 68Atn(1/12944) = 0.999999188225744</pre>
 
==={{header|Visual Basic .NET}}===
'''BigRat''' class based on the Arithmetic/Rational#C here at Rosetta Code.<br/>
The parser here allows for some flexibility in the input text. Case is ignored, and a variable number of spaces are allowed. Atan(), arctan(), atn() are all recognized as valid. If one of those three are not found, a warning will appear. The coefficient need not have a multiplication sign between it and the "arctan()". The left side of the equation must be pi / 4, otherwise a warning will appear.
<syntaxhighlight lang="vbnet">Imports System.Numerics
 
Public Class BigRat ' Big Rational Class constructed with BigIntegers
Implements IComparable
Public nu, de As BigInteger
Public Shared Zero = New BigRat(BigInteger.Zero, BigInteger.One),
One = New BigRat(BigInteger.One, BigInteger.One)
Sub New(bRat As BigRat)
nu = bRat.nu : de = bRat.de
End Sub
Sub New(n As BigInteger, d As BigInteger)
If d = BigInteger.Zero Then _
Throw (New Exception(String.Format("tried to set a BigRat with ({0}/{1})", n, d)))
Dim bi As BigInteger = BigInteger.GreatestCommonDivisor(n, d)
If bi > BigInteger.One Then n /= bi : d /= bi
If d < BigInteger.Zero Then n = -n : d = -d
nu = n : de = d
End Sub
Shared Operator -(x As BigRat) As BigRat
Return New BigRat(-x.nu, x.de)
End Operator
Shared Operator +(x As BigRat, y As BigRat)
Return New BigRat(x.nu * y.de + x.de * y.nu, x.de * y.de)
End Operator
Shared Operator -(x As BigRat, y As BigRat) As BigRat
Return x + (-y)
End Operator
Shared Operator *(x As BigRat, y As BigRat) As BigRat
Return New BigRat(x.nu * y.nu, x.de * y.de)
End Operator
Shared Operator /(x As BigRat, y As BigRat) As BigRat
Return New BigRat(x.nu * y.de, x.de * y.nu)
End Operator
Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
Dim dif As BigRat = New BigRat(nu, de) - obj
If dif.nu < BigInteger.Zero Then Return -1
If dif.nu > BigInteger.Zero Then Return 1
Return 0
End Function
Shared Operator =(x As BigRat, y As BigRat) As Boolean
Return x.CompareTo(y) = 0
End Operator
Shared Operator <>(x As BigRat, y As BigRat) As Boolean
Return x.CompareTo(y) <> 0
End Operator
Overrides Function ToString() As String
If de = BigInteger.One Then Return nu.ToString
Return String.Format("({0}/{1})", nu, de)
End Function
Shared Function Combine(a As BigRat, b As BigRat) As BigRat
Return (a + b) / (BigRat.One - (a * b))
End Function
End Class
 
Public Structure Term ' coefficent, BigRational construction for each term
Dim c As Integer, br As BigRat
Sub New(cc As Integer, bigr As BigRat)
c = cc : br = bigr
End Sub
End Structure
 
Module Module1
Function Eval(c As Integer, x As BigRat) As BigRat
If c = 1 Then Return x Else If c < 0 Then Return Eval(-c, -x)
Dim hc As Integer = c \ 2
Return BigRat.Combine(Eval(hc, x), Eval(c - hc, x))
End Function
 
Function Sum(terms As List(Of Term)) As BigRat
If terms.Count = 1 Then Return Eval(terms(0).c, terms(0).br)
Dim htc As Integer = terms.Count / 2
Return BigRat.Combine(Sum(terms.Take(htc).ToList), Sum(terms.Skip(htc).ToList))
End Function
 
Function ParseLine(ByVal s As String) As List(Of Term)
ParseLine = New List(Of Term) : Dim t As String = s.ToLower, p As Integer, x As New Term(1, BigRat.Zero)
While t.Contains(" ") : t = t.Replace(" ", "") : End While
p = t.IndexOf("pi/4=") : If p < 0 Then _
Console.WriteLine("warning: tan(left side of equation) <> 1") : ParseLine.Add(x) : Exit Function
t = t.Substring(p + 5)
For Each item As String In t.Split(")")
If item.Length > 5 Then
If (Not item.Contains("tan") OrElse item.IndexOf("a") < 0 OrElse
item.IndexOf("a") > item.IndexOf("tan")) AndAlso Not item.Contains("atn") Then
Console.WriteLine("warning: a term is mising a valid arctangent identifier on the right side of the equation: [{0})]", item)
ParseLine = New List(Of Term) : ParseLine.Add(New Term(1, BigRat.Zero)) : Exit Function
End If
x.c = 1 : x.br = New BigRat(BigRat.One)
p = item.IndexOf("/") : If p > 0 Then
x.br.de = UInt64.Parse(item.Substring(p + 1))
item = item.Substring(0, p)
p = item.IndexOf("(") : If p > 0 Then
x.br.nu = UInt64.Parse(item.Substring(p + 1))
p = item.IndexOf("a") : If p > 0 Then
Integer.TryParse(item.Substring(0, p).Replace("*", ""), x.c)
If x.c = 0 Then x.c = 1
If item.Contains("-") AndAlso x.c > 0 Then x.c = -x.c
End If
ParseLine.Add(x)
End If
End If
End If
Next
End Function
 
Sub Main(ByVal args As String())
Dim nl As String = vbLf
For Each item In ("pi/4 = ATan(1 / 2) + ATan(1/3)" & nl &
"pi/4 = 2Atan(1/3) + ATan(1/7)" & nl &
"pi/4 = 4ArcTan(1/5) - ATan(1 / 239)" & nl &
"pi/4 = 5arctan(1/7) + 2 * atan(3/79)" & nl &
"Pi/4 = 5ATan(29/278) + 7*ATan(3/79)" & nl &
"pi/4 = atn(1/2) + ATan(1/5) + ATan(1/8)" & nl &
"PI/4 = 4ATan(1/5) - Atan(1/70) + ATan(1/99)" & nl &
"pi /4 = 5*ATan(1/7) + 4 ATan(1/53) + 2ATan(1/4443)" & nl &
"pi / 4 = 6ATan(1/8) + 2arctangent(1/57) + ATan(1/239)" & nl &
"pi/ 4 = 8ATan(1/10) - ATan(1/239) - 4ATan(1/515)" & nl &
"pi/4 = 12ATan(1/18) + 8ATan(1/57) - 5ATan(1/239)" & nl &
"pi/4 = 16 * ATan(1/21) + 3ATan(1/239) + 4ATan(3/1042)" & nl &
"pi/4 = 22ATan(1/28) + 2ATan(1/443) - 5ATan(1/1393) - 10 ATan( 1 / 11018 )" & nl &
"pi/4 = 22ATan(1/38) + 17ATan(7/601) + 10ATan(7 / 8149)" & nl &
"pi/4 = 44ATan(1/57) + 7ATan(1/239) - 12ATan(1/682) + 24ATan(1/12943)" & nl &
"pi/4 = 88ATan(1/172) + 51ATan(1/239) + 32ATan(1/682) + 44ATan(1/5357) + 68ATan(1/12943)" & nl &
"pi/4 = 88ATan(1/172) + 51ATan(1/239) + 32ATan(1/682) + 44ATan(1/5357) + 68ATan(1/12944)").Split(nl)
Console.WriteLine("{0}: {1}", If(Sum(ParseLine(item)) = BigRat.One, "Pass", "Fail"), item)
Next
End Sub
End Module</syntaxhighlight>
{{out}}
<pre>Pass: pi/4 = ATan(1 / 2) + ATan(1/3)
Pass: pi/4 = 2Atan(1/3) + ATan(1/7)
Pass: pi/4 = 4ArcTan(1/5) - ATan(1 / 239)
Pass: pi/4 = 5arctan(1/7) + 2 * atan(3/79)
Pass: pi/4 = 5ATan(29/278) + 7*ATan(3/79)
Pass: pi/4 = atn(1/2) + ATan(1/5) + ATan(1/8)
Pass: pi/4 = 4ATan(1/5) - Atan(1/70) + ATan(1/99)
Pass: pi /4 = 5*ATan(1/7) + 4 ATan(1/53) + 2ATan(1/4443)
Pass: pi / 4 = 6ATan(1/8) + 2arctangent(1/57) + ATan(1/239)
Pass: pi/ 4 = 8ATan(1/10) - ATan(1/239) - 4ATan(1/515)
Pass: pi/4 = 12ATan(1/18) + 8ATan(1/57) - 5ATan(1/239)
Pass: pi/4 = 16 * ATan(1/21) + 3ATan(1/239) + 4ATan(3/1042)
Pass: pi/4 = 22ATan(1/28) + 2ATan(1/443) - 5ATan(1/1393) - 10 ATan( 1 / 11018 )
Pass: pi/4 = 22ATan(1/38) + 17ATan(7/601) + 10ATan(7 / 8149)
Pass: pi/4 = 44ATan(1/57) + 7ATan(1/239) - 12ATan(1/682) + 24ATan(1/12943)
Pass: pi/4 = 88ATan(1/172) + 51ATan(1/239) + 32ATan(1/682) + 44ATan(1/5357) + 68ATan(1/12943)
Fail: pi/4 = 88ATan(1/172) + 51ATan(1/239) + 32ATan(1/682) + 44ATan(1/5357) + 68ATan(1/12944)</pre>
 
=={{header|Clojure}}==
Clojure automatically handles ratio of numbers as fractions
{{trans|Go}}
<langsyntaxhighlight lang="lisp">(ns tanevaulator
(:gen-class))
 
Line 97 ⟶ 421:
" Display results "
(println "tan " q " = "(tans q)))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 120 ⟶ 444:
(equals 0.9999991882257445)
</pre>
 
=={{header|D}}==
This uses the module of the Arithmetic Rational Task.
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.regex, std.conv, std.string, std.range,
arithmetic_rational;
 
Line 194 ⟶ 517:
writefln("%5s: %s", ans == 1 ? "OK" : "ERROR", eqn);
}
}</langsyntaxhighlight>
{{out}}
<pre> OK: pi/4 = arctan(1/2) + arctan(1/3)
Line 213 ⟶ 536:
OK: pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)
ERROR: pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)</pre>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'math)
(lib 'match)
Line 274 ⟶ 596:
(writeln '❌ f '➽ (reduce f) ))))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
 
(define machins
Line 321 ⟶ 643:
(* 44 (arctan 1/5357)) (* 68 (arctan 1/12944)))) ➽ 0.9999991882257442
 
</syntaxhighlight>
</lang>
=={{header|FreeBASICFactor}}==
<syntaxhighlight lang="factor">USING: combinators formatting kernel locals math sequences ;
{{libheader|GMP}}
IN: rosetta-code.machin
<lang freebasic>' version 07-04-2018
' compile with: fbc -s console
 
: tan+ ( x y -- z ) [ + ] [ * 1 swap - / ] 2bi ;
#Include "gmp.bi"
 
:: tan-eval ( coef frac -- x )
#Define _a(Q) (@(Q)->_mp_num) 'a
{
#Define _b(Q) (@(Q)->_mp_den) 'b
{ [ coef zero? ] [ 0 ] }
{ [ coef neg? ] [ coef neg frac tan-eval neg ] }
{ [ coef odd? ] [ frac coef 1 - frac tan-eval tan+ ] }
[ coef 2/ frac tan-eval dup tan+ ]
} cond ;
 
: tans ( seq -- x ) [ first2 tan-eval ] [ tan+ ] map-reduce ;
Data "[1, 1, 2] [1, 1, 3]"
Data "[2, 1, 3] [1, 1, 7]"
Data "[4, 1, 5] [-1, 1, 239]"
Data "[5, 1, 7] [2, 3, 79]"
Data "[1, 1, 2] [1, 1, 5] [1, 1, 8]"
Data "[4, 1, 5] [-1, 1, 70] [1, 1, 99]"
Data "[5, 1, 7] [4, 1, 53] [2, 1, 4443]"
Data "[6, 1, 8] [2, 1, 57] [1, 1, 239]"
Data "[8, 1, 10] [-1, 1, 239] [-4, 1, 515]"
Data "[12, 1, 18] [8, 1, 57] [-5, 1, 239]"
Data "[16, 1, 21] [3, 1, 239] [4, 3, 1042]"
Data "[22, 1, 28] [2, 1, 443] [-5, 1, 1393] [-10, 1, 11018]"
Data "[22, 1, 38] [17, 7, 601] [10, 7, 8149]"
Data "[44, 1, 57] [7, 1, 239] [-12, 1, 682] [24, 1, 12943]"
Data "[88, 1, 172] [51, 1, 239] [32, 1, 682] [44, 1, 5357] [68, 1, 12943]"
Data "[88, 1, 172] [51, 1, 239] [32, 1, 682] [44, 1, 5357] [68, 1, 12944]"
Data ""
 
: machin ( -- )
Sub work2do (ByRef a As LongInt, f1 As mpq_ptr)
{
{ { 1 1/2 } { 1 1/3 } }
{ { 2 1/3 } { 1 1/7 } }
{ { 4 1/5 } { -1 1/239 } }
{ { 5 1/7 } { 2 3/79 } }
{ { 5 29/278 } { 7 3/79 } }
{ { 1 1/2 } { 1 1/5 } { 1 1/8 } }
{ { 5 1/7 } { 4 1/53 } { 2 1/4443 } }
{ { 6 1/8 } { 2 1/57 } { 1 1/239 } }
{ { 8 1/10 } { -1 1/239 } { -4 1/515 } }
{ { 12 1/18 } { 8 1/57 } { -5 1/239 } }
{ { 16 1/21 } { 3 1/239 } { 4 3/1042 } }
{ { 22 1/28 } { 2 1/443 }
{ -5 1/1393 } { -10 1/11018 } }
{ { 22 1/38 } { 17 7/601 } { 10 7/8149 } }
{ { 44 1/57 } { 7 1/239 } { -12 1/682 } { 24 1/12943 } }
{ { 88 1/172 } { 51 1/239 } { 32 1/682 }
{ 44 1/5357 } { 68 1/12943 } }
{ { 88 1/172 } { 51 1/239 } { 32 1/682 }
{ 44 1/5357 } { 68 1/12944 } }
} [ dup tans "tan %u = %u\n" printf ] each ;
 
MAIN: machin</syntaxhighlight>
Dim As LongInt flag = -1
 
Dim As Mpq_ptr x, y, z
x = Allocate(Len(__mpq_struct)) : Mpq_init(x)
y = Allocate(Len(__mpq_struct)) : Mpq_init(y)
z = Allocate(Len(__mpq_struct)) : Mpq_init(z)
 
Dim As Mpz_ptr temp1, temp2
temp1 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp1)
temp2 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp2)
 
mpq_set(y, f1)
 
While a > 0
If (a And 1) = 1 Then
If flag = -1 Then
mpq_set(x, y)
flag = 0
Else
Mpz_mul(temp1, _a(x), _b(y))
Mpz_mul(temp2, _b(x), _a(y))
Mpz_add(_a(z), temp1, temp2)
Mpz_mul(temp1, _b(x), _b(y))
Mpz_mul(temp2, _a(x), _a(y))
Mpz_sub(_b(z), temp1, temp2)
mpq_canonicalize(z)
mpq_set(x, z)
End If
End If
 
Mpz_mul(temp1, _a(y), _b(y))
Mpz_mul(temp2, _b(y), _a(y))
Mpz_add(_a(z), temp1, temp2)
Mpz_mul(temp1, _b(y), _b(y))
Mpz_mul(temp2, _a(y), _a(y))
Mpz_sub(_b(z), temp1, temp2)
mpq_canonicalize(z)
mpq_set(y, z)
 
a = a Shr 1
Wend
 
mpq_set(f1, x)
 
End Sub
 
' ------=< MAIN >=------
 
Dim As Mpq_ptr f1, f2, f3
f1 = Allocate(Len(__mpq_struct)) : Mpq_init(f1)
f2 = Allocate(Len(__mpq_struct)) : Mpq_init(f2)
f3 = Allocate(Len(__mpq_struct)) : Mpq_init(f3)
 
Dim As Mpz_ptr temp1, temp2
temp1 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp1)
temp2 = Allocate(Len(__Mpz_struct)) : Mpz_init(temp2)
 
Dim As mpf_ptr float
float = Allocate(Len(__mpf_struct)) : Mpf_init(float)
 
Dim As LongInt m1, a1, b1, flag, t1, t2, t3, t4
Dim As String s, s1, s2, s3, sign
Dim As ZString Ptr zstr
 
Do
 
Read s
If s = "" Then Exit Do
flag = -1
 
While s <> ""
t1 = InStr(s, "[") +1
t2 = InStr(t1, s, ",") +1
t3 = InStr(t2, s, ",") +1
t4 = InStr(t3, s, "]")
s1 = Trim(Mid(s, t1, t2 - t1 -1))
s2 = Trim(Mid(s, t2, t3 - t2 -1))
s3 = Trim(Mid(s, t3, t4 - t3))
m1 = Val(s1)
a1 = Val(s2)
b1 = Val(s3)
 
sign = IIf(m1 < 0, " - ", " + ")
If m1 < 0 Then a1 = -a1 : m1 = Abs(m1)
s = Mid(s, t4 +1)
Print IIf(flag = 0, sign, ""); IIf(m1 = 1, "", Str(m1));
Print "Atn("; s2; "/" ;s3; ")";
 
If flag = -1 Then
flag = 0
Mpz_set_si(_a(f1), a1)
Mpz_set_si(_b(f1), b1)
If m1 > 1 Then work2do(m1, f1)
Continue While
End If
 
Mpz_set_si(_a(f2), a1)
Mpz_set_si(_b(f2), b1)
If m1 > 1 Then work2do(m1, f2)
 
Mpz_mul(temp1, _a(f1), _b(f2))
Mpz_mul(temp2, _b(f1), _a(f2))
Mpz_add(_a(f3), temp1, temp2)
Mpz_mul(temp1, _b(f1), _b(f2))
Mpz_mul(temp2, _a(f1), _a(f2))
Mpz_sub(_b(f3), temp1, temp2)
mpq_canonicalize(f3)
mpq_set(f1, f3)
 
Wend
 
If Mpz_cmp_ui(_b(f1), 1) = 0 AndAlso Mpz_cmp(_a(f1), _b(f1)) = 0 Then
Print " = 1"
Else
Mpf_set_q(float, f1)
gmp_printf(!" = %.*Ff\n", 15, float)
End If
 
Loop
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>
<pre>Atn(1/2) + Atn(1/3) = 1
2Atn(tan { { 1 1/3)2 +} { 1 Atn(1/7)3 } } = 1
4Atn(tan { { 2 1/5)3 -} { 1 Atn(1/239)7 } } = 1
5Atn(tan { { 4 1/7)5 +} 2Atn(3{ -1 1/79)239 } } = 1
Atn(1/2)tan +{ { 5 Atn(1/5)7 +} Atn(1{ 2 3/8)79 } } = 1
4Atn(1/5)tan -{ Atn(1{ 5 29/70)278 +} Atn(1{ 7 3/99)79 } } = 1
5Atn(tan { { 1 1/7)2 +} { 1 4Atn(1/53)5 +} { 1 2Atn(1/4443)8 } } = 1
6Atn(tan { { 5 1/8)7 +} { 4 2Atn(1/57)53 +} { 2 Atn(1/239)4443 } } = 1
8Atn(tan { { 6 1/10)8 -} { 2 Atn(1/239)57 -} { 1 4Atn(1/515)239 } } = 1
12Atn(tan { { 8 1/18)10 +} { -1 8Atn(1/57)239 } { -4 5Atn(1/239)515 } } = 1
16Atn(tan { { 12 1/21)18 +} { 8 3Atn(1/239)57 +} 4Atn(3{ -5 1/1042)239 } } = 1
22Atn(1/28)tan +{ { 16 2Atn(1/443)21 -} { 3 5Atn(1/1393)239 -} 10Atn(1{ 4 3/11018)1042 } } = 1
tan { { 22 1/28 } { 2 1/443 } { -5 1/1393 } { -10 1/11018 } } = 1
22Atn(1/38) + 17Atn(7/601) + 10Atn(7/8149) = 1
44Atn(1/57)tan +{ { 22 7Atn(1/239)38 -} 12Atn(1{ 17 7/682)601 +} 24Atn(1{ 10 7/12943)8149 } } = 1
88Atn(tan { { 44 1/172)57 +} { 7 51Atn(1/239) +} { -12 32Atn(1/682) +} 44Atn(1/5357){ +24 68Atn(1/12943) } } = 1
tan {
88Atn(1/172) + 51Atn(1/239) + 32Atn(1/682) + 44Atn(1/5357) + 68Atn(1/12944) = 0.999999188225744</pre>
{ 88 1/172 }
 
{ 51 1/239 }
{ 32 1/682 }
{ 44 1/5357 }
{ 68 1/12943 }
} = 1
tan {
{ 88 1/172 }
{ 51 1/239 }
{ 32 1/682 }
{ 44 1/5357 }
{ 68 1/12944 }
} = 10092...08223/10092...39711
</pre>
=={{header|GAP}}==
The formula is entered as a list of pairs [k, x], where each pair means k*atan(x), and all the terms in the list are summed. Like most other solutions, the program will only check that the tangent of the resulting sum is 1. For instance, <code>Check([[5, 1/2], [5, 1/3]]);</code> returns also <code>true</code>, though the result is 5pi/4.
 
<langsyntaxhighlight lang="gap">TanPlus := function(a, b)
return (a + b) / (1 - a * b);
end;
Line 541 ⟶ 761:
[[88, 1/172], [51, 1/239], [32, 1/682], [44, 1/5357], [68, 1/12943]]], Check);
Check([[88, 1/172], [51, 1/239], [32, 1/682], [44, 1/5357], [68, 1/12944]]);</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 608 ⟶ 827:
r := new(big.Rat)
return r.Quo(new(big.Rat).Add(a, b), r.Sub(one, r.Mul(a, b)))
}</langsyntaxhighlight>
{{out}}
Last line edited to show only most significant digits of fraction which is near, but not exactly equal to 1.
Line 631 ⟶ 850:
100928883...
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Ratio
import Data.List (foldl')
 
Line 663 ⟶ 881:
 
putStr "\nnot Machin: "; print not_machin
print (tans not_machin)</langsyntaxhighlight>
 
A crazier way to do the above, exploiting the built-in exponentiation algorithms:
<langsyntaxhighlight lang="haskell">import Data.Ratio
 
-- Private type. Do not use outside of the tans function
Line 699 ⟶ 917:
 
putStr "\nnot Machin: "; print not_machin
print (tans not_machin)</langsyntaxhighlight>
 
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j"> machin =: 1r4p1 = [: +/ ({. * _3 o. %/@:}.)"1@:x:</langsyntaxhighlight>
'''Example''' (''test cases from task description''):<langsyntaxhighlight lang="j"> R =: <@:(0&".);._2 ];._2 noun define
  1  1     2
  1  1     3
Line 771 ⟶ 987:
 
   machin&> R
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1</langsyntaxhighlight>
'''Example''' (''counterexample''):<langsyntaxhighlight lang="j">   counterExample=. 12944 (<_1;_1)} >{:R
   counterExample  NB. Same as final test case with 12943 incremented to 12944
88 1   172
Line 780 ⟶ 996:
68 1 12944
   machin counterExample
0</langsyntaxhighlight>
'''Notes''': The function <tt>machin</tt> compares the results of each formula to π/4 (expressed as <tt>1r4p1</tt> in J's numeric notation). The first example above shows the results of these comparisons for each formula (with 1 for true and 0 for false). In J, arctan is expressed as <tt>_3 o. ''values''</tt> and the function <tt>x:</tt> coerces values to exact representation; thereafter J will maintain exactness throughout its calculations, as long as it can.
=={{header|Java}}==
Read formula from file. Parse and evaluate formula.
Implement Fraction class to support task.
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class CheckMachinFormula {
private static String FILE_NAME = "MachinFormula.txt";
public static void main(String[] args) {
try {
runPrivate();
} catch (Exception e) {
e.printStackTrace();
}
}
 
private static void runPrivate() throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(new File(FILE_NAME)));) {
String inLine = null;
while ( (inLine = reader.readLine()) != null ) {
String[] split = inLine.split("=");
System.out.println(tanLeft(split[0].trim()) + " = " + split[1].trim().replaceAll("\\s+", " ") + " = " + tanRight(split[1].trim()));
}
}
}
private static String tanLeft(String formula) {
if ( formula.compareTo("pi/4") == 0 ) {
return "1";
}
throw new RuntimeException("ERROR 104: Unknown left side: " + formula);
}
private static final Pattern ARCTAN_PATTERN = Pattern.compile("(-{0,1}\\d+)\\*arctan\\((\\d+)/(\\d+)\\)");
private static Fraction tanRight(String formula) {
Matcher matcher = ARCTAN_PATTERN.matcher(formula);
List<Term> terms = new ArrayList<>();
while ( matcher.find() ) {
terms.add(new Term(Integer.parseInt(matcher.group(1)), new Fraction(matcher.group(2), matcher.group(3))));
}
return evaluateArctan(terms);
}
private static Fraction evaluateArctan(List<Term> terms) {
if ( terms.size() == 1 ) {
Term term = terms.get(0);
return evaluateArctan(term.coefficient, term.fraction);
}
int size = terms.size();
List<Term> left = terms.subList(0, (size+1) / 2);
List<Term> right = terms.subList((size+1) / 2, size);
return arctanFormula(evaluateArctan(left), evaluateArctan(right));
}
private static Fraction evaluateArctan(int coefficient, Fraction fraction) {
//System.out.println("C = " + coefficient + ", F = " + fraction);
if ( coefficient == 1 ) {
return fraction;
}
else if ( coefficient < 0 ) {
return evaluateArctan(-coefficient, fraction).negate();
}
if ( coefficient % 2 == 0 ) {
Fraction f = evaluateArctan(coefficient/2, fraction);
return arctanFormula(f, f);
}
Fraction a = evaluateArctan(coefficient/2, fraction);
Fraction b = evaluateArctan(coefficient - (coefficient/2), fraction);
return arctanFormula(a, b);
}
private static Fraction arctanFormula(Fraction f1, Fraction f2) {
return f1.add(f2).divide(Fraction.ONE.subtract(f1.multiply(f2)));
}
private static class Fraction {
public static final Fraction ONE = new Fraction("1", "1");
private BigInteger numerator;
private BigInteger denominator;
public Fraction(String num, String den) {
numerator = new BigInteger(num);
denominator = new BigInteger(den);
}
 
public Fraction(BigInteger num, BigInteger den) {
numerator = num;
denominator = den;
}
 
public Fraction negate() {
return new Fraction(numerator.negate(), denominator);
}
public Fraction add(Fraction f) {
BigInteger gcd = denominator.gcd(f.denominator);
BigInteger first = numerator.multiply(f.denominator.divide(gcd));
BigInteger second = f.numerator.multiply(denominator.divide(gcd));
return new Fraction(first.add(second), denominator.multiply(f.denominator).divide(gcd));
}
public Fraction subtract(Fraction f) {
return add(f.negate());
}
public Fraction multiply(Fraction f) {
BigInteger num = numerator.multiply(f.numerator);
BigInteger den = denominator.multiply(f.denominator);
BigInteger gcd = num.gcd(den);
return new Fraction(num.divide(gcd), den.divide(gcd));
}
 
public Fraction divide(Fraction f) {
return multiply(new Fraction(f.denominator, f.numerator));
}
@Override
public String toString() {
if ( denominator.compareTo(BigInteger.ONE) == 0 ) {
return numerator.toString();
}
return numerator + " / " + denominator;
}
}
private static class Term {
private int coefficient;
private Fraction fraction;
public Term(int c, Fraction f) {
coefficient = c;
fraction = f;
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
1 = 1*arctan(1/2) + 1*arctan(1/3) = 1
1 = 2*arctan(1/3) + 1*arctan(1/7) = 1
1 = 4*arctan(1/5) + -1*arctan(1/239) = 1
1 = 5*arctan(1/7) + 2*arctan(3/79) = 1
1 = 5*arctan(29/278) + 7*arctan(3/79) = 1
1 = 1*arctan(1/2) + 1*arctan(1/5) + 1*arctan(1/8) = 1
1 = 4*arctan(1/5) + -1*arctan(1/70) + 1*arctan(1/99) = 1
1 = 5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443) = 1
1 = 6*arctan(1/8) + 2*arctan(1/57) + 1*arctan(1/239) = 1
1 = 8*arctan(1/10) + -1*arctan(1/239) + -4*arctan(1/515) = 1
1 = 12*arctan(1/18) + 8*arctan(1/57) + -5*arctan(1/239) = 1
1 = 16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042) = 1
1 = 22*arctan(1/28) + 2*arctan(1/443) + -5*arctan(1/1393) + -10*arctan(1/11018) = 1
1 = 22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149) = 1
1 = 44*arctan(1/57) + 7*arctan(1/239) + -12*arctan(1/682) + 24*arctan(1/12943) = 1
1 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943) = 1
1 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944) = 1009288018000944050967896710431587186456256928584351786643498522649995492271475761189348270710224618853590682465929080006511691833816436374107451368838065354726517908250456341991684635768915704374493675498637876700129004484434187627909285979251682006538817341793224963346197503893270875008524149334251672855130857035205217929335932890740051319216343365800342290782260673215928499123722781078448297609548233999010983373327601187505623621602789012550584784738082074783523787011976757247516095289966708782862528690942242793667539020699840402353522108223 / 1009288837315638583415701528780402795721935641614456853534313491853293025565940011104051964874275710024625850092154664245109626053906509780125743180758231049920425664246286578958307532545458843067352531217230461290763258378749459637420702619029075083089762088232401888676895047947363883809724322868121990870409574061477638203859217672620508200713073485398199091153535700094640095900731630771349477187594074169815106104524371099618096164871416282464532355211521113449237814080332335526420331468258917484010722587072087349909684004660371264507984339711
 
</pre>
=={{header|Julia}}==
<syntaxhighlight lang="julia">
using AbstractAlgebra # implements arbitrary precision rationals
 
tanplus(x,y) = (x + y) / (1 - x * y)
 
function taneval(coef, frac)
if coef == 0
return 0
elseif coef < 0
return -taneval(-coef, frac)
elseif isodd(coef)
return tanplus(frac, taneval(coef - 1, frac))
else
x = taneval(div(coef, 2), frac)
return tanplus(x, x)
end
end
 
taneval(tup::Tuple) = taneval(tup[1], tup[2])
 
tans(v::Vector{Tuple{BigInt, Rational{BigInt}}}) = foldl(tanplus, map(taneval, v), init=0)
 
const testmats = Dict{Vector{Tuple{BigInt, Rational{BigInt}}}, Bool}([
([(1, 1//2), (1, 1//3)], true), ([(2, 1//3), (1, 1//7)], true),
([(12, 1//18), (8, 1//57), (-5, 1//239)], true),
([(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12943)], true),
([(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12944)], false)])
 
function runtestmats()
println("Testing matrices:")
for (k, m) in testmats
ans = tans(k)
println((ans == 1) == m ? "Verified as $m: " : "Not Verified as $m: ", "tan $k = $ans")
end
end
 
runtestmats()
</syntaxhighlight> {{output}} <pre>
Testing matrices:
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(1, 1//2), (1, 1//3)] = 1//1
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(2, 1//3), (1, 1//7)] = 1//1
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12943)] = 1//1
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(12, 1//18), (8, 1//57), (-5, 1//239)] = 1//1
Verified as false: tan Tuple{BigInt,Rational{BigInt}}[(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12944)] = 1009288018000944050967896710431587186456256928584351786643498522649995492271475761189348270710224618853590682465929080006511691833816436374107451368838065354726517908250456341991684635768915704374493675498637876700129004484434187627909285979251682006538817341793224963346197503893270875008524149334251672855130857035205217929335932890740051319216343365800342290782260673215928499123722781078448297609548233999010983373327601187505623621602789012550584784738082074783523787011976757247516095289966708782862528690942242793667539020699840402353522108223//1009288837315638583415701528780402795721935641614456853534313491853293025565940011104051964874275710024625850092154664245109626053906509780125743180758231049920425664246286578958307532545458843067352531217230461290763258378749459637420702619029075083089762088232401888676895047947363883809724322868121990870409574061477638203859217672620508200713073485398199091153535700094640095900731630771349477187594074169815106104524371099618096164871416282464532355211521113449237814080332335526420331468258917484010722587072087349909684004660371264507984339711
</pre>
=={{header|Kotlin}}==
As the JVM and Kotlin standard libraries lack a BigRational class, I've written one which just provides sufficient functionality to complete this task:
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import java.math.BigInteger
Line 916 ⟶ 1,351:
println(")")
}
}</langsyntaxhighlight>
 
{{out}}
Line 938 ⟶ 1,373:
false << 1 == tan(88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12944))
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="text">Tan[ArcTan[1/2] + ArcTan[1/3]] == 1
Tan[2 ArcTan[1/3] + ArcTan[1/7]] == 1
Tan[4 ArcTan[1/5] - ArcTan[1/239]] == 1
Line 960 ⟶ 1,394:
44 ArcTan[1/5357] + 68 ArcTan[1/12943]] == 1
Tan[88 ArcTan[1/172] + 51 ArcTan[1/239] + 32 ArcTan[1/682] +
44 ArcTan[1/5357] + 68 ArcTan[1/12944]] == 1</langsyntaxhighlight>
 
{{Out}}
Line 996 ⟶ 1,430:
 
False</pre>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">trigexpand:true$
is(tan(atan(1/2)+atan(1/3))=1);
is(tan(2*atan(1/3)+atan(1/7))=1);
Line 1,015 ⟶ 1,448:
is(tan(44*atan(1/57)+7*atan(1/239)-12*atan(1/682)+24*atan(1/12943))=1);
is(tan(88*atan(1/172)+51*atan(1/239)+32*atan(1/682)+44*atan(1/5357)+68*atan(1/12943))=1);
is(tan(88*atan(1/172)+51*atan(1/239)+32*atan(1/682)+44*atan(1/5357)+68*atan(1/12944))=1);</langsyntaxhighlight>
{{out}}
<pre>(%i2)
Line 1,051 ⟶ 1,484:
(%i18)
(%o18) false</pre>
=={{header|Nim}}==
{{libheader|bignum}}
The most important part of our program is the formula parser which proceeds to a full syntactical validation. The parser builds an expression which is a sequence of terms, a term being itself composed of a factor and a fraction (the argument to arctan).
 
<syntaxhighlight lang="nim">import bignum
 
type
 
# Description of a term.
Term = object
factor: int # Multiplier (may be negative).
fract: Rat # Argument of arc tangent.
 
Expression = seq[Term]
 
# Rational 1.
let One = newRat(1)
 
 
####################################################################################################
# Formula parser.
 
type
 
# Possible tokens for parsing.
Token = enum tkPi, tkArctan, tkNumber, tkEqual, tkAdd, tkSub,
tkMul, tkDiv, tkLPar, tkRPar, tkError, tkEnd
 
# Lexer description.
Lexer = object
line: string # The line to parse.
pos: Natural # Current position of lexer.
token: Token # Current token.
value: Natural # Associated value (for numbers).
 
# Exception raised if an error is found.
SyntaxError = object of CatchableError
 
#---------------------------------------------------------------------------------------------------
 
proc initLexer(line: string): Lexer =
## Create and initialize a lexer.
result.line = line
result.pos = 0
 
#---------------------------------------------------------------------------------------------------
 
proc parseName(lexer: var Lexer; pos: Natural) =
## Parse a name.
 
# Build the name.
var pos = pos
var name = ""
while pos < lexer.line.len and (let c = lexer.line[pos]; c) in 'a'..'z':
name.add(c)
inc pos
 
# Update lexer state.
lexer.token = if name == "arctan": tkArctan
elif name == "pi": tkPi
else: tkError
lexer.pos = pos
 
#---------------------------------------------------------------------------------------------------
 
proc parseNumber(lexer: var Lexer; pos: Natural) =
## Parse a number.
 
# Build the number.
var pos = pos
var value = 0
while pos < lexer.line.len and (let c = lexer.line[pos]; c) in '0'..'9':
value = 10 * value + ord(c) - ord('0')
inc pos
 
# Update lexer state.
lexer.token = tkNumber
lexer.value = value
lexer.pos = pos
 
#---------------------------------------------------------------------------------------------------
 
proc getNextToken(lexer: var Lexer) =
## Find next token.
 
var pos = lexer.pos
var token: Token
while pos < lexer.line.len and lexer.line[pos] == ' ': inc pos
if pos == lexer.line.len:
# Reached end of string.
lexer.pos = pos
lexer.token = tkEnd
return
 
# Find token.
case lexer.line[pos]
of '=': token = tkEqual
of '+': token = tkAdd
of '-': token = tkSub
of '*': token = tkMul
of '/': token = tkDiv
of '(': token = tkLPar
of ')': token = tkRPar
of 'a'..'z':
lexer.parseName(pos)
return
of '0'..'9':
lexer.parseNumber(pos)
return
else: token = tkError
 
# Update lexer state.
lexer.pos = pos + 1
lexer.token = token
 
#---------------------------------------------------------------------------------------------------
 
template syntaxError(message: string) =
## Raise a syntax error exception.
raise newException(SyntaxError, message)
 
#---------------------------------------------------------------------------------------------------
 
proc parseFraction(lexer: var Lexer): Rat =
## Parse a fraction: number / number.
 
lexer.getNextToken()
if lexer.token != tkNumber:
syntaxError("number expected.")
let num = lexer.value
lexer.getNextToken()
if lexer.token != tkDiv:
syntaxError("“/” expected.")
lexer.getNextToken()
if lexer.token != tkNumber:
syntaxError("number expected")
if lexer.value == 0:
raise newException(ValueError, "null denominator.")
let den = lexer.value
result = newRat(num, den)
 
#---------------------------------------------------------------------------------------------------
 
proc parseTerm(lexer: var Lexer): Term =
## Parse a term: factor * arctan(fraction) or arctan(fraction).
 
lexer.getNextToken()
 
# Parse factor.
if lexer.token == tkNumber:
result.factor = lexer.value
lexer.getNextToken
if lexer.token != tkMul:
syntaxError("“*” expected.")
lexer.getNextToken()
else:
result.factor = 1
 
# Parse arctan.
if lexer.token != tkArctan:
syntaxError("“arctan” expected.")
lexer.getNextToken()
if lexer.token != tkLPar:
syntaxError("“(” expected.")
result.fract = lexer.parseFraction()
lexer.getNextToken()
if lexer.token != tkRPar:
syntaxError("“)” expected.")
 
#---------------------------------------------------------------------------------------------------
 
proc parse(line: string): Expression =
## Parse a formula.
 
var lexer = initLexer(line)
lexer.getNextToken()
 
if lexer.token != tkPi:
syntaxError("pi symbol expected.")
lexer.getNextToken()
 
if lexer.token != tkDiv:
syntaxError("'/' expected.")
lexer.getNextToken()
 
if lexer.token != tkNumber:
syntaxError("number expected.")
if lexer.value != 4:
raise newException(ValueError, "value 4 expected.")
lexer.getNextToken()
 
if lexer.token != tkEqual:
syntaxError("“=” expected.")
result.add(lexer.parseTerm())
lexer.getNextToken()
 
# Parse the next terms.
while (let token = lexer.token; token) in {tkAdd, tkSub}:
var term = lexer.parseTerm()
if token == tkSub:
term.factor = -term.factor
result.add(term)
lexer.getNextToken()
 
if lexer.token != tkEnd:
syntaxError("invalid characters at end of formula.")
 
 
####################################################################################################
# Evaluator.
 
proc tangent(factor: int; fract: Rat): Rat =
## Compute the tangent of "factor * arctan(fract)".
 
if factor == 1:
return fract
if factor < 0:
return -tangent(-factor, fract)
 
# Split in two parts.
let n = factor div 2
let a = tangent(n, fract)
let b = tangent(factor - n, fract)
result = (a + b) / (One - a * b)
 
#---------------------------------------------------------------------------------------------------
 
proc tangent(expr: Expression): Rat =
## Compute the tangent of a sum of terms.
 
if expr.len == 1:
result = tangent(expr[0].factor, expr[0].fract)
else:
# Split in two parts.
let a = tangent(expr[0..<(expr.len div 2)])
let b = tangent(expr[(expr.len div 2)..^1])
result = (a + b) / (One - a * b)
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
when isMainModule:
 
const Formulas = [
"pi/4 = arctan(1/2) + arctan(1/3)",
"pi/4 = 2*arctan(1/3) + arctan(1/7)",
"pi/4 = 4*arctan(1/5) - arctan(1/239)",
"pi/4 = 5*arctan(1/7) + 2*arctan(3/79)",
"pi/4 = 5*arctan(29/278) + 7*arctan(3/79)",
"pi/4 = arctan(1/2) + arctan(1/5) + arctan(1/8)",
"pi/4 = 4*arctan(1/5) - arctan(1/70) + arctan(1/99)",
"pi/4 = 5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443)",
"pi/4 = 6*arctan(1/8) + 2*arctan(1/57) + arctan(1/239)",
"pi/4 = 8*arctan(1/10) - arctan(1/239) - 4*arctan(1/515)",
"pi/4 = 12*arctan(1/18) + 8*arctan(1/57) - 5*arctan(1/239)",
"pi/4 = 16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042)",
"pi/4 = 22*arctan(1/28) + 2*arctan(1/443) - 5*arctan(1/1393) - 10*arctan(1/11018)",
"pi/4 = 22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149)",
"pi/4 = 44*arctan(1/57) + 7*arctan(1/239) - 12*arctan(1/682) + 24*arctan(1/12943)",
"pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)",
"pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)"]
 
for formula in Formulas:
let expr = formula.parse()
let value = tangent(expr)
if value == 1:
echo "True: ", formula
else:
echo "False: ", formula
echo "Tangent of the right expression is about ", value.toFloat</syntaxhighlight>
 
{{out}}
<pre>True: pi/4 = arctan(1/2) + arctan(1/3)
True: pi/4 = 2*arctan(1/3) + arctan(1/7)
True: pi/4 = 4*arctan(1/5) - arctan(1/239)
True: pi/4 = 5*arctan(1/7) + 2*arctan(3/79)
True: pi/4 = 5*arctan(29/278) + 7*arctan(3/79)
True: pi/4 = arctan(1/2) + arctan(1/5) + arctan(1/8)
True: pi/4 = 4*arctan(1/5) - arctan(1/70) + arctan(1/99)
True: pi/4 = 5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443)
True: pi/4 = 6*arctan(1/8) + 2*arctan(1/57) + arctan(1/239)
True: pi/4 = 8*arctan(1/10) - arctan(1/239) - 4*arctan(1/515)
True: pi/4 = 12*arctan(1/18) + 8*arctan(1/57) - 5*arctan(1/239)
True: pi/4 = 16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042)
True: pi/4 = 22*arctan(1/28) + 2*arctan(1/443) - 5*arctan(1/1393) - 10*arctan(1/11018)
True: pi/4 = 22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149)
True: pi/4 = 44*arctan(1/57) + 7*arctan(1/239) - 12*arctan(1/682) + 24*arctan(1/12943)
True: pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)
False: pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)
Tangent of the right expression is about 0.9999991882257444</pre>
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">open Num;; (* use exact rationals for results *)
 
let tadd p q = (p +/ q) // ((Int 1) -/ (p */ q)) in
Line 1,104 ⟶ 1,824:
(4,[(88,1,172);(51,1,239);(32,1,682);(44,1,5357);(68,1,12943)]);
(4,[(88,1,172);(51,1,239);(32,1,682);(44,1,5357);(68,1,12944)])
]</langsyntaxhighlight>
 
Compile with
Line 1,148 ⟶ 1,868:
tan(RHS) is not one
</pre>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/*REXX ----------------------------------------------------------------
* 09.04.2014 Walter Pachl the REXX solution adapted for ooRexx
* which provides a function package rxMath
Line 1,178 ⟶ 1,897:
end /*j*/ /* [?] show OK | bad, formula. */
::requires rxmath library
</syntaxhighlight>
</lang>
{{out}}
<pre> OK: pi/4=rxCalcarctan(1/2,16,R)+rxCalcarctan(1/3,16,R)
Line 1,197 ⟶ 1,916:
OK: pi/4=88*rxCalcarctan(1/172,16,R)+51*rxCalcarctan(1/239,16,R)+32*rxCalcarctan(1/682,16,R)+44*rxCalcarctan(1/5357,16,R)+68*rxCalcarctan(1/12943,16,R)
bad: pi/4=88*rxCalcarctan(1/172,16,R)+51*rxCalcarctan(1/239,16,R)+32*rxCalcarctan(1/682,16,R)+44*rxCalcarctan(1/5357,16,R)+68*rxCalcarctan(1/12944,16,R)</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">tanEval(coef, f)={
if (coef <= 1, return(if(coef<1,-tanEval(-coef, f),f)));
my(a=tanEval(coef\2, f), b=tanEval(coef-coef\2, f));
Line 1,229 ⟶ 1,947:
test([[44,1/57],[7,1/239],[-12,1/682],[24,1/12943]]);
test([[88,1/172],[51,1/239],[32,1/682],[44,1/5357],[68,1/12943]]);
test([[88,1/172],[51,1/239],[32,1/682],[44,1/5357],[68,1/12944]]);</langsyntaxhighlight>
{{out}}
<pre>OK
Line 1,248 ⟶ 1,966:
OK
Error: [[88, 1/172], [51, 1/239], [32, 1/682], [44, 1/5357], [68, 1/12944]]</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">use Math::BigRat try=>"GMP";
 
sub taneval {
Line 1,289 ⟶ 2,006:
test([44,'1/57'],[7,'1/239'],[-12,'1/682'],[24,'1/12943']);
test([88,'1/172'],[51,'1/239'],[32,'1/682'],[44,'1/5357'],[68,'1/12943']);
test([88,'1/172'],[51,'1/239'],[32,'1/682'],[44,'1/5357'],[68,'1/12944']);</langsyntaxhighlight>
{{out}}
<pre> OK ([1 1/2] [1 1/3])
Line 1,308 ⟶ 2,025:
OK ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/12943])
Error ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/12944])</pre>
=={{header|Phix}}==
===Naive version===
Rather than test tan(a) for 1.0, test whether the sprint of it, which is rounded to 10 significant digits, is "1.0" or "1".<br>
At the end we deliberately show a test case that should and does fail.
 
<!--<syntaxhighlight lang="phix">(phixonline)-->
=={{header|Perl 6}}==
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
{{Works with|rakudo|2018.03}}
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
The coercion to FatRat provides for exact computation for all input.
<span style="color: #008080;">if</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4</span> <span style="color: #0000FF;">>=</span> <span style="color: #000000;">a</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">a</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span> <span style="color: #000080;font-style:italic;">-- or test for "1.0"/"1", but not 1.0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">79</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29</span><span style="color: #0000FF;">/</span> <span style="color: #000000;">278</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">79</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">70</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">99</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">53</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">4443</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">57</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">515</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">18</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">57</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">16</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">21</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">1042</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">22</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">28</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">443</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">1393</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">11018</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">22</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">38</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">17</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">601</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">10</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">8149</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">44</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">57</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">12</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">682</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">24</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">12943</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">88</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">172</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">51</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">32</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">682</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">44</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">5357</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">68</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">12943</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"==="</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">88</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">172</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">51</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">239</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">682</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">44</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">5357</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">68</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">12944</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
"==="
0.9999991882
</pre>
 
===Using proper fractions===
{{trans|Perl}}
{{libheader|Phix/mpfr}}
<lang perl6>sub taneval ($coef, $f) {
<!--<syntaxhighlight lang="phix">(phixonline)-->
return 0 if $coef == 0;
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
return $f if $coef == 1;
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
return -taneval(-$coef, $f) if $coef < 0;
 
<span style="color: #008080;">function</span> <span style="color: #000000;">tans</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
my $a = taneval($coef+>1, $f);
<span style="color: #004080;">sequence</span> <span style="color: #000000;">args</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">))</span>
my $b = taneval($coef - $coef+>1, $f);
<span style="color: #004080;">mpq</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">aab</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mab</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpq_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
($a+$b)/(1-$a*$b);
<span style="color: #004080;">integer</span> <span style="color: #000000;">h</span>
}
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
 
<span style="color: #0000FF;">{</span><span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">mpq</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
sub tans (@xs) {
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
return taneval(@xs[0;0], @xs[0;1].FatRat) if @xs == 1;
<span style="color: #7060A8;">mpq_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
my $a = tans(@xs[0 .. (-1+@xs+>1)]);
<span style="color: #008080;">elsif</span> <span style="color: #000000;">m</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
my $b = tans(@xs[(-1+@xs+>1)+1 .. -1+@xs]);
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tans</span><span style="color: #0000FF;">({{-</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">}})</span>
($a+$b)/(1-$a*$b);
<span style="color: #7060A8;">mpq_neg</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
}
<span style="color: #008080;">return</span> <span style="color: #000000;">f</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
sub verify (@eqn) {
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
printf "%5s (%s)\n", (tans(@eqn) == 1) ?? "OK" !! "Error",
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tans</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">}})</span>
(map { "[{.[0]} {.[1].nude.join('/')}]" }, @eqn).join(' ');
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tans</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">}})</span>
}
<span style="color: #008080;">else</span>
 
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
verify($_) for
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">h</span><span style="color: #0000FF;">])</span>
([[1,1/2], [1,1/3]],
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">h</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$])</span>
[[2,1/3], [1,1/7]],
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
[[4,1/5], [-1,1/239]],
<span style="color: #7060A8;">mpq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mab</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
[[5,1/7], [2,3/79]],
<span style="color: #7060A8;">mpq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aab</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
[[5,29/278], [7,3/79]],
<span style="color: #7060A8;">mpq_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
[[1,1/2], [1,1/5], [1,1/8]],
<span style="color: #7060A8;">mpq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mab</span><span style="color: #0000FF;">)</span>
[[4,1/5], [-1,1/70], [1,1/99]],
<span style="color: #7060A8;">mpq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">aab</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
[[5,1/7], [4,1/53], [2,1/4443]],
<span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">aab</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mab</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpq_free</span><span style="color: #0000FF;">({</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">aab</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mab</span><span style="color: #0000FF;">})</span>
[[6,1/8], [2,1/57], [1,1/239]],
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
[[8,1/10], [-1,1/239], [-4,1/515]],
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
[[12,1/18], [8,1/57], [-5,1/239]],
[[16,1/21], [3,1/239], [4,3/1042]],
<span style="color: #008080;">function</span> <span style="color: #000000;">parse</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">formula</span><span style="color: #0000FF;">)</span>
[[22,1/28], [2,1/443], [-5,1/1393], [-10,1/11018]],
<span style="color: #000080;font-style:italic;">-- obviously the error handling here is a bit brutal...</span>
[[22,1/38], [17,7/601], [10,7/8149]],
<span style="color: #000000;">formula</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- strip spaces</span>
[[44,1/57], [7,1/239], [-12,1/682], [24,1/12943]],
<span style="color: #008080;">if</span> <span style="color: #000000;">formula</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">5</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">"pi/4="</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
[[88,1/172], [51,1/239], [32,1/682], [44,1/5357], [68,1/12943]],
<span style="color: #000000;">formula</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">formula</span><span style="color: #0000FF;">[</span><span style="color: #000000;">6</span><span style="color: #0000FF;">..$]</span>
[[88,1/172], [51,1/239], [32,1/682], [44,1/5357], [68,1/21944]]
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">r</span>
);</lang>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">sgn</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span>
<span style="color: #008080;">switch</span> <span style="color: #000000;">formula</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'-'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">sgn</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #008080;">fallthrough</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'+'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">formula</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">formula</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">formula</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'a'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sgn</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d*%s"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">sgn</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"arctan(%d/%d)%s"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpq_init_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">formula</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpq</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpq_cmp_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"OK: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ERROR: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">formula</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpq_get_num</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpq_get_den</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s\n\\ %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)),</span>
<span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">formulae</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"pi/4 = arctan(1/2) + arctan(1/3)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 2*arctan(1/3) + arctan(1/7)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 4*arctan(1/5) - arctan(1/239)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 5*arctan(1/7) + 2*arctan(3/79)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 5*arctan(29/278) + 7*arctan(3/79)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = arctan(1/2) + arctan(1/5) + arctan(1/8)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 4*arctan(1/5) - arctan(1/70) + arctan(1/99)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 6*arctan(1/8) + 2*arctan(1/57) + arctan(1/239)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 8*arctan(1/10) - arctan(1/239) - 4*arctan(1/515)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 12*arctan(1/18) + 8*arctan(1/57) - 5*arctan(1/239)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 22*arctan(1/28) + 2*arctan(1/443) - 5*arctan(1/1393) - 10*arctan(1/11018)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 44*arctan(1/57) + 7*arctan(1/239) - 12*arctan(1/682) + 24*arctan(1/12943)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">formulae</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">formulae</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
As above, the last case should and does fail.
<pre> OK ([1 1/2] [1 1/3])
<pre>
OK ([2 1/3] [1 1/7])
OK: pi/4 = arctan(1/2) + arctan(1/3)
OK ([4 1/5] [-1 1/239])
OK: pi/4 = 2*arctan(1/3) + arctan(1/7)
OK ([5 1/7] [2 3/79])
OK: pi/4 = 4*arctan(1/5) - arctan(1/239)
OK ([5 29/278] [7 3/79])
OK: pi/4 = 5*arctan(1/7) + 2*arctan(3/79)
OK ([1 1/2] [1 1/5] [1 1/8])
OK: pi/4 = 5*arctan(29/278) + 7*arctan(3/79)
OK ([4 1/5] [-1 1/70] [1 1/99])
OK: pi/4 = arctan(1/2) + arctan(1/5) + arctan(1/8)
OK ([5 1/7] [4 1/53] [2 1/4443])
OK: pi/4 = 4*arctan(1/5) - arctan(1/70) + arctan(1/99)
OK ([6 1/8] [2 1/57] [1 1/239])
OK: pi/4 = 5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443)
OK ([8 1/10] [-1 1/239] [-4 1/515])
OK: pi/4 = 6*arctan(1/8) + 2*arctan(1/57) + arctan(1/239)
OK ([12 1/18] [8 1/57] [-5 1/239])
OK: pi/4 = 8*arctan(1/10) - arctan(1/239) - 4*arctan(1/515)
OK ([16 1/21] [3 1/239] [4 3/1042])
OK: pi/4 = 12*arctan(1/18) + 8*arctan(1/57) - 5*arctan(1/239)
OK ([22 1/28] [2 1/443] [-5 1/1393] [-10 1/11018])
OK: pi/4 = 16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042)
OK ([22 1/38] [17 7/601] [10 7/8149])
OK: pi/4 = 22*arctan(1/28) + 2*arctan(1/443) - 5*arctan(1/1393) - 10*arctan(1/11018)
OK ([44 1/57] [7 1/239] [-12 1/682] [24 1/12943])
OK: pi/4 = 22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149)
OK ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/12943])
OK: pi/4 = 44*arctan(1/57) + 7*arctan(1/239) - 12*arctan(1/682) + 24*arctan(1/12943)
Error ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/21944])</pre>
OK: pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)
 
ERROR: pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)
  10092880180009440509...99840402353522108223 (550 digits)
\ 10092888373156385834...60371264507984339711 (550 digits)
</pre>
=={{header|Python}}==
This example parses the [http://rosettacode.org/mw/index.php?title=Check_Machin-like_formulas&oldid=146749 original] equations to form an intermediate representation then does the checks.<br>
Function tans and tanEval are translations of the HaskelHaskell functions of the same names.
<langsyntaxhighlight lang="python">import re
from fractions import Fraction
from pprint import pprint as pp
Line 1,448 ⟶ 2,276:
for machin, eqn in zip(machins, equationtext.split('\n')):
ans = tans(machin)
print('%5s: %s' % ( ('OK' if ans == 1 else 'ERROR'), eqn))</langsyntaxhighlight>
 
{{out}}
Line 1,470 ⟶ 2,298:
 
'''Note:''' the [http://kodos.sourceforge.net/ Kodos] tool was used in developing the regular expression.
=={{header|R}}==
<syntaxhighlight lang="rsplus">
#lang R
library(Rmpfr)
prec <- 1000 # precision in bits
`%:%` <- function(e1, e2) '/'(mpfr(e1, prec), mpfr(e2, prec)) # operator %:% for high precision division
# function for checking identity of tan of expression and 1, making use of high precision division operator %:%
tanident_1 <- function(x) identical(round(tan(eval(parse(text = gsub("/", "%:%", deparse(substitute(x)))))), (prec/10)), mpfr(1, prec))
</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="r">
tanident_1( 1*atan(1/2) + 1*atan(1/3) )
## [1] TRUE
tanident_1( 2*atan(1/3) + 1*atan(1/7))
## [1] TRUE
tanident_1( 4*atan(1/5) + -1*atan(1/239))
## [1] TRUE
tanident_1( 5*atan(1/7) + 2*atan(3/79))
## [1] TRUE
tanident_1( 5*atan(29/278) + 7*atan(3/79))
## [1] TRUE
tanident_1( 1*atan(1/2) + 1*atan(1/5) + 1*atan(1/8) )
## [1] TRUE
tanident_1( 4*atan(1/5) + -1*atan(1/70) + 1*atan(1/99) )
## [1] TRUE
tanident_1( 5*atan(1/7) + 4*atan(1/53) + 2*atan(1/4443))
## [1] TRUE
tanident_1( 6*atan(1/8) + 2*atan(1/57) + 1*atan(1/239))
## [1] TRUE
tanident_1( 8*atan(1/10) + -1*atan(1/239) + -4*atan(1/515))
## [1] TRUE
tanident_1(12*atan(1/18) + 8*atan(1/57) + -5*atan(1/239))
## [1] TRUE
tanident_1(16*atan(1/21) + 3*atan(1/239) + 4*atan(3/1042))
## [1] TRUE
tanident_1(22*atan(1/28) + 2*atan(1/443) + -5*atan(1/1393) + -10*atan(1/11018))
## [1] TRUE
tanident_1(22*atan(1/38) + 17*atan(7/601) + 10*atan(7/8149))
## [1] TRUE
tanident_1(44*atan(1/57) + 7*atan(1/239) + -12*atan(1/682) + 24*atan(1/12943))
## [1] TRUE
tanident_1(88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12943))
## [1] TRUE
tanident_1(88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12944))
## [1] FALSE
</syntaxhighlight>
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define (reduce e)
Line 1,529 ⟶ 2,403:
(displayln "The incorrect formula reduces to:")
(reduce wrong-formula)
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="racket">
Do all correct formulas reduce to 1?
#t
The incorrect formula reduces to:
1009288018000944050967896710431587186456256928584351786643498522649995492271475761189348270710224618853590682465929080006511691833816436374107451368838065354726517908250456341991684635768915704374493675498637876700129004484434187627909285979251682006538817341793224963346197503893270875008524149334251672855130857035205217929335932890740051319216343365800342290782260673215928499123722781078448297609548233999010983373327601187505623621602789012550584784738082074783523787011976757247516095289966708782862528690942242793667539020699840402353522108223/1009288837315638583415701528780402795721935641614456853534313491853293025565940011104051964874275710024625850092154664245109626053906509780125743180758231049920425664246286578958307532545458843067352531217230461290763258378749459637420702619029075083089762088232401888676895047947363883809724322868121990870409574061477638203859217672620508200713073485398199091153535700094640095900731630771349477187594074169815106104524371099618096164871416282464532355211521113449237814080332335526420331468258917484010722587072087349909684004660371264507984339711
</syntaxhighlight>
</lang>
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
The coercion to FatRat provides for exact computation for all input.
 
{{trans|Perl}}
<syntaxhighlight lang="raku" line>sub taneval ($coef, $f) {
return 0 if $coef == 0;
return $f if $coef == 1;
return -taneval(-$coef, $f) if $coef < 0;
 
my $a = taneval($coef+>1, $f);
my $b = taneval($coef - $coef+>1, $f);
($a+$b)/(1-$a*$b);
}
 
sub tans (@xs) {
return taneval(@xs[0;0], @xs[0;1].FatRat) if @xs == 1;
 
my $a = tans(@xs[0 .. (-1+@xs+>1)]);
my $b = tans(@xs[(-1+@xs+>1)+1 .. -1+@xs]);
($a+$b)/(1-$a*$b);
}
 
sub verify (@eqn) {
printf "%5s (%s)\n", (tans(@eqn) == 1) ?? "OK" !! "Error",
(map { "[{.[0]} {.[1].nude.join('/')}]" }, @eqn).join(' ');
}
 
verify($_) for
([[1,1/2], [1,1/3]],
[[2,1/3], [1,1/7]],
[[4,1/5], [-1,1/239]],
[[5,1/7], [2,3/79]],
[[5,29/278], [7,3/79]],
[[1,1/2], [1,1/5], [1,1/8]],
[[4,1/5], [-1,1/70], [1,1/99]],
[[5,1/7], [4,1/53], [2,1/4443]],
[[6,1/8], [2,1/57], [1,1/239]],
[[8,1/10], [-1,1/239], [-4,1/515]],
[[12,1/18], [8,1/57], [-5,1/239]],
[[16,1/21], [3,1/239], [4,3/1042]],
[[22,1/28], [2,1/443], [-5,1/1393], [-10,1/11018]],
[[22,1/38], [17,7/601], [10,7/8149]],
[[44,1/57], [7,1/239], [-12,1/682], [24,1/12943]],
[[88,1/172], [51,1/239], [32,1/682], [44,1/5357], [68,1/12943]],
[[88,1/172], [51,1/239], [32,1/682], [44,1/5357], [68,1/21944]]
);</syntaxhighlight>
{{out}}
<pre> OK ([1 1/2] [1 1/3])
OK ([2 1/3] [1 1/7])
OK ([4 1/5] [-1 1/239])
OK ([5 1/7] [2 3/79])
OK ([5 29/278] [7 3/79])
OK ([1 1/2] [1 1/5] [1 1/8])
OK ([4 1/5] [-1 1/70] [1 1/99])
OK ([5 1/7] [4 1/53] [2 1/4443])
OK ([6 1/8] [2 1/57] [1 1/239])
OK ([8 1/10] [-1 1/239] [-4 1/515])
OK ([12 1/18] [8 1/57] [-5 1/239])
OK ([16 1/21] [3 1/239] [4 3/1042])
OK ([22 1/28] [2 1/443] [-5 1/1393] [-10 1/11018])
OK ([22 1/38] [17 7/601] [10 7/8149])
OK ([44 1/57] [7 1/239] [-12 1/682] [24 1/12943])
OK ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/12943])
Error ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/21944])</pre>
=={{header|REXX}}==
Note: &nbsp; REXX doesn't have many high─order math functions, &nbsp; so a few of them are included here.
 
<lang rexx>/*REXX program evaluates some Machin─like formulas and verifies their veracity. */
Noticed: &nbsp; the test arguments specified for this Rosetta Code task need only '''nine''' decimal digits for verification, &nbsp; '''eight''' decimal digits is &nbsp; ''not'' &nbsp; enough to catch the "bad" equation. &nbsp; With this in mind, the REXX's &nbsp; ''decimal digit precision'' &nbsp; was increased to the number of decimal digits specified for the variable &nbsp; '''pi''' &nbsp; &nbsp; &nbsp; (which, for these cases, is a bit of overkill, but the difference in execution times were barely noticeable).
numeric fuzz 3; pi=pi(); @.=
 
@.1= 'pi/4 = atan(1/2) + atan(1/3)'
An extra formula was added to stress test the near exactness of a value.
@.2= 'pi/4 = 2*atan(1/3) + atan(1/7)'
<syntaxhighlight lang="rexx">/*REXX program evaluates some Machin─like formulas and verifies their veracity. */
@.3= 'pi/4 = 4*atan(1/5) - atan(1/239)'
@.=; pi= pi(); numeric digits( length(pi) ) - length(.); numeric fuzz 3
@.4= 'pi/4 = 5*atan(1/7) + 2*atan(3/79)'
say center(' computing with ' digits() " decimal digits ", 110, '═')
@.5= 'pi/4 = 5*atan(29/278) + 7*atan(3/79)'
@.61 = 'pi/4 = atan(1/2) + atan(1/5) + atan(1/83)'
@.72 = 'pi/4 = 42*atan(1/53) - atan(1/70) + atan(1/997)'
@.83 = 'pi/4 = 54*atan(1/75) +- 4*atan(1/53) + 2*atan(1/4443239)'
@.94 = 'pi/4 = 65*atan(1/87) + 2*atan(1/57) + atan(13/23979)'
@.5 = 'pi/4 = 5*atan(29/278) + 7*atan(3/79)'
@.6 = 'pi/4 = atan(1/2) + atan(1/5) + atan(1/8)'
@.7 = 'pi/4 = 4*atan(1/5) - atan(1/70) + atan(1/99)'
@.8 = 'pi/4 = 5*atan(1/7) + 4*atan(1/53) + 2*atan(1/4443)'
@.9 = 'pi/4 = 6*atan(1/8) + 2*atan(1/57) + atan(1/239)'
@.10= 'pi/4 = 8*atan(1/10) - atan(1/239) - 4*atan(1/515)'
@.11= 'pi/4 = 12*atan(1/18) + 8*atan(1/57) - 5*atan(1/239)'
Line 1,557 ⟶ 2,501:
@.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)'
@.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)'
@.18= 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 67.9999999994*atan(1/12943)'
 
do j=1 while @.j\=='' /*evaluate each "Machin─like" formulas.*/
interpret 'answer=' "(" @.j ')' /*where REXX does the heavy lifting. */
say right( word( 'bad OK', answer + 1), 3)": " @.j
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
pi: return 3.141592653589793238462643383279502884197169399375105820974944592307816406286
pi: return 3.141592653589793238462643383279502884197169399375105820974944592307816406186
Acos: procedure; parse arg x; return pi() * .5 - Asin(x)
Atan: procedure; arg x; if abs(x)=1 then return pi()/4*sign(x); return Asin(x/sqrt(1+x*x))
/*──────────────────────────────────────────────────────────────────────────────────────*/
Asin: procedure; parse arg x 1 z 1 o 1 p; a=abs(x); aa=a*a
if a>=sqrt(2)*.5 then return sign(x) * Acos( sqrt(1 - aa) )
do j=2 by 2 until p=z; p=z; o=o*aa*(j-1)/j; z=z+o/(j+1); end /*j*/; return z
Line 1,576 ⟶ 2,521:
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
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
'''output'''
<pre>
════════════════════════════════════ computing with 76 decimal digits ════════════════════════════════════
OK: pi/4 = atan(1/2) + atan(1/3)
OK: pi/4 = 2*atan(1/3) + atan(1/7)
Line 1,595 ⟶ 2,541:
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 = 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) + 67.9999999994*atan(1/12943)
</pre>
=={{header|RPL}}==
RPL does not support fat integers, therefore fat fractions neither, but the precision of floating-point numbers is actually sufficient to detect the incorrect formula and validate the other ones without using any trigonometric function. The 17 formulas to be checked are stored as strings in a global variable; each string is converted into a list of coefficients and real numbers, on which <code>tan(a+b)=(tan(a)+tan(b))/(1-tan(a)*tan(b))</code> is recursively applied, in a similar way to other languages.
{ "1*arctan(1/2) + 1*arctan(1/3)"
"2*arctan(1/3) + 1*arctan(1/7)"
"4*arctan(1/5) + -1*arctan(1/239)"
"5*arctan(1/7) + 2*arctan(3/79)"
"5*arctan(29/278) + 7*arctan(3/79)"
"1*arctan(1/2) + 1*arctan(1/5) + 1*arctan(1/8)"
"4*arctan(1/5) + -1*arctan(1/70) + 1*arctan(1/99)"
"5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443)"
"6*arctan(1/8) + 2*arctan(1/57) + 1*arctan(1/239)"
"8*arctan(1/10) + -1*arctan(1/239) + -4*arctan(1/515)"
"12*arctan(1/18) + 8*arctan(1/57) + -5*arctan(1/239)"
"16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042)"
"22*arctan(1/28) + 2*arctan(1/443) + -5*arctan(1/1393) + -10*arctan(1/11018)"
"22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149)"
"44*arctan(1/57) + 7*arctan(1/239) + -12*arctan(1/682) + 24*arctan(1/12943)"
"88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)"
"88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)"
} ''''Formulas'''' STO
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
IF DUP 1 == THEN DROP
ELSE IF DUP 0 < THEN NEG '''TanEval''' NEG
ELSE DUP2 2 / IP '''TanEval''' ROT ROT
DUP 2 / IP - '''TanEval'''
+ LAST * 1 - NEG /
END END
≫ ‘'''TanEval'''’ STO
DUP 2 GET →NUM OVER 1 GET →NUM '''TanEval''' SWAP
IF DUP SIZE 3 ≥ THEN
LIST→ 2 - →LIST ROT ROT DROP2
'''TanSum''' SWAP + LAST * 1 - NEG /
ELSE DROP END
≫ ‘'''TanSum'''’ STO
≪ DUP "+" POS → eq op
≪ IF op THEN eq op 1 + OVER SIZE SUB eq 1 op 1 - SUB
ELSE "" eq END
"'" DUP ROT SWAP + + STR→
≫ ≫ ''''PopTerm'''' STO
{} SWAP WHILE DUP "" ≠ REPEAT
'''PopTerm'''
ROT OVER 1 EXGET + SWAP DUP SIZE 1 - EXGET + SWAP
END DROP
≫ ‘'''ParsExp'''’ STO
≪ 1 CF 0
1 '''Formulas''' SIZE FOR f
'''Formulas''' f GET '''ParsExp TanSum'''
IF RND 1 == THEN 1 +
ELSE
1 SF "INCORRECT: π/4 ≠ "
'''Formulas''' f GET + SWAP
END NEXT
→STR IF 1 FS? THEN " others" + END " OK" +
≫ ‘'''TASK'''’ STO
|
'''TanEval''' ''( f c -- tan(c*arctan(f)) )''
if c = 1 then return f
else if c < 0 then return -tan(-c*arctan(f))
else put a = tan((c%2)*arctan(f)) at stack level 3
get b = tan((c-c%2)*arctan(f))
return (a+b)/(-(a*b-1))
'''TanSum''' ''( { c1 f1 .. cn fn } -- result )''
Get tan(c1*arctan(f1))
if input list > 2 items
make { c2 f2 .. cn fn }
evaluate tan of { c2..fn } and add it to
otherwise drop empty list
'''PopTerm''' ''( "T1+T2+..+Tn" -- "T2+..+Tn" 'T1' )''
If input string contains "+" then split it
else string contains only the last term
convert term into algebraic expression
'''ParsExp''' ''( "Machin formula" -- { c1 f1 .. cn fn } )''
Scan the formula
extract next term
extract c and f
Drop empty string
Initialize flag and counter
Scan list of formulas
evaluate tan(formula)
if ok, increase counter
else
build error message
with incorrect formula
Finalize report
|}
{{out}}
<pre>
2: "INCORRECT: π/4 ≠ 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)"
1: "16 others OK"
</pre>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
include "bigrat.s7i";
Line 1,671 ⟶ 2,731:
writeln;
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,695 ⟶ 2,755:
=={{header|Sidef}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">var equationtext = <<'EOT'
pi/4 = arctan(1/2) + arctan(1/3)
pi/4 = 2*arctan(1/3) + arctan(1/7)
Line 1,716 ⟶ 2,776:
 
func parse_eqn(equation) {
varstatic eqn_re = /%r{
( ^ \s* pi\/4 \s* = \s* )? # LHS of equation
(?: # RHS
\s* ( [+-+] )? \s*
(?: ( \d+ ) \s* \*)?
\s* arctan\( (\d+.*?) \/ (\d+) \)
)/}x
 
gather {
for lhs,sign,mult,numer,denomrat in (equation.findall(eqn_re)) {
take([
[+1, -1][sign == '-'] * (mult ? Num(mult) : 1),
Num(join('/', numer, denom ? denom : 1)rat)
])
}
}
}
 
func tanEval((1), f) { f }
 
func tanEval(coef {.is_neg}, f) {
-tanEval(-coef, f)
}
 
func tanEval(coef, f) {
varreturn caf =if (coef//2 == 1)
varreturn cb-tanEval(-coef, =f) if (coef-ca < 0)
var ca = coef>>1
var cb = (coef - ca)
var (a, b) = (tanEval(ca, f), tanEval(cb, f))
(a + b) / (1 - a*b)
}
 
func tans(xs {.len == 1}) {
tanEval(xs[0]...)
}
 
func tans(xs) {
var (aa, bb)xslen = @|(xs/2).len
return tanEval(xs[0]...) if (xslen == 1)
var (aa, bb) = xs.part(xslen>>1)
var (a, b) = (tans(aa), tans(bb))
(a + b) / (1 - a*b)
Line 1,762 ⟶ 2,816:
var ans = tans(machin)
printf("%5s: %s\n", (ans == 1 ? 'OK' : 'ERROR'), eqn)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,783 ⟶ 2,837:
ERROR: pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)
</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# Compute tan(atan(p)+atan(q)) using rationals
Line 1,863 ⟶ 2,916:
puts "No! '$formula' not true"
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,883 ⟶ 2,936:
Yes! 'pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)' is true
No! 'pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)' not true
</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
We already have a BigRat class so we use that.
<syntaxhighlight lang="wren">import "./big" for BigRat
import "./fmt" for Fmt
 
/** represents a term of the form: c * atan(n / d) */
class Term {
construct new(c, n, d) {
_c = c
_n = n
_d = d
}
 
c { _c }
n { _n }
d { _d }
 
toString {
var a = "atan(%(n)/%(d))"
return ((_c == 1) ? " + " :
(_c == -1) ? " - " :
(_c < 0) ? " - %(-c)*" : " + %(c)*") + a
}
}
 
var tanEval // recursive function
tanEval = Fn.new { |c, f|
if (c == 1) return f
if (c < 0) return -tanEval.call(-c, f)
var ca = (c/2).truncate
var cb = c - ca
var a = tanEval.call(ca, f)
var b = tanEval.call(cb, f)
return (a + b) / (BigRat.one - (a * b))
}
 
var tanSum // recursive function
tanSum = Fn.new { |terms|
if (terms.count == 1) return tanEval.call(terms[0].c, BigRat.new(terms[0].n, terms[0].d))
var half = (terms.count/2).floor
var a = tanSum.call(terms.take(half).toList)
var b = tanSum.call(terms.skip(half).toList)
return (a + b) / (BigRat.one - (a * b))
}
 
var T = Term // type alias
 
var termsList = [
[T.new(1, 1, 2), T.new(1, 1, 3)],
[T.new(2, 1, 3), T.new(1, 1, 7)],
[T.new(4, 1, 5), T.new(-1, 1, 239)],
[T.new(5, 1, 7), T.new(2, 3, 79)],
[T.new(5, 29, 278), T.new(7, 3, 79)],
[T.new(1, 1, 2), T.new(1, 1, 5), T.new(1, 1, 8)],
[T.new(4, 1, 5), T.new(-1, 1, 70), T.new(1, 1, 99)],
[T.new(5, 1, 7), T.new(4, 1, 53), T.new(2, 1, 4443)],
[T.new(6, 1, 8), T.new(2, 1, 57), T.new(1, 1, 239)],
[T.new(8, 1, 10), T.new(-1, 1, 239), T.new(-4, 1, 515)],
[T.new(12, 1, 18), T.new(8, 1, 57), T.new(-5, 1, 239)],
[T.new(16, 1, 21), T.new(3, 1, 239), T.new(4, 3, 1042)],
[T.new(22, 1, 28), T.new(2, 1, 443), T.new(-5, 1, 1393), T.new(-10, 1, 11018)],
[T.new(22, 1, 38), T.new(17, 7, 601), T.new(10, 7, 8149)],
[T.new(44, 1, 57), T.new(7, 1, 239), T.new(-12, 1, 682), T.new(24, 1, 12943)],
[T.new(88, 1, 172), T.new(51, 1, 239), T.new(32, 1, 682), T.new(44, 1, 5357), T.new(68, 1, 12943)],
[T.new(88, 1, 172), T.new(51, 1, 239), T.new(32, 1, 682), T.new(44, 1, 5357), T.new(68, 1, 12944)]
]
 
for (terms in termsList) {
var f = Fmt.swrite("$-5s: 1 == tan(", tanSum.call(terms) == BigRat.one)
System.write(f)
System.write(terms[0].toString.skip(3).join())
for (i in 1...terms.count) System.write(terms[i])
System.print(")")
}</syntaxhighlight>
 
{{out}}
<pre>
true : 1 == tan(atan(1/2) + atan(1/3))
true : 1 == tan(2*atan(1/3) + atan(1/7))
true : 1 == tan(4*atan(1/5) - atan(1/239))
true : 1 == tan(5*atan(1/7) + 2*atan(3/79))
true : 1 == tan(5*atan(29/278) + 7*atan(3/79))
true : 1 == tan(atan(1/2) + atan(1/5) + atan(1/8))
true : 1 == tan(4*atan(1/5) - atan(1/70) + atan(1/99))
true : 1 == tan(5*atan(1/7) + 4*atan(1/53) + 2*atan(1/4443))
true : 1 == tan(6*atan(1/8) + 2*atan(1/57) + atan(1/239))
true : 1 == tan(8*atan(1/10) - atan(1/239) - 4*atan(1/515))
true : 1 == tan(12*atan(1/18) + 8*atan(1/57) - 5*atan(1/239))
true : 1 == tan(16*atan(1/21) + 3*atan(1/239) + 4*atan(3/1042))
true : 1 == tan(22*atan(1/28) + 2*atan(1/443) - 5*atan(1/1393) - 10*atan(1/11018))
true : 1 == tan(22*atan(1/38) + 17*atan(7/601) + 10*atan(7/8149))
true : 1 == tan(44*atan(1/57) + 7*atan(1/239) - 12*atan(1/682) + 24*atan(1/12943))
true : 1 == tan(88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12943))
false: 1 == tan(88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12944))
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code ChOut=8, Text=12; \intrinsic routines
int Number(18); \numbers from equations
def LF=$0A; \ASCII line feed (end-of-line character)
Line 1,981 ⟶ 3,132:
repeat ChOut(0, SS(0)); SS:= SS+1 until SS(0)=LF; ChOut(0, LF); \show equation
];
]</langsyntaxhighlight>
 
{{out}}
9,476

edits