Class: Numerals::FltConversion
- Inherits:
-
ContextConversion
- Object
- ContextConversion
- Numerals::FltConversion
- Defined in:
- lib/numerals/conversions/flt.rb
Instance Attribute Summary
Attributes inherited from ContextConversion
#context, #input_rounding, #type
Instance Method Summary collapse
- #exact?(value, options = {}) ⇒ Boolean
-
#initialize(context_or_type, options = {}) ⇒ FltConversion
constructor
Options:.
- #number_of_digits(value, options = {}) ⇒ Object
-
#number_to_numeral(number, mode, rounding) ⇒ Object
mode is either :exact or :approximate.
- #numeral_to_number(numeral, mode) ⇒ Object
- #order_of_magnitude(value, options = {}) ⇒ Object
- #read(numeral, exact_input, approximate_simplified) ⇒ Object
- #write(number, exact_input, output_rounding) ⇒ Object
Constructor Details
#initialize(context_or_type, options = {}) ⇒ FltConversion
Options:
-
:input_rounding (optional, a non-exact Rounding or rounding mode) which is used when input is approximate as the assumed rounding mode which would be used so that the result numeral rounds back to the input number. :context can be used to use the numeric context as input rounding. input_rounding is also used to round input …
14 15 16 |
# File 'lib/numerals/conversions/flt.rb', line 14 def initialize(context_or_type, ={}) super end |
Instance Method Details
#exact?(value, options = {}) ⇒ Boolean
36 37 38 |
# File 'lib/numerals/conversions/flt.rb', line 36 def exact?(value, ={}) [:exact] end |
#number_of_digits(value, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/numerals/conversions/flt.rb', line 27 def number_of_digits(value, ={}) base = [:base] || 10 if base == @context.radix value.number_of_digits else x.class.context[precision: value.number_of_digits].necessary_digits(base) end end |
#number_to_numeral(number, mode, rounding) ⇒ Object
mode is either :exact or :approximate
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/numerals/conversions/flt.rb', line 41 def number_to_numeral(number, mode, rounding) if number.special? # @context.special?(number) special_num_to_numeral(number) else if mode == :exact exact_num_to_numeral number, rounding else # mode == :approximate approximate_num_to_numeral(number, rounding) end end end |
#numeral_to_number(numeral, mode) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/numerals/conversions/flt.rb', line 53 def numeral_to_number(numeral, mode) if numeral.special? special_numeral_to_num numeral elsif mode == :fixed fixed_numeral_to_num numeral else # mode == :free free_numeral_to_num numeral end end |
#order_of_magnitude(value, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/numerals/conversions/flt.rb', line 18 def order_of_magnitude(value, ={}) base = [:base] || 10 # value.num_class.radix if value.class.radix == base value.adjusted_exponent + 1 else value.abs.log(base).floor + 1 end end |
#read(numeral, exact_input, approximate_simplified) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/numerals/conversions/flt.rb', line 94 def read(numeral, exact_input, approximate_simplified) if numeral.special? special_numeral_to_num numeral elsif numeral.approximate? && !exact_input if approximate_simplified # akin to @context.Num(numeral_text, :short) short_numeral_to_num numeral else # akin to @context.Num(numeral_text, :free) free_numeral_to_num numeral end else # akin to @context.Num(numeral_text, :fixed) fixed_numeral_to_num numeral end end |
#write(number, exact_input, output_rounding) ⇒ Object
63 64 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 |
# File 'lib/numerals/conversions/flt.rb', line 63 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. # 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 |