Module: SchemeNumbers

Includes:
SchemeNumbersHelper
Included in:
Tokenizer
Defined in:
lib/lisp/interpreter/core/numbers.rb

Overview

Scheme numbers module

Instance Method Summary collapse

Methods included from SchemeNumbersHelper

#compare_value_arithmetic, #convert_to_num, #divide_number, #divide_special_convert, #find_idx_numerators, #get_num_denom, #get_one_arg_function, #num_denom_helper

Instance Method Details

#*(other) ⇒ Object



92
93
94
95
# File 'lib/lisp/interpreter/core/numbers.rb', line 92

def *(other)
  other = convert_to_num other
  other.reduce(1, :*)
end

#+(other) ⇒ Object



80
81
82
83
# File 'lib/lisp/interpreter/core/numbers.rb', line 80

def +(other)
  other = convert_to_num other
  other.reduce(0, :+)
end

#-(other) ⇒ Object



85
86
87
88
89
90
# File 'lib/lisp/interpreter/core/numbers.rb', line 85

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) ⇒ Object



97
98
99
100
101
102
# File 'lib/lisp/interpreter/core/numbers.rb', line 97

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) ⇒ Object



64
65
66
# File 'lib/lisp/interpreter/core/numbers.rb', line 64

def <(other)
  compare_value_arithmetic other, '<'
end

#<=(other) ⇒ Object



72
73
74
# File 'lib/lisp/interpreter/core/numbers.rb', line 72

def <=(other)
  compare_value_arithmetic other, '<='
end

#>(other) ⇒ Object



68
69
70
# File 'lib/lisp/interpreter/core/numbers.rb', line 68

def >(other)
  compare_value_arithmetic other, '>'
end

#>=(other) ⇒ Object



76
77
78
# File 'lib/lisp/interpreter/core/numbers.rb', line 76

def >=(other)
  compare_value_arithmetic other, '>='
end

#abs(other) ⇒ Object



139
140
141
# File 'lib/lisp/interpreter/core/numbers.rb', line 139

def abs(other)
  (get_one_arg_function other).abs
end

#add1(other) ⇒ Object



143
144
145
# File 'lib/lisp/interpreter/core/numbers.rb', line 143

def add1(other)
  (get_one_arg_function other) + 1
end

#denominator(other) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/lisp/interpreter/core/numbers.rb', line 131

def denominator(other)
  raise arg_err_build 1, 0 if other.empty?
  other = num_denom_helper other
  result = (get_num_denom other)[1]
  raise data_type_err '<number>', result.type unless check_for_num result
  result.to_num
end

#max(other) ⇒ Object



157
158
159
160
161
# File 'lib/lisp/interpreter/core/numbers.rb', line 157

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



151
152
153
154
155
# File 'lib/lisp/interpreter/core/numbers.rb', line 151

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



117
118
119
120
121
# File 'lib/lisp/interpreter/core/numbers.rb', line 117

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



123
124
125
126
127
128
129
# File 'lib/lisp/interpreter/core/numbers.rb', line 123

def numerator(other)
  raise arg_err_build 1, 0 if other.empty?
  other = num_denom_helper other
  result = (get_num_denom other)[0]
  raise data_type_err '<number>', result.type unless check_for_num result
  result.to_num
end

#quotient(other) ⇒ Object



104
105
106
107
108
109
# File 'lib/lisp/interpreter/core/numbers.rb', line 104

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



111
112
113
114
115
# File 'lib/lisp/interpreter/core/numbers.rb', line 111

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



147
148
149
# File 'lib/lisp/interpreter/core/numbers.rb', line 147

def sub1(other)
  (get_one_arg_function other) - 1
end