Class: Plurimath::Formatter::Numbers::Source
- Inherits:
-
Object
- Object
- Plurimath::Formatter::Numbers::Source
- Defined in:
- lib/plurimath/formatter/numbers/source.rb
Overview
Captures raw input, BigDecimal interpretation, and source digit metadata before formatter transforms run.
Constant Summary collapse
- DEFAULT_INTEGER =
"0"- EMPTY_STRING =
""
Instance Attribute Summary collapse
-
#decimal ⇒ Object
readonly
Returns the value of attribute decimal.
-
#exponent ⇒ Object
readonly
Returns the value of attribute exponent.
-
#exponent_text ⇒ Object
readonly
Returns the value of attribute exponent_text.
-
#fraction_digits ⇒ Object
readonly
Returns the value of attribute fraction_digits.
-
#integer_digits ⇒ Object
readonly
Returns the value of attribute integer_digits.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#sign ⇒ Object
readonly
Returns the value of attribute sign.
Instance Method Summary collapse
- #decimal_precision ⇒ Object
- #fractional? ⇒ Boolean
-
#initialize(value) ⇒ Source
constructor
A new instance of Source.
- #notation_precision ⇒ Object
- #significant_digit_count ⇒ Object
- #target_base_integer_length(base) ⇒ Object
- #to_parts(base: nil, precision: nil) ⇒ Object
- #trailing_fraction_zero_count ⇒ Object
Constructor Details
#initialize(value) ⇒ Source
Returns a new instance of Source.
17 18 19 20 21 22 23 24 25 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 17 def initialize(value) @raw = value.to_s @decimal = BigDecimal(raw) @sign = raw.start_with?("-") ? -1 : 1 mantissa, @exponent_text = unsigned_value.split("e", 2) @exponent = exponent_text.to_i @integer_digits, @fraction_digits = split_mantissa(mantissa) end |
Instance Attribute Details
#decimal ⇒ Object (readonly)
Returns the value of attribute decimal.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 11 def decimal @decimal end |
#exponent ⇒ Object (readonly)
Returns the value of attribute exponent.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 11 def exponent @exponent end |
#exponent_text ⇒ Object (readonly)
Returns the value of attribute exponent_text.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 11 def exponent_text @exponent_text end |
#fraction_digits ⇒ Object (readonly)
Returns the value of attribute fraction_digits.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 11 def fraction_digits @fraction_digits end |
#integer_digits ⇒ Object (readonly)
Returns the value of attribute integer_digits.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 11 def integer_digits @integer_digits end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 11 def raw @raw end |
#sign ⇒ Object (readonly)
Returns the value of attribute sign.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 11 def sign @sign end |
Instance Method Details
#decimal_precision ⇒ Object
31 32 33 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 31 def decimal_precision decimal_digits.last.length end |
#fractional? ⇒ Boolean
27 28 29 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 27 def fractional? fraction_digits.length > exponent end |
#notation_precision ⇒ Object
35 36 37 38 39 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 35 def notation_precision precision = integer_digits.length + fraction_digits.length - 1 precision += 1 if sign.negative? [precision, 0].max end |
#significant_digit_count ⇒ Object
41 42 43 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 41 def significant_digit_count significant_digits.length end |
#target_base_integer_length(base) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 45 def target_base_integer_length(base) return decimal_parts_integer_length if base == Base::DEFAULT_BASE integer = decimal.abs.to_i return 0 if integer.zero? integer.to_s(base).length end |
#to_parts(base: nil, precision: nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 54 def to_parts(base: nil, precision: nil) integer, fraction = decimal_digits fraction = apply_precision(fraction, precision) Parts.new( sign: sign, base: base || Base::DEFAULT_BASE, integer_digits: integer, fraction_digits: fraction, ) end |
#trailing_fraction_zero_count ⇒ Object
66 67 68 69 70 |
# File 'lib/plurimath/formatter/numbers/source.rb', line 66 def trailing_fraction_zero_count return 0 if fraction_digits.empty? fraction_digits.length - fraction_digits.sub(/0+\z/, "").length end |