Class: EncodeM::Numeric
- Inherits:
-
Object
- Object
- EncodeM::Numeric
- Includes:
- Comparable
- Defined in:
- lib/encode_m/numeric.rb
Constant Summary collapse
- MAX_PRECISION =
M language typically uses 18-digit precision
18
Instance Attribute Summary collapse
-
#encoded ⇒ Object
readonly
Returns the value of attribute encoded.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #**(other) ⇒ Object
-
#+(other) ⇒ Object
Arithmetic operations.
- #-(other) ⇒ Object
- #/(other) ⇒ Object
-
#<=>(other) ⇒ Object
M language feature: encoded comparison.
- #==(other) ⇒ Object
- #abs ⇒ Object
-
#initialize(value) ⇒ Numeric
constructor
A new instance of Numeric.
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #round(n = 0) ⇒ Object
- #to_encoded ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(value) ⇒ Numeric
Returns a new instance of Numeric.
11 12 13 14 |
# File 'lib/encode_m/numeric.rb', line 11 def initialize(value) @value = parse_value(value) @encoded = Encoder.encode_integer(@value.is_a?(Integer) ? @value : @value.to_i) end |
Instance Attribute Details
#encoded ⇒ Object (readonly)
Returns the value of attribute encoded.
6 7 8 |
# File 'lib/encode_m/numeric.rb', line 6 def encoded @encoded end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/encode_m/numeric.rb', line 6 def value @value end |
Instance Method Details
#*(other) ⇒ Object
41 42 43 |
# File 'lib/encode_m/numeric.rb', line 41 def *(other) self.class.new(@value * coerce_value(other)) end |
#**(other) ⇒ Object
56 57 58 |
# File 'lib/encode_m/numeric.rb', line 56 def **(other) self.class.new(@value ** coerce_value(other)) end |
#+(other) ⇒ Object
Arithmetic operations
33 34 35 |
# File 'lib/encode_m/numeric.rb', line 33 def +(other) self.class.new(@value + coerce_value(other)) end |
#-(other) ⇒ Object
37 38 39 |
# File 'lib/encode_m/numeric.rb', line 37 def -(other) self.class.new(@value - coerce_value(other)) end |
#/(other) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/encode_m/numeric.rb', line 45 def /(other) divisor = coerce_value(other) raise ZeroDivisionError if divisor == 0 if @value.is_a?(Integer) && divisor.is_a?(Integer) && @value % divisor == 0 self.class.new(@value / divisor) else self.class.new(@value.to_f / divisor.to_f) end end |
#<=>(other) ⇒ Object
M language feature: encoded comparison
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/encode_m/numeric.rb', line 61 def <=>(other) case other when EncodeM::Numeric @encoded <=> other.encoded when EncodeM::String -1 # Numbers always sort before strings in M language when EncodeM::Composite # Let Composite handle the comparison -(other <=> self) when Numeric @encoded <=> self.class.new(other).encoded else nil end end |
#==(other) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/encode_m/numeric.rb', line 77 def ==(other) case other when EncodeM::Numeric @value == other.value when Numeric @value == other else false end end |
#abs ⇒ Object
88 89 90 |
# File 'lib/encode_m/numeric.rb', line 88 def abs self.class.new(@value.abs) end |
#negative? ⇒ Boolean
92 93 94 |
# File 'lib/encode_m/numeric.rb', line 92 def negative? @value < 0 end |
#positive? ⇒ Boolean
96 97 98 |
# File 'lib/encode_m/numeric.rb', line 96 def positive? @value > 0 end |
#round(n = 0) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/encode_m/numeric.rb', line 104 def round(n = 0) if n == 0 self.class.new(@value.round) else self.class.new(@value.to_f.round(n)) end end |
#to_encoded ⇒ Object
28 29 30 |
# File 'lib/encode_m/numeric.rb', line 28 def to_encoded @encoded end |
#to_f ⇒ Object
20 21 22 |
# File 'lib/encode_m/numeric.rb', line 20 def to_f @value.to_f end |
#to_i ⇒ Object
16 17 18 |
# File 'lib/encode_m/numeric.rb', line 16 def to_i @value.to_i end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/encode_m/numeric.rb', line 24 def to_s @value.to_s end |
#zero? ⇒ Boolean
100 101 102 |
# File 'lib/encode_m/numeric.rb', line 100 def zero? @value == 0 end |