Method: Numerals::Rounding#initialize

Defined in:
lib/numerals/rounding.rb

#initialize(*args) ⇒ Rounding

Rounding defines a rounding mode and a precision, and is used to establish the desired accuracy of a Numeral result.

Rounding also defines the base of the numerals to be rounded, which is 10 by default.

The rounding mode is the rule used to limit the precision of a numeral; the rounding modes available are those of Flt::Num, namely:

  • :half_even

  • :half_up

  • :half_down

  • :ceiling

  • :floor

  • :up

  • :down

  • :up05

Regarding the rounding precision there are two types of Roundings:

  • Fixed (limited) precision: the precision of the rounded result is either defined as relative (number of significant digits defined by the precision property) or absolute (number of fractional places –decimals for base 10– defined by the places property)

  • Free (unlimited) precision, which preserves the value of the input numeral. As much precision as needed is used to keep unambiguously the original value. When applied to exact input, this kind of rounding doesn’t perform any rounding. For approximate input there are two variants:

    • Preserving the original value precision, which produces and approximate output. (All original digits are preserved; full precision mode). This is the default free precision mode, established by using the :free symbol for the precision (or its synonym :preserve).

    • Simplifiying or reducing the result to produce an exact output without unneeded digits to restore the original value within its original precision (e.g. traling zeros are not keep). This case can be defined with the :short symbol for the precision (or its synonum :simplify).



47
48
49
50
51
52
# File 'lib/numerals/rounding.rb', line 47

def initialize(*args)
  DEFAULTS.each do |param, value|
    instance_variable_set "@#{param}", value
  end
  set! *args
end