Class: Keisan::AST::Number

Inherits:
ConstantLiteral show all
Defined in:
lib/keisan/ast/number.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConstantLiteral

#!, #==, #and, #evaluate, #is_constant?, #or, #to_s

Methods inherited from Node

#!, #and, #coerce, #contains_a?, #deep_dup, #differentiated, #evaluate, #evaluate_assignments, #evaluated, #false?, #is_constant?, #or, #replace, #replaced, #simplified, #to_cell, #to_node, #traverse, #true?, #unbound_functions, #unbound_variables, #well_defined?

Constructor Details

#initialize(number) ⇒ Number

Returns a new instance of Number.



6
7
8
9
10
11
12
13
# File 'lib/keisan/ast/number.rb', line 6

def initialize(number)
  @number = number
  # Reduce the number if possible
  case @number
  when Rational
    @number = @number.numerator if @number.denominator == 1
  end
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/keisan/ast/number.rb', line 4

def number
  @number
end

Instance Method Details

#%(other) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/keisan/ast/number.rb', line 75

def %(other)
  other = other.to_node
  case other
  when Number
    Number.new(value % other.value)
  else
    super
  end
end

#&(other) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/keisan/ast/number.rb', line 85

def &(other)
  other = other.to_node
  case other
  when Number
    Number.new(value & other.value)
  else
    super
  end
end

#*(other) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/keisan/ast/number.rb', line 45

def *(other)
  other = other.to_node
  case other
  when Number
    Number.new(value * other.value)
  else
    super
  end
end

#**(other) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/keisan/ast/number.rb', line 65

def **(other)
  other = other.to_node
  case other
  when Number
    Number.new(value ** other.value)
  else
    super
  end
end

#+(other) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/keisan/ast/number.rb', line 27

def +(other)
  other = other.to_node
  case other
  when Number
    Number.new(value + other.value)
  when Date
    Date.new(other.value + value)
  when Time
    Time.new(other.value + value)
  else
    super
  end
end

#+@Object



23
24
25
# File 'lib/keisan/ast/number.rb', line 23

def +@
  Number.new(value)
end

#-(other) ⇒ Object



41
42
43
# File 'lib/keisan/ast/number.rb', line 41

def -(other)
  self + (-other.to_node)
end

#-@Object



19
20
21
# File 'lib/keisan/ast/number.rb', line 19

def -@
  Number.new(-value)
end

#/(other) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/keisan/ast/number.rb', line 55

def /(other)
  other = other.to_node
  case other
  when Number
    Number.new(Rational(value, other.value))
  else
    super
  end
end

#<(other) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/keisan/ast/number.rb', line 159

def <(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value < other.value)
  else
    super
  end
end

#<<(other) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/keisan/ast/number.rb', line 119

def <<(other)
  other = other.to_node
  case other
  when Number
    Number.new(value << other.value)
  else
    super
  end
end

#<=(other) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/keisan/ast/number.rb', line 169

def <=(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value <= other.value)
  else
    super
  end
end

#>(other) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/keisan/ast/number.rb', line 139

def >(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value > other.value)
  else
    super
  end
end

#>=(other) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/keisan/ast/number.rb', line 149

def >=(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value >= other.value)
  else
    super
  end
end

#>>(other) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/keisan/ast/number.rb', line 129

def >>(other)
  other = other.to_node
  case other
  when Number
    Number.new(value >> other.value)
  else
    super
  end
end

#^(other) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/keisan/ast/number.rb', line 99

def ^(other)
  other = other.to_node
  case other
  when Number
    Number.new(value ^ other.value)
  else
    super
  end
end

#differentiate(variable, context = nil) ⇒ Object



210
211
212
# File 'lib/keisan/ast/number.rb', line 210

def differentiate(variable, context = nil)
  0.to_node
end

#equal(other) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/keisan/ast/number.rb', line 179

def equal(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value == other.value)
  else
    super
  end
end

#not_equal(other) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/keisan/ast/number.rb', line 189

def not_equal(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value != other.value)
  else
    super
  end
end

#simplify(context = nil) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/keisan/ast/number.rb', line 199

def simplify(context = nil)
  case number
  when Rational
    if number.denominator == 1
      @number = number.numerator
    end
  end

  self
end

#value(context = nil) ⇒ Object



15
16
17
# File 'lib/keisan/ast/number.rb', line 15

def value(context = nil)
  number
end

#|(other) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/keisan/ast/number.rb', line 109

def |(other)
  other = other.to_node
  case other
  when Number
    Number.new(value | other.value)
  else
    super
  end
end

#~Object



95
96
97
# File 'lib/keisan/ast/number.rb', line 95

def ~
  Number.new(~value)
end