Class: Numerals::RationalConversion

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/numerals/conversions/rational.rb

Instance Method Summary collapse

Instance Method Details

#exact?(value, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/numerals/conversions/rational.rb', line 25

def exact?(value, options={})
  true
end

#number_of_digits(value, options = {}) ⇒ Object



21
22
23
# File 'lib/numerals/conversions/rational.rb', line 21

def number_of_digits(value, options={})
  return 0 # this is needed only for non-exact values
end

#number_to_numeral(number, mode, rounding) ⇒ Object



29
30
31
32
33
34
# File 'lib/numerals/conversions/rational.rb', line 29

def number_to_numeral(number, mode, rounding)
  q = [number.numerator, number.denominator]
  numeral = Numerals::Numeral.from_quotient(q)
  numeral = rounding.round(numeral) # unless rounding.free?
  numeral
end

#numeral_to_number(numeral, mode) ⇒ Object



36
37
38
# File 'lib/numerals/conversions/rational.rb', line 36

def numeral_to_number(numeral, mode)
  Rational(*numeral.to_quotient)
end

#order_of_magnitude(value, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/numerals/conversions/rational.rb', line 12

def order_of_magnitude(value, options={})
  base = options[:base] || 10
  if base == 10
    Math.log10(value.abs).floor + 1
  else
    (Math.log(value.abs)/Math.log(base)).floor + 1
  end
end

#read(numeral, exact_input, approximate_simplified) ⇒ Object



48
49
50
# File 'lib/numerals/conversions/rational.rb', line 48

def read(numeral, exact_input, approximate_simplified)
  Rational(*numeral.to_quotient)
end

#typeObject



8
9
10
# File 'lib/numerals/conversions/rational.rb', line 8

def type
  Rational
end

#write(number, exact_input, output_rounding) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/numerals/conversions/rational.rb', line 40

def write(number, exact_input, output_rounding)
  output_base = output_rounding.base
  q = [number.numerator, number.denominator]
  numeral = Numerals::Numeral.from_quotient(q, base: output_base)
  numeral = output_rounding.round(numeral) # unless output_rounding.free?
  numeral
end