module Rubic::Util

Public Instance Methods

exact_to_inexact(num) click to toggle source
# File lib/rubic/util.rb, line 18
def exact_to_inexact(num)
  case num
  when Complex
    r = exact_to_inexact(num.real)
    i = exact_to_inexact(num.imag)
    normalize_number Complex(r, i)
  when Float, Rational, Integer
    num.to_f
  else
    raise TypeError, "unexpected type of number: #{num.class}"
  end
end
inexact_to_exact(num) click to toggle source
# File lib/rubic/util.rb, line 3
def inexact_to_exact(num)
  case num
  when Complex
    r = inexact_to_exact(num.real)
    i = inexact_to_exact(num.imag)
    normalize_number Complex(r, i)
  when Float
    normalize_number num.rationalize
  when Rational, Integer
    num
  else
    raise TypeError, "unexpected type of number: #{num.class}"
  end
end
normalize_number(num) click to toggle source
# File lib/rubic/util.rb, line 31
def normalize_number(num)
  case num
  when Complex
    num.imag.zero? ? num.real : num
  when Rational
    num.denominator == 1 ? num.numerator : num
  else
    num
  end
end