Module: Flt::Support

Included in:
Num
Defined in:
lib/flt/support.rb,
lib/flt/support/reader.rb,
lib/flt/support/formatter.rb,
lib/flt/support/flag_values.rb,
lib/flt/support/rationalizer.rb,
lib/flt/support/rationalizer_extra.rb

Defined Under Namespace

Modules: AuxiliarFunctions Classes: FlagValues, Flags, Formatter, InfiniteLoopError, Rationalizer, Reader

Class Method Summary collapse

Class Method Details

.adjust_digits(dec_pos, digits, options = {}) ⇒ Object

Adjust truncated digits based on the rounding mode (:round_mode option) and on the information about the following digits contained in the :round_up parameter (nil for only zeros, :lo for nonzero values below tie, :tie for a :tie and :hi for nonzero digits over the tie). Other parameters: :negative to consider the number negative, :base the base of the number.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/flt/support.rb', line 30

def adjust_digits(dec_pos, digits, options={})
  round_mode = options[:round_mode]
  negative   = options[:negative]
  round_up   = options[:round_up]
  base       = options[:base]
  round_mode = simplified_round_mode(round_mode, negative)

  increment = (round_up && (round_mode != :down)) &&
                ((round_mode == :up) ||
                 (round_up == :hi) ||
                 ((round_up == :tie) &&
                  ((round_mode==:half_up) ||
                   ((round_mode==:half_even) && ((digits.last % 2)==1)))))

  if increment
    digits = digits.dup    # carry = increment ? 1 : 0
    # digits = digits.reverse.map{|d| d += carry; d>=base ? 0 : (carry=0;d)}.reverse
    # if carry != 0
    #   digits.unshift carry
    #   dec_pos += 1
    # end

    i = digits.size - 1
    while i>=0
      digits[i] += 1
      if digits[i] == base
        digits[i] = 0
      else
        break
      end
      i -= 1
    end
    if i<0
      dec_pos += 1
      digits.unshift 1
    end
  end
  [dec_pos, digits]
end

.Flags(*params) ⇒ Object

Constructor for Flags


331
332
333
334
335
336
337
# File 'lib/flt/support/flag_values.rb', line 331

def Flags(*params)
  if params.size==1 && params.first.kind_of?(Flags)
    params.first
  else
    Flags.new(*params)
  end
end

.FlagValues(*params) ⇒ Object

Constructor for FlagValues


322
323
324
325
326
327
328
# File 'lib/flt/support/flag_values.rb', line 322

def FlagValues(*params)
  if params.size==1 && params.first.kind_of?(FlagValues)
    params.first
  else
    FlagValues.new(*params)
  end
end

.simplified_round_mode(round_mode, negative) ⇒ Object

replace :ceiling and :floor rounding modes by :up/:down (depending on sign of the number to be rounded)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flt/support.rb', line 9

def simplified_round_mode(round_mode, negative)
  if negative
    if round_mode == :ceiling
      round_mode = :floor
    elsif round_mode == :floor
      round_mode = :ceiling
    end
  end
  if round_mode == :ceiling
    round_mode = :up
  elsif round_mode == :floor
    round_mode = :down
  end
  round_mode
end