Set of real numbers: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output section.)
Line 1,647: Line 1,647:
=={{header|REXX}}==
=={{header|REXX}}==
===no error checking, no ∞===
===no error checking, no ∞===
<lang rexx>/*REXX pgm demonstrates a way to represent any set of real #s and usage.*/
<lang rexx>/*REXX program demonstrates a way to represent any set of real numbers and usage. */
call set_query , , '(0,1] union [0,2)'
call quertySet 1, 3, '[1,2)'
call set_query , , '[0,2) (1,3)'
call quertySet , , '[0,2) union (1,3)'
call set_query , , '[0,3] - (0,1)'
call quertySet , , '[0,1) union (2,3]'
call set_query , , '[0,3] - [0,1]'
call quertySet , , '[0,2] inter (1,3)'
exit /*stick a fork in it, we're done.*/
call quertySet , , '(1,2) (2,3]'
call quertySet , , '[0,2) \ (1,3)'
/*──────────────────────────────────SET_empty subroutine────────────────*/
say; say center(' start of required tasks ', 40, "═")
set_empty: parse arg _; nam=set_val(_,00); return @.3>@.4
call quertySet , , '(0,1] union [0,2)'
/*──────────────────────────────────SET_ISIN subroutine─────────────────*/
set_isin: parse arg #,x; call set_val x
call quertySet , , '[0,2) (1,3)'
call quertySet , , '[0,3] - (0,1)'
if \datatype(#,'N') then call set_bad "number isn't not numeric:" #
if (@.1=='(' & #<=@.2) |,
call quertySet , , '[0,3] - [0,1]'
(@.1=='[' & #< @.2) |,
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
(@.4==')' & #>=@.3) |,
(@.4==']' & #> @.3) then return 0
emptySet: parse arg _; nam= valSet(_, 00); return @.3>@.4
/*──────────────────────────────────────────────────────────────────────────────────────*/
return 1
isInSet: parse arg #,x; call valSet x
/*──────────────────────────────────SET_query subroutine────────────────*/
if \datatype(#, 'N') then call set_bad "number isn't not numeric:" #
set_query: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
if lv=='' then lv=0; if hv=='' then hv=2; if op=='' then cop=0
if (@.1=='(' & #<=@.2) |,
if wordpos(op,'| or UNION') \==0 then cop='|'
(@.1=='[' & #< @.2) |,
(@.4==')' & #>=@.3) |,
if wordpos(op,'& ∩ AND INTER INTERSECTION') \==0 then cop='&'
if wordpos(op,'\ - DIF DIFF DIFFERENCE') \==0 then cop='\'
(@.4==']' & #> @.3) then return 0
return 1
say
/*──────────────────────────────────────────────────────────────────────────────────────*/
do i=lv to hv; b =set_isin(i,s1)
quertySet: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
if cop\==0 then do
b2=set_isin(i,s2)
if lv=='' then lv=0; if hv=='' then hv= 2; if op=='' then cop= 0
if cop=='&' then b=b & b2
if wordpos(op, '| or UNION') \==0 then cop= "|"
if cop=='|' then b=b | b2
if wordpos(op, '& ∩ AND INTER INTERSECTION') \==0 then cop= "&"
if cop=='\' then b=b & \b2
if wordpos(op, '\ - DIF DIFF DIFFERENCE') \==0 then cop= "\"
end
say
do i=lv to hv; b = isInSet(i, s1)
express = s1 center(oop,max(5,length(oop))) s2
say right(i,5) ' is in set' express": " word('no yes',b+1)
if cop\==0 then do
end /*i*/
b2= isInSet(i, s2)
if cop=='&' then b= b & b2
return
if cop=='|' then b= b | b2
/*──────────────────────────────────SET_VAL subroutine──────────────────*/
set_val: parse arg q; q=space(q,0); L=length(q); @.0=','
if cop=='\' then b= b & \b2
end
@.4=right(q,1)
express = s1 center(oop, max(5, length(oop) ) ) s2
parse var q @.1 2 @.2 ',' @.3 (@.4)
say right(i, 5) ' is in set' express": " word('no yes', b+1)
if @.2>@.3 then parse var L . @.0 @.2 @.3
end /*i*/
return space(@.1 @.2 @.0 @.3 @.4,0)</lang>
return
'''output''' is the same as version 2
/*──────────────────────────────────────────────────────────────────────────────────────*/
valSet: parse arg q; q=space(q, 0); L=length(q); @.0= ','; @.4= right(q,1)
parse var q @.1 2 @.2 ',' @.3 (@.4)
if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4, 0)</lang>
{{out|output|text=&nbsp; is the same as the next REXX version (below).}}


===has error checking, ∞ support===
===has error checking, ∞ support===
<lang rexx>/*REXX pgm demonstrates a way to represent any set of real #s and usage.*/
<lang rexx>/*REXX program demonstrates a way to represent any set of real numbers and usage. */
call set_query , , '(0,1] union [0,2)'
call quertySet 1, 3, '[1,2)'
call set_query , , '[0,2) (1,3)'
call quertySet , , '[0,2) union (1,3)'
call set_query , , '[0,3] - (0,1)'
call quertySet , , '[0,1) union (2,3]'
call set_query , , '[0,3] - [0,1]'
call quertySet , , '[0,2] inter (1,3)'
exit /*stick a fork in it, we're done.*/
call quertySet , , '(1,2) (2,3]'
call quertySet , , '[0,2) \ (1,3)'
/*──────────────────────────────────SET_BAD subroutine─────────-────────*/
say; say center(' start of required tasks ', 40, "═")
set_bad: say; say '***error!*** bad format of SET_def: ('arg(1)")"; exit
call quertySet , , '(0,1] union [0,2)'
/*──────────────────────────────────SET_empty subroutine────────────────*/
set_empty: parse arg _; nam=set_val(_,00); return @.3>@.4
call quertySet , , '[0,2) (1,3)'
call quertySet , , '[0,3] - (0,1)'
/*──────────────────────────────────SET_ISIN subroutine─────────────────*/
set_isin: parse arg #,x; call set_val x
call quertySet , , '[0,3] - [0,1]'
exit /*stick a fork in it, we're all done. */
if \datatype(#,'N') then call set_bad "number isn't not numeric:" #
/*──────────────────────────────────────────────────────────────────────────────────────*/
if (@.1=='(' & #<=@.2) |,
(@.1=='[' & #< @.2) |,
badSet: say; say '***error*** bad format of SET_def: ('arg(1)")"; exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
(@.4==')' & #>=@.3) |,
(@.4==']' & #> @.3) then return 0
emptySet: parse arg _; nam= valSet(_, 00); return @.3>@.4
/*──────────────────────────────────────────────────────────────────────────────────────*/
return 1
isInSet: parse arg #,x; call valSet x
/*──────────────────────────────────SET_query subroutine────────────────*/
if \datatype(#, 'N') then call set_bad "number isn't not numeric:" #
set_query: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
if lv=='' then lv=0; if hv=='' then hv=2; if op=='' then cop=0
if (@.1=='(' & #<=@.2) |,
if wordpos(op,'| or UNION') \==0 then cop='|'
(@.1=='[' & #< @.2) |,
(@.4==')' & #>=@.3) |,
if wordpos(op,'& ∩ AND INTER INTERSECTION') \==0 then cop='&'
if wordpos(op,'\ - DIF DIFF DIFFERENCE') \==0 then cop='\'
(@.4==']' & #> @.3) then return 0
return 1
if cop=='' then call set_bad 'invalid operation:' oop
/*──────────────────────────────────────────────────────────────────────────────────────*/
say
quertySet: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
do i=lv to hv; b =set_isin(i,s1)
if cop\==0 then do
if lv=='' then lv=0; if hv=='' then hv= 2; if op=='' then cop= 0
b2=set_isin(i,s2)
if wordpos(op, '| or UNION') \==0 then cop= "|"
if cop=='&' then b=b & b2
if wordpos(op, '& ∩ AND INTER INTERSECTION') \==0 then cop= "&"
if cop=='|' then b=b | b2
if wordpos(op, '\ - DIF DIFF DIFFERENCE') \==0 then cop= "\"
if cop=='\' then b=b & \b2
say
end
do i=lv to hv; b = isInSet(i, s1)
if cop\==0 then do
express = s1 center(oop,max(5,length(oop))) s2
say right(i,5) ' is in set' express": " word('no yes',b+1)
b2= isInSet(i, s2)
if cop=='&' then b= b & b2
end /*i*/
if cop=='|' then b= b | b2
return
if cop=='\' then b= b & \b2
/*──────────────────────────────────SET_VAL subroutine──────────────────*/
set_val: parse arg q; q=space(q,0); L=length(q); @.0=','
end
express = s1 center(oop, max(5, length(oop) ) ) s2
infinity = copies(9,digits()-1)'e'copies(9,digits()-1)'0'
if L<2 then call set_bad 'invalid expression'
say right(i, 5) ' is in set' express": " word('no yes', b+1)
end /*i*/
@.4=right(q,1)
parse var q @.1 2 @.2 ',' @.3 (@.4)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
if @.1\=='(' & @.1\=='[' then call set_bad 'left boundry'
valSet: parse arg q; q=space(q, 0); L= length(q); @.0= ','
if @.4\==')' & @.4\==']' then call set_bad 'right boundry'
infinity = copies(9, digits() - 1)'e'copies(9, digits() - 1)0
if L<2 then call set_bad 'invalid expression'
@.4= right(q, 1)
parse var q @.1 2 @.2 ',' @.3 (@.4)
if @.1\=='(' & @.1\=="[" then call set_bad 'left boundry'
if @.4\==')' & @.4\=="]" then call set_bad 'right boundry'
do j=2 to 3; u=@.j; upper u
if right(@.j, 1)=='∞' | u="INFINITY" then @.j= '-'infinity
if \datatype(@.j, 'N') then call set_bad "value not numeric:" @.j
end /*j*/
if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4, 0)</lang>
{{out|output|text=&nbsp; when using the (internal) default inputs:}}
<pre>
1 is in set [1,2) : yes
2 is in set [1,2) : no
3 is in set [1,2) : no


do j=2 to 3; u=@.j; upper u
0 is in set [0,2) union (1,3): yes
1 is in set [0,2) union (1,3): yes
if right(@.j,1)=='∞' | u="INFINITY" then @.j='-'infinity
2 is in set [0,2) union (1,3): yes
if \datatype(@.j,'N') then call set_bad 'value not numeric:' @.j

end /*j*/
0 is in set [0,1) union (2,3]: yes
1 is in set [0,1) union (2,3]: no
2 is in set [0,1) union (2,3]: no

0 is in set [0,2] inter (1,3): no
1 is in set [0,2] inter (1,3): no
2 is in set [0,2] inter (1,3): yes

0 is in set (1,2) ∩ (2,3]: no
1 is in set (1,2) ∩ (2,3]: no
2 is in set (1,2) ∩ (2,3]: no

0 is in set [0,2) \ (1,3): yes
1 is in set [0,2) \ (1,3): yes
2 is in set [0,2) \ (1,3): no

═══════ start of required tasks ════════


if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4,0)</lang>
'''output'''
<pre style="overflow:scroll">
0 is in set (0,1] union [0,2): yes
0 is in set (0,1] union [0,2): yes
1 is in set (0,1] union [0,2): yes
1 is in set (0,1] union [0,2): yes