Class: Numerals::IntegerConversion

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

Defined Under Namespace

Classes: InvalidConversion

Instance Method Summary collapse

Instance Method Details

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

Returns:

  • (Boolean)


29
30
31
# File 'lib/numerals/conversions/integer.rb', line 29

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

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



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

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

#number_to_numeral(number, mode, rounding) ⇒ Object



33
34
35
36
37
38
# File 'lib/numerals/conversions/integer.rb', line 33

def number_to_numeral(number, mode, rounding)
  # Rational.numerals_conversion Rational(number), mode, rounding
  numeral = Numerals::Numeral.from_quotient(number, 1)
  numeral = rounding.round(numeral) # unless rounding.free?
  numeral
end

#numeral_to_number(numeral, mode) ⇒ Object



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

def numeral_to_number(numeral, mode)
  rational = Rational.numerals_conversion.numeral_to_number numeral, mode
  if rational.denominator != 1
    raise InvalidConversion, "Invalid numeral to rational conversion"
  end
  rational.numerator
end

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



15
16
17
18
19
20
21
22
# File 'lib/numerals/conversions/integer.rb', line 15

def order_of_magnitude(value, options={})
  base = options[:base] || 10
  if base == 2 && value.respond_to?(:bit_length)
    value.bit_length
  else
    value.to_s(base).size
  end
end

#read(numeral, exact_input, approximate_simplified) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/numerals/conversions/integer.rb', line 55

def read(numeral, exact_input, approximate_simplified)
  rational = Rational.numerals_conversion.read numeral, exact_input, approximate_simplified
  if rational.denominator != 1
    raise InvalidConversion, "Invalid numeral to rational conversion"
  end
  rational.numerator
end

#typeObject



11
12
13
# File 'lib/numerals/conversions/integer.rb', line 11

def type
  Integer
end

#write(number, exact_input, output_rounding) ⇒ Object



48
49
50
51
52
53
# File 'lib/numerals/conversions/integer.rb', line 48

def write(number, exact_input, output_rounding)
  output_base = output_rounding.base
  numeral = Numerals::Numeral.from_quotient(number, 1, base: output_base)
  numeral = output_rounding.round(numeral) # unless output_rounding.free?
  numeral
end