Module: BigDecimal::Internal
Do not use. This module is for internal use only.
| Relationships & Source Files | |
| Defined in: | lib/bigdecimal.rb |
Class Method Summary
-
.coerce_to_bigdecimal(x, prec, method_name)
Coerce x to
::BigDecimalwith the specified precision. - .coerce_validate_prec(prec, method_name, accept_zero: false)
- .infinity_computation_result
- .nan_computation_result
Class Method Details
.coerce_to_bigdecimal(x, prec, method_name)
Coerce x to ::BigDecimal with the specified precision. TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
# File 'lib/bigdecimal.rb', line 18
def self.coerce_to_bigdecimal(x, prec, method_name) # :nodoc: case x when BigDecimal return x when Integer, Float return BigDecimal(x, 0) when Rational return BigDecimal(x, [prec, 2 * BigDecimal.double_fig].max) end raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal" end
.coerce_validate_prec(prec, method_name, accept_zero: false)
[ GitHub ]# File 'lib/bigdecimal.rb', line 30
def self.coerce_validate_prec(prec, method_name, accept_zero: false) # :nodoc: unless Integer === prec original = prec # Emulate Integer.try_convert for ruby < 3.1 if prec.respond_to?(:to_int) prec = prec.to_int else raise TypeError, "no implicit conversion of #{original.class} into Integer" end raise TypeError, "can't convert #{original.class} to Integer" unless Integer === prec end if accept_zero raise ArgumentError, "Negative precision for #{method_name}" if prec < 0 else raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0 end prec end
.infinity_computation_result
[ GitHub ]# File 'lib/bigdecimal.rb', line 50
def self.infinity_computation_result # :nodoc: if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_INFINITY) raise FloatDomainError, "Computation results in 'Infinity'" end BigDecimal::INFINITY end
.nan_computation_result
[ GitHub ]# File 'lib/bigdecimal.rb', line 57
def self.nan_computation_result # :nodoc: if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN) raise FloatDomainError, "Computation results to 'NaN'" end BigDecimal::NAN end