Class: UnboundedFixnum

Inherits:
Numeric
  • Object
show all
Includes:
Comparable, Constants
Defined in:
app/models/unbounded_fixnum.rb

Overview

Copyright (C) 2010-2012 by Greg Lawson

<[email protected]>

Copyright: See COPYING file that comes with this distribution

Extention of FixNum class to unbounded limits nil means unbounded (i.e. infinity)

Defined Under Namespace

Modules: Constants

Constant Summary

Constants included from Constants

Constants::Inf, Constants::Neg_inf

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, infinity_sign = nil) ⇒ UnboundedFixnum

infinity_sign is only needed for number==nil


14
15
16
17
18
19
20
21
22
23
# File 'app/models/unbounded_fixnum.rb', line 14

def initialize(number, infinity_sign=nil)
	raise "In UnboundedFixnum.new infinities must have a infinity_sign" if number.nil? && infinity_sign.nil?
	if number.instance_of?(UnboundedFixnum) then
		@fixnum=number.to_i
		@infinity_sign=number.infinity_sign
	else	
		@fixnum=number
		@infinity_sign=infinity_sign
	end #if
end

Instance Attribute Details

#infinity_signObject (readonly)

Returns the value of attribute infinity_sign


12
13
14
# File 'app/models/unbounded_fixnum.rb', line 12

def infinity_sign
  @infinity_sign
end

Class Method Details

.promote(other, infinity_sign = nil) ⇒ Object

or coerce


30
31
32
33
34
35
36
# File 'app/models/unbounded_fixnum.rb', line 30

def UnboundedFixnum.promote(other, infinity_sign=nil)
	if other.instance_of?(UnboundedFixnum) then
		UnboundedFixnum.new(other.to_i, other.infinity_sign)
	else
		UnboundedFixnum.new(other, infinity_sign)
	end #if
end

Instance Method Details

#+(rhs) ⇒ Object

UnboundedFixnum_compare


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/unbounded_fixnum.rb', line 90

def +(rhs)
	if !rhs.instance_of?(UnboundedFixnum) then
		rhs=UnboundedFixnum.new(rhs)
		rhs_unbounded=rhs.unbounded?
	else
		rhs_unbounded=rhs.unbounded?
	end #if
	lhs_unbounded=self.unbounded?
	case [lhs_unbounded, rhs_unbounded] 
	when [nil,nil]
		return UnboundedFixnum.new(self.to_i + rhs.to_i)
	when [+1,+1], [-1,-1]
		return self
	when [nil,-1], [-1, nil]
		return UnboundedFixnum::Neg_inf
	when [nil,+1], [+1, nil]
		return UnboundedFixnum::Inf
	when [-1,+1], [+1,-1]
		return nil # could be any value
	end #if
end

#<=>(rhs) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/unbounded_fixnum.rb', line 77

def <=>(rhs)
	rhs=UnboundedFixnum.promote(rhs)
	case [self.unbounded?, rhs.unbounded?] 
	when [nil,nil]
		return self.to_i <=> rhs.to_i
	when [+1,+1], [-1,-1]
		return 0
	when [nil,+1], [-1, nil], [-1,+1]
		return -1
	when [nil,-1], [+1, nil], [+1,-1]
		return +1
	end #if
end

#==(rhs) ⇒ Object

no coercion of argument as in Numeric


74
75
76
# File 'app/models/unbounded_fixnum.rb', line 74

def ==(rhs)
	eql?(rhs)
end

#eql?(rhs) ⇒ Boolean

unbounded


63
64
65
66
67
68
69
70
71
72
# File 'app/models/unbounded_fixnum.rb', line 63

def eql?(rhs)
	lhs=self
 	if lhs.unbounded? && rhs.unbounded? then
		true
 	elsif lhs.to_i == rhs.to_i then
		true
	else
		false
	end #if
end

#inspectObject

to_s


47
48
49
# File 'app/models/unbounded_fixnum.rb', line 47

def inspect
	return to_s
end

#integer?Boolean

inspect


50
51
52
# File 'app/models/unbounded_fixnum.rb', line 50

def integer? # for Numeric Class
	return true 
end

#to_iObject

integer


53
54
55
# File 'app/models/unbounded_fixnum.rb', line 53

def to_i
	return @fixnum
end

#to_sObject

promote


37
38
39
40
41
42
43
44
45
46
# File 'app/models/unbounded_fixnum.rb', line 37

def to_s
	case self
	when Inf
		return 'Inf'
	when Neg_inf
		return 'Neg_inf'
	else
		return @fixnum
	end #case
end

#unbounded?Boolean

to_i


56
57
58
59
60
61
62
# File 'app/models/unbounded_fixnum.rb', line 56

def unbounded?
	if @fixnum.nil? then
		@infinity_sign
	else
		nil
	end #if
end