Anonymous user
Set of real numbers: Difference between revisions
→{{header|Ruby}}: bug fix, change display format.
(→{{header|Ruby}}: add length method and Optional work) |
(→{{header|Ruby}}: bug fix, change display format.) |
||
Line 1,233:
end
def to_s
"
end
end
Line 1,251:
end
def self.
raise ArgumentError unless str =~ /(\[|\()(.+),(.+)(\]|\))/
b0, lo, hi, b1 = $~.captures # $~ : Regexp.last_match
Line 1,261:
end
def
super
end
Line 1,362 ⟶ 1,363:
def ==(other)
end
Line 1,371:
def to_s
"#{self.class}
end
alias inspect to_s
Line 1,424:
test_set.each do |sa,ope,sb|
str = "#{sa} #{ope} #{sb}"
e = eval("Rset.
puts "%s -> %s" % [str, e]
(0..2).each{|i| puts " #{i} : #{e.include?(i)}"}
Line 1,440:
inf = 1.0 / 0.0 # infinity
puts "a = #{a = Rset(-inf,inf)}"
puts "b = #{b = Rset.
puts "a - b -> #{a - b}"</lang>
Line 1,451:
Rset[0,2) | Rset(1,3) -> Rset[0,3)
Rset[0,1) | Rset(2,3] -> Rset[0,1)
Rset[0,2) & Rset(1,3) -> Rset(1,2)
Line 1,459:
Rset[0,2) - Rset(1,3) -> Rset[0,1]
Rset[0,2) - Rset[0,2) -> Rset
Rset(0,3] - Rset[1,2] -> Rset(0,1)
Test :
Line 1,470:
1 : false
2 : false
[0, 3) - (0, 1) -> Rset[0,0]
0 : true
1 : true
Line 1,479:
2 : true
x = Rset[0,2] | Rset(3,7) | Rset[8,10] -> Rset[0,2]
y = Rset(7,9) | Rset(5,6) | Rset[1,4] -> Rset[1,4]
x | y -> Rset[0,7)
x & y -> Rset[1,2]
x - y -> Rset[0,1)
y - x -> Rset(2,3]
x ^ y -> Rset[0,1)
y ^ x == (x | y) - (x & y) -> true
a = Rset(-Infinity,Infinity)
b = Rset[1/3,11/7)
a - b -> Rset(-Infinity,1/3)
</pre>
'''Optional work:'''
{{works with|Ruby|2.1+}}
(with Rational suffix.)
<lang ruby>str, e = "e = Rset.new", nil
puts "#{str} -> #{eval(str)}\t\t# create empty set"
Line 1,532 ⟶ 1,533:
e.empty? -> true
a : Rset(0.4082482904638631,0.912870929175277)
a.length : 6.50103079235655
b : Rset(1/6,5/6)
b.length : 20/3
a - b : Rset[5/6,0.912870929175277)
(a-b).length : 2.0758648411846745
</pre>
|