module SchemeNumbers
Scheme numbers module
Public Instance Methods
*(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 99 def *(other) other = convert_to_num other other.reduce(1, :*) end
+(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 87 def +(other) other = convert_to_num other other.reduce(0, :+) end
-(other)
click to toggle source
# 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
/(other)
click to toggle source
# 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
<(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 71 def <(other) compare_value_arithmetic other, '<' end
<=(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 79 def <=(other) compare_value_arithmetic other, '<=' end
>(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 75 def >(other) compare_value_arithmetic other, '>' end
>=(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 83 def >=(other) compare_value_arithmetic other, '>=' end
abs(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 142 def abs(other) (get_one_arg_function other).abs end
add1(other)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 146 def add1(other) (get_one_arg_function other) + 1 end
denominator(other)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 150 def sub1(other) (get_one_arg_function other) - 1 end