Module: BigDecimal::Internal

Defined in:
lib/bigdecimal.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.coerce_to_bigdecimal(x, prec, method_name) ⇒ Object

Coerce x to BigDecimal with the specified precision. TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

:nodoc:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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_resultObject

:nodoc:



50
51
52
53
54
55
# 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_resultObject

:nodoc:



57
58
59
60
61
62
# 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