Class: Numerals::Format::ExpSetter
- Inherits:
-
Object
- Object
- Numerals::Format::ExpSetter
- Includes:
- ModalSupport::BracketConstructor
- Defined in:
- lib/numerals/format/exp_setter.rb
Overview
Adjust exponent to be used in a Numeral expression; break up the numeral into integer, fractional and repeating parts.
setter = ExpSetter[numeral]
# To use 'fixed' format:
setter.exponent = 0
# To use scientific notation:
setter.integer_part_size = 1
# To adjust scientific notation to engineering mode:
setter.integer_part_size += 1 (while setter.exponent % 3) != 0
# To automatically choose between fixed/scientific format:
setter.exponent = 0 # fixed
if setter.leading_size > 6 || setter.trailing_size > 0
setter.integer_part_size = 1 # scientific
end
# To access the numeric parts for formatting:
setter.sign
setter.integer_part # digits before radix point
setter.fractional_part # digits after radix point, before repetition
setter.repeating_part # repeated digits
setter.base # base for exponent
setter.exponent # exponent
Instance Attribute Summary collapse
-
#exponent ⇒ Object
Returns the value of attribute exponent.
-
#fractional_insignificant_size ⇒ Object
readonly
Returns the value of attribute fractional_insignificant_size.
-
#integer_insignificant_size ⇒ Object
readonly
Returns the value of attribute integer_insignificant_size.
-
#integer_part_size ⇒ Object
Returns the value of attribute integer_part_size.
-
#leading_size ⇒ Object
readonly
Returns the value of attribute leading_size.
-
#numeral ⇒ Object
readonly
Returns the value of attribute numeral.
-
#trailing_size ⇒ Object
readonly
Returns the value of attribute trailing_size.
Instance Method Summary collapse
- #base ⇒ Object
- #exponent_base ⇒ Object
- #fractional_part ⇒ Object
- #fractional_part_size ⇒ Object
-
#initialize(numeral, options = {}) ⇒ ExpSetter
constructor
A new instance of ExpSetter.
- #integer_part ⇒ Object
- #repeat_insignificant_size ⇒ Object
- #repeat_part ⇒ Object
- #repeat_part_size ⇒ Object
- #repeating? ⇒ Boolean
- #sign ⇒ Object
- #special ⇒ Object
- #special? ⇒ Boolean
Constructor Details
#initialize(numeral, options = {}) ⇒ ExpSetter
Returns a new instance of ExpSetter.
29 30 31 32 33 34 35 36 37 |
# File 'lib/numerals/format/exp_setter.rb', line 29 def initialize(numeral, ={}) @insignificant_digits = [:insignificant_digits] || 0 @numeral = numeral @integer_part_size = @numeral.point @digits = @numeral.digits @exponent = 0 @repeat_size = @numeral.repeating? ? @digits.size - @numeral.repeat : 0 adjust end |
Instance Attribute Details
#exponent ⇒ Object
Returns the value of attribute exponent.
42 43 44 |
# File 'lib/numerals/format/exp_setter.rb', line 42 def exponent @exponent end |
#fractional_insignificant_size ⇒ Object (readonly)
Returns the value of attribute fractional_insignificant_size.
118 119 120 |
# File 'lib/numerals/format/exp_setter.rb', line 118 def fractional_insignificant_size @fractional_insignificant_size end |
#integer_insignificant_size ⇒ Object (readonly)
Returns the value of attribute integer_insignificant_size.
118 119 120 |
# File 'lib/numerals/format/exp_setter.rb', line 118 def integer_insignificant_size @integer_insignificant_size end |
#integer_part_size ⇒ Object
Returns the value of attribute integer_part_size.
42 43 44 |
# File 'lib/numerals/format/exp_setter.rb', line 42 def integer_part_size @integer_part_size end |
#leading_size ⇒ Object (readonly)
Returns the value of attribute leading_size.
43 44 45 |
# File 'lib/numerals/format/exp_setter.rb', line 43 def leading_size @leading_size end |
#numeral ⇒ Object (readonly)
Returns the value of attribute numeral.
39 40 41 |
# File 'lib/numerals/format/exp_setter.rb', line 39 def numeral @numeral end |
#trailing_size ⇒ Object (readonly)
Returns the value of attribute trailing_size.
43 44 45 |
# File 'lib/numerals/format/exp_setter.rb', line 43 def trailing_size @trailing_size end |
Instance Method Details
#base ⇒ Object
45 46 47 |
# File 'lib/numerals/format/exp_setter.rb', line 45 def base @numeral.base end |
#exponent_base ⇒ Object
49 50 51 |
# File 'lib/numerals/format/exp_setter.rb', line 49 def exponent_base base end |
#fractional_part ⇒ Object
106 107 108 |
# File 'lib/numerals/format/exp_setter.rb', line 106 def fractional_part leading + @digits[@fractional_start...@fractional_end] end |
#fractional_part_size ⇒ Object
114 115 116 |
# File 'lib/numerals/format/exp_setter.rb', line 114 def fractional_part_size @fractional_end - @fractional_start + @leading_size end |
#integer_part ⇒ Object
110 111 112 |
# File 'lib/numerals/format/exp_setter.rb', line 110 def integer_part @digits[@integer_start...@integer_end] + trailing end |
#repeat_insignificant_size ⇒ Object
120 121 122 |
# File 'lib/numerals/format/exp_setter.rb', line 120 def repeat_insignificant_size 0 end |
#repeat_part ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/numerals/format/exp_setter.rb', line 85 def repeat_part if @numeral.repeating? if @repeat_phase != 0 || @numeral.repeat < 0 start = @numeral.repeat + @repeat_phase (start...start+repeat_part_size).map{|i| @numeral.digit_value_at(i)} else @digits[@numeral.repeat..-1] end else [] end end |
#repeat_part_size ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/numerals/format/exp_setter.rb', line 98 def repeat_part_size if @numeral.repeating? @digits.size - @numeral.repeat else 0 end end |
#repeating? ⇒ Boolean
61 62 63 |
# File 'lib/numerals/format/exp_setter.rb', line 61 def repeating? @numeral.repeating? end |
#sign ⇒ Object
65 66 67 |
# File 'lib/numerals/format/exp_setter.rb', line 65 def sign @numeral.sign end |
#special ⇒ Object
57 58 59 |
# File 'lib/numerals/format/exp_setter.rb', line 57 def special @numeral.special end |
#special? ⇒ Boolean
53 54 55 |
# File 'lib/numerals/format/exp_setter.rb', line 53 def special? @numeral.special? end |