Sha256: 1d4c99f8f60c28f6e0c6b6b5d8a26edc49dd61524fdde3f1589eae1416cd8f88

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

require 'numerals/conversions'
require 'singleton'

class Numerals::RationalConversion

  include Singleton

  def type
    Rational
  end

  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

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

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

  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

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

  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

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

end

def Rational.numerals_conversion(options = {})
  Numerals::RationalConversion.instance
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
numerals-0.3.1 lib/numerals/conversions/rational.rb
numerals-0.3.0 lib/numerals/conversions/rational.rb
numerals-0.2.1 lib/numerals/conversions/rational.rb
numerals-0.2.0 lib/numerals/conversions/rational.rb
numerals-0.1.0 lib/numerals/conversions/rational.rb