Class: Numerals::Format::ExpSetter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  @insignificant_digits = options[: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

#exponentObject

Returns the value of attribute exponent



42
43
44
# File 'lib/numerals/format/exp_setter.rb', line 42

def exponent
  @exponent
end

#fractional_insignificant_sizeObject (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_sizeObject (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_sizeObject

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_sizeObject (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

#numeralObject (readonly)

Returns the value of attribute numeral



39
40
41
# File 'lib/numerals/format/exp_setter.rb', line 39

def numeral
  @numeral
end

#trailing_sizeObject (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

#baseObject



45
46
47
# File 'lib/numerals/format/exp_setter.rb', line 45

def base
  @numeral.base
end

#exponent_baseObject



49
50
51
# File 'lib/numerals/format/exp_setter.rb', line 49

def exponent_base
  base
end

#fractional_partObject



106
107
108
# File 'lib/numerals/format/exp_setter.rb', line 106

def fractional_part
  leading + @digits[@fractional_start...@fractional_end]
end

#fractional_part_sizeObject



114
115
116
# File 'lib/numerals/format/exp_setter.rb', line 114

def fractional_part_size
  @fractional_end - @fractional_start + @leading_size
end

#integer_partObject



110
111
112
# File 'lib/numerals/format/exp_setter.rb', line 110

def integer_part
  @digits[@integer_start...@integer_end] + trailing
end

#repeat_insignificant_sizeObject



120
121
122
# File 'lib/numerals/format/exp_setter.rb', line 120

def repeat_insignificant_size
  0
end

#repeat_partObject



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_sizeObject



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

Returns:

  • (Boolean)


61
62
63
# File 'lib/numerals/format/exp_setter.rb', line 61

def repeating?
  @numeral.repeating?
end

#signObject



65
66
67
# File 'lib/numerals/format/exp_setter.rb', line 65

def sign
  @numeral.sign
end

#specialObject



57
58
59
# File 'lib/numerals/format/exp_setter.rb', line 57

def special
  @numeral.special
end

#special?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/numerals/format/exp_setter.rb', line 53

def special?
  @numeral.special?
end