module SchemeNumbersHelper

Helper functions for SchemeNumbers

Public Instance Methods

compare_value_arithmetic(other, oper) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 41
def compare_value_arithmetic(other, oper)
  raise arg_err_build 'at least 2', other.size if other.size < 2
  other = convert_to_num other
  result = other.each_cons(2).all? { |x, y| x.public_send oper, y }
  result ? TRUE : FALSE
end
convert_to_num(other) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 48
def convert_to_num(other)
  other.each do |t|
    raise type_err '<number>', t.type unless check_for_num t
  end
  other.map(&:to_num)
end
divide_number(a, b) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 61
def divide_number(a, b)
  return a / b if (a / b).to_i.to_f == a / b.to_f
  a / b.to_f
end
divide_special_convert(other) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 55
def divide_special_convert(other)
  other = convert_to_num other
  return [0] if other.size == 1 && other[0] == 0.0
  other
end
find_idx_numerators(other) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 9
def find_idx_numerators(other)
  other[0] == '(' ? (find_bracket_idx other, 0) + 1 : 1
end
get_num_denom(other) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 32
def get_num_denom(other)
  x, temp = find_next_value other
  num_denom_validator temp
  return [x, 1] if temp.empty?
  y, temp = find_next_value temp[1..-1]
  num_denom_validator temp
  [x, y]
end
get_one_arg_function(other) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 3
def get_one_arg_function(other)
  raise arg_err_build 1, other.size if other.size != 1
  raise type_err '<number>', other[0].type unless check_for_num other[0]
  other[0].to_num
end
num_denom_helper(other) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 17
def num_denom_helper(other)
  if other.size == 1
    other = rationalize_num other[0] if check_for_num other[0]
    other[0].split('/')
  else
    other = other.map { |t| t.to_s.split(%r{(\/)}) }.flatten
    other.delete('')
    get_num_denom other
  end
end
num_denom_validator(temp) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 28
def num_denom_validator(temp)
  raise arg_err_build 1, temp.size + 1 if (temp[0] != '/') && !temp.empty?
end
rationalize_num(num) click to toggle source
# File lib/lisp/interpreter/core/numbers.rb, line 13
def rationalize_num(num)
  [num.to_num.to_r.to_s]
end