Class: Abachrome::AbcDecimal
- Inherits:
-
Object
- Object
- Abachrome::AbcDecimal
- Extended by:
- Forwardable
- Defined in:
- lib/abachrome/floatify.rb,
lib/abachrome/abc_decimal.rb
Constant Summary collapse
- DEFAULT_PRECISION =
(ENV["ABC_DECIMAL_PRECISION"] || "24").to_i
Instance Attribute Summary collapse
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.atan2(y, x) ⇒ AbcDecimal
Calculates the arctangent of y/x using the signs of the arguments to determine the quadrant.
-
.from_float(float, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal instance from a float value.
-
.from_integer(integer, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal from an integer value.
-
.from_rational(rational, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal from a Rational number.
-
.from_string(str, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal from a string representation of a number.
Instance Method Summary collapse
-
#%(other) ⇒ AbcDecimal
Performs modulo operation with another value.
-
#*(other) ⇒ AbcDecimal
Multiplies this AbcDecimal by another value.
-
#**(other) ⇒ AbcDecimal
Raises self to the power of another value.
-
#+(other) ⇒ Object
Addition operation # # Adds another value to this decimal.
-
#-(other) ⇒ AbcDecimal
Subtracts another numeric value from this AbcDecimal.
-
#/(other) ⇒ AbcDecimal
Divides this decimal by another value.
-
#<(other) ⇒ Boolean
Compares this decimal with another value.
-
#<=(other) ⇒ Boolean
Compares this AbcDecimal with another value.
-
#<=>(other) ⇒ Integer?
Compares this AbcDecimal instance with another AbcDecimal or a value that can be converted to an AbcDecimal.
-
#==(other) ⇒ Boolean
Compares this decimal value with another value for equality.
-
#>(other) ⇒ Boolean
Compares this decimal with another value.
-
#>=(other) ⇒ Boolean
Compares this decimal value with another value.
-
#abs(*args) ⇒ AbcDecimal
Returns the absolute value (magnitude) of the decimal number.
-
#clamp(min, max) ⇒ AbcDecimal
Constrains the value to be between the specified minimum and maximum values.
-
#coerce(other) ⇒ Array<AbcDecimal>
Allows for mixed arithmetic operations between AbcDecimal and other numeric types.
-
#initialize(value, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
constructor
Initializes a new AbcDecimal object with the specified value and precision.
-
#inspect ⇒ String
Returns a string representation of the decimal value for inspection purposes.
-
#negative? ⇒ Boolean
Returns true if the internal value is negative, false otherwise.
-
#positive? ⇒ Boolean
Returns true if the internal value is positive, false otherwise.
-
#round(*args) ⇒ AbcDecimal
Rounds this decimal to a specified precision.
-
#sqrt ⇒ AbcDecimal
Returns the square root of the AbcDecimal value.
-
#to_f ⇒ Float
Converts the decimal value to a floating-point number.
-
#to_s ⇒ String
Returns a string representation of the decimal value.
Constructor Details
#initialize(value, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Initializes a new AbcDecimal object with the specified value and precision.
If an AbcDecimal is provided, its internal value is used. If a BigDecimal or Rational is provided, it's used directly. Otherwise, the value is converted to a string and parsed as a BigDecimal. Defaults to DEFAULT_PRECISION.
31 32 33 34 35 36 37 38 |
# File 'lib/abachrome/floatify.rb', line 31 def initialize(value, _precision = nil) @value = case value when AbcDecimal value.value else value.to_f end end |
Instance Attribute Details
#precision ⇒ Object
Returns the value of attribute precision.
27 28 29 |
# File 'lib/abachrome/abc_decimal.rb', line 27 def precision @precision end |
#value ⇒ Object
Returns the value of attribute value.
21 22 23 |
# File 'lib/abachrome/floatify.rb', line 21 def value @value end |
Class Method Details
.atan2(y, x) ⇒ AbcDecimal
Calculates the arctangent of y/x using the signs of the arguments to determine the quadrant. Unlike the standard Math.atan2, this method accepts AbcDecimal objects or any values that can be converted to AbcDecimal.
274 275 276 277 278 |
# File 'lib/abachrome/floatify.rb', line 274 def self.atan2(y, x) y_value = y.is_a?(AbcDecimal) ? y.value : AbcDecimal(y).value x_value = x.is_a?(AbcDecimal) ? x.value : AbcDecimal(x).value new(Math.atan2(y_value, x_value)) end |
.from_float(float, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal instance from a float value.
77 78 79 |
# File 'lib/abachrome/floatify.rb', line 77 def self.from_float(float, _precision = nil) new(float) end |
.from_integer(integer, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal from an integer value.
86 87 88 |
# File 'lib/abachrome/floatify.rb', line 86 def self.from_integer(integer, _precision = nil) new(integer) end |
.from_rational(rational, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal from a Rational number.
68 69 70 |
# File 'lib/abachrome/floatify.rb', line 68 def self.from_rational(rational, _precision = nil) new(rational) end |
.from_string(str, precision = DEFAULT_PRECISION) ⇒ AbcDecimal
Creates a new AbcDecimal from a string representation of a number.
59 60 61 |
# File 'lib/abachrome/floatify.rb', line 59 def self.from_string(str, _precision = nil) new(str) end |
Instance Method Details
#%(other) ⇒ AbcDecimal
Performs modulo operation with another value.
133 134 135 136 |
# File 'lib/abachrome/floatify.rb', line 133 def %(other) other_value = other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value self.class.new(@value % other_value) end |
#*(other) ⇒ AbcDecimal
Multiplies this AbcDecimal by another value.
dec1 = AbcDecimal.new(5) dec2 = AbcDecimal.new(2) result = dec1 * dec2 # => AbcDecimal representing 10
With a non-AbcDecimal value
result = dec1 * 3 # => AbcDecimal representing 15
115 116 117 118 |
# File 'lib/abachrome/floatify.rb', line 115 def *(other) other_value = other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value self.class.new(@value * other_value) end |
#**(other) ⇒ AbcDecimal
Raises self to the power of another value. This method handles different input types, including Rational values and other AbcDecimal instances.
151 152 153 154 |
# File 'lib/abachrome/floatify.rb', line 151 def **(other) other_value = other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value self.class.new(@value**other_value) end |
#+(other) ⇒ Object
Addition operation
Adds another value to this decimal.
@param other [AbcDecimal, Numeric] The value to add. If not an AbcDecimal,
it will be converted to one.
@return [AbcDecimal] A new AbcDecimal instance with the sum of the two values
97 98 99 100 |
# File 'lib/abachrome/floatify.rb', line 97 def +(other) other_value = other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value self.class.new(@value + other_value) end |
#-(other) ⇒ AbcDecimal
Subtracts another numeric value from this AbcDecimal.
106 107 108 109 |
# File 'lib/abachrome/floatify.rb', line 106 def -(other) other_value = other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value self.class.new(@value - other_value) end |
#/(other) ⇒ AbcDecimal
Divides this decimal by another value.
decimal = AbcDecimal.new(10) result = decimal / 2
=> AbcDecimal.new(5)
124 125 126 127 |
# File 'lib/abachrome/floatify.rb', line 124 def /(other) other_value = other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value self.class.new(@value / other_value) end |
#<(other) ⇒ Boolean
Compares this decimal with another value.
dec1 = AbcDecimal.new(1.5) dec2 = AbcDecimal.new(2.0) dec1 < dec2 #=> true dec1 < 2.0 #=> true
217 218 219 |
# File 'lib/abachrome/floatify.rb', line 217 def <(other) @value < (other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value) end |
#<=(other) ⇒ Boolean
Compares this AbcDecimal with another value.
it will be converted to one. false otherwise.
227 228 229 |
# File 'lib/abachrome/floatify.rb', line 227 def <=(other) @value <= (other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value) end |
#<=>(other) ⇒ Integer?
Compares this AbcDecimal instance with another AbcDecimal or a value that can be converted to an AbcDecimal.
If not an AbcDecimal, it will be converted using AbcDecimal(). 0 if they are equal, 1 if self is greater than other, or nil if the comparison is not possible.
190 191 192 |
# File 'lib/abachrome/floatify.rb', line 190 def <=>(other) @value <=> (other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value) end |
#==(other) ⇒ Boolean
Compares this decimal value with another value for equality. Attempts to convert the other value to an AbcDecimal if it isn't one already.
177 178 179 |
# File 'lib/abachrome/floatify.rb', line 177 def ==(other) @value == (other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value) end |
#>(other) ⇒ Boolean
Compares this decimal with another value.
convertible to AbcDecimal
199 200 201 |
# File 'lib/abachrome/floatify.rb', line 199 def >(other) @value > (other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value) end |
#>=(other) ⇒ Boolean
Compares this decimal value with another value.
it will be converted to one. false otherwise.
209 210 211 |
# File 'lib/abachrome/floatify.rb', line 209 def >=(other) @value >= (other.is_a?(AbcDecimal) ? other.value : AbcDecimal(other).value) end |
#abs(*args) ⇒ AbcDecimal
Returns the absolute value (magnitude) of the decimal number.
Wraps BigDecimal#abs to ensure return values are properly converted to AbcDecimal.
244 245 246 |
# File 'lib/abachrome/floatify.rb', line 244 def abs(*_args) AbcDecimal(@value.abs) end |
#clamp(min, max) ⇒ AbcDecimal
Constrains the value to be between the specified minimum and maximum values.
AbcDecimal(5).clamp(0, 10) # => 5 AbcDecimal(15).clamp(0, 10) # => 10 AbcDecimal(-5).clamp(0, 10) # => 0
143 144 145 |
# File 'lib/abachrome/floatify.rb', line 143 def clamp(min, max) @value.clamp(AbcDecimal(min).value, AbcDecimal(max).value) end |
#coerce(other) ⇒ Array<AbcDecimal>
Allows for mixed arithmetic operations between AbcDecimal and other numeric types.
allowing Ruby to perform arithmetic operations with mixed types
161 162 163 |
# File 'lib/abachrome/floatify.rb', line 161 def coerce(other) [self.class.new(other), self] end |
#inspect ⇒ String
Returns a string representation of the decimal value for inspection purposes. This method returns a formatted string that includes the class name and the string representation of the decimal value itself.
168 169 170 |
# File 'lib/abachrome/floatify.rb', line 168 def inspect "#{self.class}('#{self}')" end |
#negative? ⇒ Boolean
Returns true if the internal value is negative, false otherwise.
258 259 260 |
# File 'lib/abachrome/floatify.rb', line 258 def negative? @value.negative? end |
#positive? ⇒ Boolean
Returns true if the internal value is positive, false otherwise.
265 266 267 |
# File 'lib/abachrome/floatify.rb', line 265 def positive? @value > 0 end |
#round(*args) ⇒ AbcDecimal
Rounds this decimal to a specified precision.
the number of decimal places to round to and the rounding mode. decimal = AbcDecimal(3.14159) decimal.round(2) #=> 3.14 decimal = AbcDecimal(3.5) decimal.round(0, half: :up) #=> 4
236 237 238 |
# File 'lib/abachrome/floatify.rb', line 236 def round(*args) AbcDecimal(@value.round(*args)) end |
#sqrt ⇒ AbcDecimal
Returns the square root of the AbcDecimal value. Calculates the square root by using Ruby's built-in Math.sqrt function and converting the result back to an AbcDecimal.
251 252 253 |
# File 'lib/abachrome/floatify.rb', line 251 def sqrt AbcDecimal(Math.sqrt(@value)) end |
#to_f ⇒ Float
Converts the decimal value to a floating-point number.
50 51 52 |
# File 'lib/abachrome/floatify.rb', line 50 def to_f @value end |
#to_s ⇒ String
Returns a string representation of the decimal value.
This method converts the internal value to a String, using a fixed-point notation format. If the internal value is a Rational, it's first converted to a BigDecimal with the configured precision before string conversion.
43 44 45 |
# File 'lib/abachrome/floatify.rb', line 43 def to_s @value.to_s end |