Set of real numbers: Difference between revisions

m (→‎{{header|Perl 6}}: 'singularizer' not needed)
Line 1,578:
(9.9582461641931,9.99166319154791)
Length A − B = 2.07586484118467</pre>
 
=={{header|Phix}}==
{{trans|Go}}
<lang Phix>enum ID,ARGS
function cf(sequence f, atom x) return call_func(f[ID],f[ARGS]&x) end function
function Union(sequence a, b, atom x) return cf(a,x) or cf(b,x) end function constant r_Union = routine_id("Union")
function Inter(sequence a, b, atom x) return cf(a,x) and cf(b,x) end function constant r_Inter = routine_id("Inter")
function Diffr(sequence a, b, atom x) return cf(a,x) and not cf(b,x) end function constant r_Diffr = routine_id("Diffr")
function OpOp(atom a, b, x) return a < x and x < b end function constant r_OpOp = routine_id("OpOp")
function ClCl(atom a, b, x) return a <= x and x <= b end function constant r_ClCl = routine_id("ClCl")
function OpCl(atom a, b, x) return a < x and x <= b end function constant r_OpCl = routine_id("OpCl")
function ClOp(atom a, b, x) return a <= x and x < b end function constant r_ClOp = routine_id("ClOp")
procedure assert(bool res) if not res then crash("error") end if end procedure
-- expected
-- ---- desc ----, 0 1 2, --------------- set method ---------------
constant s = {{"(0,1] u [0,2)",{1,1,0},{r_Union,{{r_OpCl,{0,1}},{r_ClOp,{0,2}}}}},
{"[0,2) n (1,2]",{0,0,0},{r_Inter,{{r_ClOp,{0,2}},{r_OpCl,{1,2}}}}},
{"[0,3) - (0,1)",{1,1,1},{r_Diffr,{{r_ClOp,{0,3}},{r_OpOp,{0,1}}}}},
{"[0,3) - [0,1]",{0,0,1},{r_Diffr,{{r_ClOp,{0,3}},{r_ClCl,{0,1}}}}}},
tf = {"True","False"}
 
for i=1 to length(s) do
sequence {desc, expect, method} = s[i]
for x=0 to 2 do
bool r = cf(method,x)
assert(r==expect[x+1])
printf(1,"%d in %s : %s\n", {x, desc, tf[2-r]})
end for
printf(1,"\n")
end for</lang>
{{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) n (1,2] : False
1 in [0,2) n (1,2] : False
2 in [0,2) n (1,2] : False
 
0 in [0,3) - (0,1) : True
1 in [0,3) - (0,1) : True
2 in [0,3) - (0,1) : True
 
0 in [0,3) - [0,1] : False
1 in [0,3) - [0,1] : False
2 in [0,3) - [0,1] : True
</pre>
Extra credit - also translated from Go, but with an extended loop and crude summation, inspired by Java/Kotlin.
<lang Phix>function aspxx(atom x) return abs(sin(PI*x*x))>0.5 end function constant r_aspxx = routine_id("aspxx")
function aspx(atom x) return abs(sin(PI*x))>0.5 end function constant r_aspx = routine_id("aspx")
 
constant A = {r_Inter,{{r_OpOp,{0,10}},{r_aspxx,{}}}},
B = {r_Inter,{{r_OpOp,{0,10}},{r_aspx,{}}}},
C = {r_Diffr,{A,B}}
atom x = 0, step = 0.00001, count = 0
while x<=10 do
count += cf(C,x)
x += step
end while
printf(1,"Approximate length of A-B: %.5f\n",{count*step})</lang>
{{out}}
<pre>
Approximate length of A-B: 2.07587
</pre>
 
=={{header|Python}}==
7,820

edits