Set of real numbers: Difference between revisions

Added FreeBASIC
m (→‎{{header|Wren}}: Minor tidy)
(Added FreeBASIC)
 
Line 843:
set3(2.0r).assertTrue();
}</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{incomplete|FreeBASIC|Despite my efforts, the set difference results are erroneous.
 
I would appreciate help.}}
 
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Type Func
As Integer ID
As Double ARGS(2)
End Type
 
Declare Function cf(f As Func, x As Double) As Boolean
Declare Function Union_(a As Func, b As Func, x As Double) As Boolean
Declare Function Inters(a As Func, b As Func, x As Double) As Boolean
Declare Function Differ(a As Func, b As Func, x As Double) As Boolean
Declare Function OpOp(a As Double, b As Double, x As Double) As Boolean
Declare Function ClCl(a As Double, b As Double, x As Double) As Boolean
Declare Function OpCl(a As Double, b As Double, x As Double) As Boolean
Declare Function ClOp(a As Double, b As Double, x As Double) As Boolean
Declare Function aspxx(a As Double) As Boolean
Declare Function aspx(a As Double) As Boolean
 
Function cf(f As Func, x As Double) As Boolean
Select Case f.ID
Case 1: Return OpOp(f.ARGS(0), f.ARGS(1), x)
Case 2: Return ClCl(f.ARGS(0), f.ARGS(1), x)
Case 3: Return OpCl(f.ARGS(0), f.ARGS(1), x)
Case 4: Return ClOp(f.ARGS(0), f.ARGS(1), x)
'Extra credit
Case 5: Return OpOp(f.ARGS(0), f.ARGS(1), x) And aspxx(x)
Case 6: Return OpOp(f.ARGS(0), f.ARGS(1), x) And aspx(x)
End Select
End Function
 
Function Union_(a As Func, b As Func, x As Double) As Boolean
Return cf(a, x) Or cf(b, x)
End Function
 
Function Inters(a As Func, b As Func, x As Double) As Boolean
Return cf(a, x) And cf(b, x)
End Function
 
Function Differ(a As Func, b As Func, x As Double) As Boolean
Return cf(a, x) And (Not cf(b, x))
End Function
 
Function OpOp(a As Double, b As Double, x As Double) As Boolean
Return a < x And x < b
End Function
 
Function ClCl(a As Double, b As Double, x As Double) As Boolean
Return a <= x And x <= b
End Function
 
Function OpCl(a As Double, b As Double, x As Double) As Boolean
Return a < x And x <= b
End Function
 
Function ClOp(a As Double, b As Double, x As Double) As Boolean
Return a <= x And x < b
End Function
 
'Extra credit
Function aspxx(a As Double) As Boolean
Return Abs(Sin(3.14159 * a * a)) > 0.5
End Function
 
Function aspx(a As Double) As Boolean
Return Abs(Sin(3.14159 * a)) > 0.5
End Function
 
' Set definitions and test methods
Dim As Func s(6, 2)
s(1, 0).ID = 3: s(1, 0).ARGS(0) = 0: s(1, 0).ARGS(1) = 1
s(1, 1).ID = 4: s(1, 1).ARGS(0) = 0: s(1, 1).ARGS(1) = 2
s(2, 0).ID = 4: s(2, 0).ARGS(0) = 0: s(2, 0).ARGS(1) = 2
s(2, 1).ID = 3: s(2, 1).ARGS(0) = 1: s(2, 1).ARGS(1) = 2
s(3, 0).ID = 4: s(3, 0).ARGS(0) = 0: s(3, 0).ARGS(1) = 3
s(3, 1).ID = 1: s(3, 1).ARGS(0) = 0: s(3, 1).ARGS(1) = 1
s(4, 0).ID = 4: s(4, 0).ARGS(0) = 0: s(4, 0).ARGS(1) = 3
s(4, 1).ID = 2: s(4, 1).ARGS(0) = 0: s(4, 1).ARGS(1) = 1
s(5, 0).ID = 2: s(5, 0).ARGS(0) = 0: s(5, 0).ARGS(1) = 0
'Extra credit
s(6, 1).ID = 5: s(6, 1).ARGS(0) = 0: s(6, 1).ARGS(1) = 10
s(6, 2).ID = 6: s(6, 2).ARGS(0) = 0: s(6, 2).ARGS(1) = 10
 
Dim As Integer i, x, r
For x = 0 To 2
i = 1
r = Union_(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
For x = 0 To 2
i = 2
r = Inters(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
For x = 0 To 2
i = 3
r = Differ(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
For x = 0 To 2
i = 4
r = Differ(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
 
x = 0
i = 5
r = Differ(s(i, 1), s(i, 2), x)
Print Using "[#_,#] is empty : &"; s(i, 0).ARGS(0); s(i, 0).ARGS(1); Cbool(r)
Print
 
'Extra credit
Dim As Double z = 0, paso = 0.00001
Dim As Integer count = 0
While z <= 10
If Differ(s(6, 1), s(6, 2), z) Then count += 1
z += paso
Wend
Print "Approximate length of A-B: "; count * paso
 
Sleep</syntaxhighlight>
{{out}}
<pre>0 in (0,1] u [0,2) : true
1 in (0,1] u [0,2) : true
2 in (0,1] u [0,2) : false
 
0 in (0,2] u [1,2) : false
1 in (0,2] u [1,2) : false
2 in (0,2] u [1,2) : false
 
0 in (0,3] u [0,2) : false
1 in (0,3] u [0,2) : false
2 in (0,3] u [0,2) : false
 
0 in (0,3] u [0,2) : true
1 in (0,3] u [0,2) : true
2 in (0,3] u [0,2) : false
 
[0,0] is empty : false
 
Approximate length of A-B: 2.07586</pre>
 
=={{header|F#|F sharp}}==
2,122

edits