Class: Plurimath::Formatter::Numbers::DigitSequence
- Inherits:
-
Object
- Object
- Plurimath::Formatter::Numbers::DigitSequence
- Defined in:
- lib/plurimath/formatter/numbers/digit_sequence.rb
Overview
Shared base-digit helper for counting, rounding thresholds, next-digit lookup, and carry propagation.
Constant Summary collapse
- ZERO =
"0"
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
- #digit?(char) ⇒ Boolean
- #digit_count(chars, stop_at: nil) ⇒ Object
- #increment_reversed(digits, carry: 1, skip: [], overflow: ZERO) ⇒ Object
-
#initialize(base:) ⇒ DigitSequence
constructor
A new instance of DigitSequence.
- #max_digit?(char) ⇒ Boolean
- #next_digit(char) ⇒ Object
- #round_up?(char) ⇒ Boolean
- #significant?(char) ⇒ Boolean
- #significant_digit_count(chars) ⇒ Object
- #threshold ⇒ Object
Constructor Details
#initialize(base:) ⇒ DigitSequence
Returns a new instance of DigitSequence.
13 14 15 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 13 def initialize(base:) @base = base end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
9 10 11 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 9 def base @base end |
Instance Method Details
#digit?(char) ⇒ Boolean
17 18 19 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 17 def digit?(char) Base::DIGIT_VALUE.key?(char) end |
#digit_count(chars, stop_at: nil) ⇒ Object
25 26 27 28 29 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 25 def digit_count(chars, stop_at: nil) count = 0 each_countable_digit(chars, stop_at: stop_at) { count += 1 } count end |
#increment_reversed(digits, carry: 1, skip: [], overflow: ZERO) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 63 def increment_reversed(digits, carry: 1, skip: [], overflow: ZERO) digits = digits.dup return [digits, carry] unless carry.positive? digits.each_with_index do |digit, index| next if skip.include?(digit) next unless digit?(digit) if max_digit?(digit) digits[index] = overflow else digits[index] = next_digit(digit) carry = 0 break end end [digits, carry] end |
#max_digit?(char) ⇒ Boolean
43 44 45 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 43 def max_digit?(char) Base::DIGIT_VALUE[char] == base.pred end |
#next_digit(char) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 47 def next_digit(char) current_index = Base::DIGIT_VALUE[char] return unless current_index Base::HEX_ALPHANUMERIC[current_index + 1] end |
#round_up?(char) ⇒ Boolean
54 55 56 57 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 54 def round_up?(char) value = Base::DIGIT_VALUE[char] !!(value && value >= threshold) end |
#significant?(char) ⇒ Boolean
21 22 23 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 21 def significant?(char) Base::DIGIT_VALUE[char]&.positive? end |
#significant_digit_count(chars) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 31 def significant_digit_count(chars) start_counting = false count = 0 each_countable_digit(chars) do |char| start_counting = true if significant?(char) count += 1 if start_counting end count end |
#threshold ⇒ Object
59 60 61 |
# File 'lib/plurimath/formatter/numbers/digit_sequence.rb', line 59 def threshold @threshold ||= base.div(2) end |