Module: SchemeNumbers
Overview
Instance Method Summary
collapse
#compare_value_arithmetic, #convert_to_num, #divide_number, #divide_special_convert, #find_idx_numerators, #get_num_denom, #get_one_arg_function, #num_denom_helper, #num_denom_validator, #rationalize_num
Instance Method Details
99
100
101
102
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 99
def *(other)
other = convert_to_num other
other.reduce(1, :*)
end
|
87
88
89
90
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 87
def +(other)
other = convert_to_num other
other.reduce(0, :+)
end
|
92
93
94
95
96
97
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 92
def -(other)
return 0 if other.empty?
other = convert_to_num other
return -other[0] if other.size == 1
other[0] + other[1..-1].reduce(0, :-)
end
|
104
105
106
107
108
109
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 104
def /(other)
raise arg_err_build 'at least 1', 0 if other.empty?
other = divide_special_convert other
return (divide_number 1, other[0].to_num) if other.size == 1
other[1..-1].inject(other[0]) { |res, t| divide_number res, t }
end
|
71
72
73
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 71
def <(other)
compare_value_arithmetic other, '<'
end
|
#<=(other) ⇒ Object
79
80
81
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 79
def <=(other)
compare_value_arithmetic other, '<='
end
|
75
76
77
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 75
def >(other)
compare_value_arithmetic other, '>'
end
|
#>=(other) ⇒ Object
83
84
85
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 83
def >=(other)
compare_value_arithmetic other, '>='
end
|
#abs(other) ⇒ Object
142
143
144
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 142
def abs(other)
(get_one_arg_function other).abs
end
|
#add1(other) ⇒ Object
146
147
148
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 146
def add1(other)
(get_one_arg_function other) + 1
end
|
#denominator(other) ⇒ Object
136
137
138
139
140
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 136
def denominator(other)
raise arg_err_build 1, 0 if other.empty?
nums = num_denom_helper other
nums[1].to_num
end
|
#max(other) ⇒ Object
160
161
162
163
164
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 160
def max(other)
raise arg_err_build 'at least 1', 0 if other.empty?
other = convert_to_num other
other.max
end
|
#min(other) ⇒ Object
154
155
156
157
158
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 154
def min(other)
raise arg_err_build 'at least 1', 0 if other.empty?
other = convert_to_num other
other.min
end
|
#modulo(other) ⇒ Object
124
125
126
127
128
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 124
def modulo(other)
raise arg_err_build 2, other.size if other.size != 2
x, y = convert_to_num other
x.modulo y
end
|
#numerator(other) ⇒ Object
130
131
132
133
134
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 130
def numerator(other)
raise arg_err_build 1, 0 if other.empty?
nums = num_denom_helper other
nums[0].to_num
end
|
#quotient(other) ⇒ Object
111
112
113
114
115
116
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 111
def quotient(other)
raise arg_err_build 2, other.size if other.size != 2
x, y = convert_to_num other
result = divide_number x, y
result < 0 ? result.ceil : result.floor
end
|
#remainder(other) ⇒ Object
118
119
120
121
122
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 118
def remainder(other)
raise arg_err_build 2, other.size if other.size != 2
x, y = convert_to_num other
(x.abs % y.abs) * (x / x.abs)
end
|
#sub1(other) ⇒ Object
150
151
152
|
# File 'lib/lisp/interpreter/core/numbers.rb', line 150
def sub1(other)
(get_one_arg_function other) - 1
end
|