Class: Numerals::Format::BaseScaler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ModalSupport::BracketConstructor
Defined in:
lib/numerals/format/base_scaler.rb

Overview

This converts the number representation contained in an ExpSetter scaling the base of the significand.

This is typically used when the ExpSetter base is 2 to render the number in C99 ‘%A’ format, i.e., in hexadecimal base. Only the significand is shown in base 16; the exponent is still a power of two, and represented in base 10.

This is a generalization of the %A format where any base which is a power of the original base can be used for the significand.

The number exponent is previously adjusted in the ExpSetter and that doesn’t change, only the significand parts are converted from the original base ‘base` to the new base `base**base_scale`.

This will require adjusting the repeating digits position and length, and adding leading 0s (in the original base) to the signficant and/or trailing digits may be required.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp_setter, base_scale) ⇒ BaseScaler

Returns a new instance of BaseScaler.



26
27
28
29
30
31
32
# File 'lib/numerals/format/base_scaler.rb', line 26

def initialize(exp_setter, base_scale)
  @setter = exp_setter
  @numeral = @setter.numeral
  @base_scale = base_scale
  @scaled_base = @setter.base**@base_scale
  adjust
end

Instance Attribute Details

#base_scaleObject (readonly)

Returns the value of attribute base_scale.



36
37
38
# File 'lib/numerals/format/base_scaler.rb', line 36

def base_scale
  @base_scale
end

#numeralObject (readonly)

Returns the value of attribute numeral.



36
37
38
# File 'lib/numerals/format/base_scaler.rb', line 36

def numeral
  @numeral
end

#scaled_baseObject (readonly)

Returns the value of attribute scaled_base.



36
37
38
# File 'lib/numerals/format/base_scaler.rb', line 36

def scaled_base
  @scaled_base
end

Instance Method Details

#baseObject



43
44
45
# File 'lib/numerals/format/base_scaler.rb', line 43

def base
  scaled_base
end

#fractional_insignificant_sizeObject



56
57
58
59
60
61
62
# File 'lib/numerals/format/base_scaler.rb', line 56

def fractional_insignificant_size
  if @setter.numeral.approximate?
    (@setter.fractional_insignificant_size + @scaling_trailing_size)/@base_scale
  else
    0
  end
end

#fractional_partObject



47
48
49
50
# File 'lib/numerals/format/base_scaler.rb', line 47

def fractional_part
  ungrouped = @setter.fractional_part + (0...@scaling_trailing_size).map{|i| repeat_digit(i)}
  grouped_digits ungrouped
end

#fractional_part_sizeObject



52
53
54
# File 'lib/numerals/format/base_scaler.rb', line 52

def fractional_part_size
  (@setter.fractional_part_size + @scaling_trailing_size)/@base_scale
end

#integer_insignificant_sizeObject



73
74
75
76
77
78
79
# File 'lib/numerals/format/base_scaler.rb', line 73

def integer_insignificant_size
  if @setter.numeral.approximate?
    (@setter.integer_insignificant_size + @scaling_leading_size)/@base_scale
  else
    0
  end
end

#integer_partObject



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

def integer_part
  ungrouped = [0]*@scaling_leading_size + @setter.integer_part
  grouped_digits ungrouped
end

#integer_part_sizeObject



69
70
71
# File 'lib/numerals/format/base_scaler.rb', line 69

def integer_part_size
  (@setter.integer_part_size + @scaling_leading_size)/@base_scale
end

#repeat_insignificant_sizeObject



85
86
87
# File 'lib/numerals/format/base_scaler.rb', line 85

def repeat_insignificant_size
  0
end

#repeat_partObject



89
90
91
92
# File 'lib/numerals/format/base_scaler.rb', line 89

def repeat_part
  ungrouped = (@scaling_trailing_size...@scaling_trailing_size+@repeat_length).map{|i| repeat_digit(i)}
  grouped_digits ungrouped
end

#repeat_size_sizeObject



81
82
83
# File 'lib/numerals/format/base_scaler.rb', line 81

def repeat_size_size
  @repeat_length/@base_scale
end