Module: Horologium::PreciseValue
- Includes:
- Comparable
- Defined in:
- lib/horologium/precise_value.rb,
sig/horologium/precise_value.rbs
Overview
Instance Attribute Summary collapse
-
#precision ⇒ Symbol
readonly
private
The precision, set when the value was built.
-
#rational ⇒ Rational
readonly
private
The value denoted, as a Rational, computed once at construction so a frozen value never recomputes it for comparison.
-
#value ⇒ Horologium::Numeric::TwoPartFloat, Horologium::Numeric::Exact
readonly
private
The numeric value, held as a Numeric::TwoPartFloat or a Numeric::Exact.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Orders by the value denoted, across precisions.
-
#eql?(other) ⇒ Boolean
Stricter than
==: the precision must match too. -
#hash ⇒ Integer
A hash matching #eql?.
-
#initialize(value, precision) ⇒ PreciseValue
private
Wraps a numeric value in a precision.
Instance Attribute Details
#precision ⇒ Symbol (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The precision, set when the value was built.
21 22 23 |
# File 'lib/horologium/precise_value.rb', line 21 def precision @precision end |
#rational ⇒ Rational (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The value denoted, as a Rational, computed once at construction so a frozen value never recomputes it for comparison.
77 78 79 |
# File 'lib/horologium/precise_value.rb', line 77 def rational @rational end |
#value ⇒ Horologium::Numeric::TwoPartFloat, Horologium::Numeric::Exact (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The numeric value, held as a Numeric::TwoPartFloat or a Numeric::Exact.
15 16 17 |
# File 'lib/horologium/precise_value.rb', line 15 def value @value end |
Instance Method Details
#<=>(other) ⇒ Integer?
Orders by the value denoted, across precisions. The same value compares
equal whatever the precision, so == (from Comparable) and sorting
ignore it.
49 50 51 52 53 |
# File 'lib/horologium/precise_value.rb', line 49 def <=>(other) return unless other.is_a?(self.class) rational <=> other.rational end |
#eql?(other) ⇒ Boolean
Stricter than ==: the precision must match too.
59 60 61 62 63 |
# File 'lib/horologium/precise_value.rb', line 59 def eql?(other) other.is_a?(self.class) && precision == other.precision && rational == other.rational end |
#hash ⇒ Integer
Returns a hash matching #eql?.
66 67 68 |
# File 'lib/horologium/precise_value.rb', line 66 def hash [self.class, precision, rational].hash end |
#initialize(value, precision) ⇒ PreciseValue
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wraps a numeric value in a precision. The value must match the
precision: a Numeric::Exact for :exact, a Numeric::TwoPartFloat
for :standard.
33 34 35 36 37 38 39 40 |
# File 'lib/horologium/precise_value.rb', line 33 def initialize(value, precision) Numeric::Precision.validate_value!(value, precision) @value = value @precision = precision @rational = value.to_r freeze end |