Method: Numerals::FltConversion#write

Defined in:
lib/numerals/conversions/flt.rb

#write(number, exact_input, output_rounding) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/numerals/conversions/flt.rb', line 65

def write(number, exact_input, output_rounding)
  output_base = output_rounding.base
  input_base = @context.radix

  if number.special? # @context.special?(number)
    special_num_to_numeral number
  elsif exact_input
    if output_base == input_base && output_rounding.free?
      # akin to number.format(base: output_base, simplified: true)
      general_num_to_numeral number, output_rounding, false
    else
      # akin to number.format(base: output_base, exact: true)
      exact_num_to_numeral number, output_rounding
    end
  else
    if output_base == input_base && output_rounding.preserving?
      # akin to number.format(base: output_base)
      Numeral.from_coefficient_scale(
        number.sign*number.coefficient, number.integral_exponent,
        approximate: true, base: output_base
      )
    elsif output_rounding.simplifying?
      # akin to number.forma(base: output_base, simplify: true)
      general_num_to_numeral number, output_rounding, false
    else
      # akin to number.forma(base: output_base, all_digits: true)
      general_num_to_numeral number, output_rounding, true
    end
  end
end