Class: Plurimath::Formatter::Numbers::FormattedNotation

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/formatter/numbers/formatted_notation.rb

Overview

Structured result of notation formatting (e, scientific, engineering). Carries the coefficient as a FormattedNumber, the exponent as an integer, and notation style metadata so output renderers (MathML, LaTeX, etc.) can produce structured representations instead of flat strings.

Constant Summary collapse

STYLES =
%i[e scientific engineering].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coefficient:, notation_style:, exponent:, times_symbol: "×", exponent_separator: "e", exponent_sign: nil) ⇒ FormattedNotation

Returns a new instance of FormattedNotation.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 17

def initialize(
  coefficient:,
  notation_style:,
  exponent:,
  times_symbol: "×",
  exponent_separator: "e",
  exponent_sign: nil
)
  @coefficient = coefficient
  @notation_style = notation_style
  @exponent = exponent
  @times_symbol = times_symbol
  @exponent_separator = exponent_separator
  @exponent_sign = exponent_sign
end

Instance Attribute Details

#coefficientObject (readonly)

Returns the value of attribute coefficient.



14
15
16
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 14

def coefficient
  @coefficient
end

#exponentObject (readonly)

Returns the value of attribute exponent.



14
15
16
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 14

def exponent
  @exponent
end

#exponent_separatorObject (readonly)

Returns the value of attribute exponent_separator.



14
15
16
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 14

def exponent_separator
  @exponent_separator
end

#exponent_signObject (readonly)

Returns the value of attribute exponent_sign.



14
15
16
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 14

def exponent_sign
  @exponent_sign
end

#notation_styleObject (readonly)

Returns the value of attribute notation_style.



14
15
16
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 14

def notation_style
  @notation_style
end

#times_symbolObject (readonly)

Returns the value of attribute times_symbol.



14
15
16
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 14

def times_symbol
  @times_symbol
end

Instance Method Details

#base_notation?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 46

def base_notation?
  coefficient.base_notation?
end

#formatted_exponentObject

The exponent rendered as a string, applying sign conventions. Public so that output renderers can access it directly.



52
53
54
55
56
57
58
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 52

def formatted_exponent
  return "0" if exponent.zero?

  sign_prefix = exponent_sign == :plus ? "+" : nil
  abs_exp = exponent.abs.to_s
  exponent.negative? ? "-#{abs_exp}" : "#{sign_prefix}#{abs_exp}"
end

#to_sObject



33
34
35
36
37
38
39
40
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 33

def to_s
  case notation_style
  when :e
    "#{coefficient}#{exponent_separator}#{formatted_exponent}"
  when :scientific, :engineering
    "#{coefficient} #{times_symbol} 10^#{formatted_exponent}"
  end
end

#to_strObject



42
43
44
# File 'lib/plurimath/formatter/numbers/formatted_notation.rb', line 42

def to_str
  to_s
end