Class: Flt::BigDecimalContext

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/flt/bigdecimal.rb

Overview

Context class with some of the Flt::Num context functionality, to allow the use of BigDecimal numbers similarly to other Num values; this eases the implementation of functions compatible with either Num or BigDecimal values.

Constant Summary collapse

ROUNDING_MODES =
{
  BigDecimal::ROUND_UP=>:up,
  BigDecimal::ROUND_DOWN=>:down,
  BigDecimal::ROUND_CEILING=>:ceiling,
  BigDecimal::ROUND_FLOOR=>:floor,
  BigDecimal::ROUND_HALF_UP=>:half_up,
  BigDecimal::ROUND_HALF_DOWN=>:half_down,
  BigDecimal::ROUND_HALF_EVEN=>:half_even
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.big_decimal_method(*methods) ⇒ Object

:nodoc:


148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/flt/bigdecimal.rb', line 148

def big_decimal_method(*methods) #:nodoc:
  methods.each do |method|
    if method.is_a?(Array)
      float_method, context_method = method
    else
      float_method = context_method = method
    end
    define_method(context_method) do |x|
      Num(x).send float_method
    end
  end
end

Instance Method Details

#copy_sign(x, y) ⇒ Object

Return copy of x with the sign of y


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/flt/bigdecimal.rb', line 94

def copy_sign(x, y)
  self_sign = sign(x)
  other_sign = y.is_a?(Integer) ? (y < 0 ? -1 : +1) : y.sign
  if self_sign && other_sign
    if self_sign == other_sign
      x
    else
      -x
    end
  else
    nan
  end
end

#eval {|_self| ... } ⇒ Object

TODO: Context class with precision, rounding, etc. (no singleton)

Yields:

  • (_self)

Yield Parameters:


15
16
17
# File 'lib/flt/bigdecimal.rb', line 15

def eval
  yield self
end

#exact?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'lib/flt/bigdecimal.rb', line 61

def exact?
  BigDecimal.limit == 0
end

#infinity(sign = +1) ⇒ Object


49
50
51
# File 'lib/flt/bigdecimal.rb', line 49

def infinity(sign=+1)
  BigDecimal(sign.to_s)/BigDecimal('0')
end

#int_radix_power(n) ⇒ Object


57
58
59
# File 'lib/flt/bigdecimal.rb', line 57

def int_radix_power(n)
  10**n
end

#minus(x) ⇒ Object


128
129
130
# File 'lib/flt/bigdecimal.rb', line 128

def minus(x)
  -x
end

#nanObject

NaN (not a number value)


45
46
47
# File 'lib/flt/bigdecimal.rb', line 45

def nan
  BigDecimal('0')/BigDecimal('0')
end

#Num(*args) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/flt/bigdecimal.rb', line 23

def Num(*args)
  args = *args if args.size==1 && args.first.is_a?(Array)
  if args.size > 1
    BigDecimal(Flt::DecNum(*args).to_s)
  else
    x = args.first
    case x
    when BigDecimal
      x
    when Rational
      BigDecimal(x.numerator.to_s)/BigDecimal(x.denominator.to_s)
    else
      BigDecimal(x.to_s)
    end
  end
end

#num_classObject


19
20
21
# File 'lib/flt/bigdecimal.rb', line 19

def num_class
  BigDecimal
end

#plus(x) ⇒ Object


124
125
126
# File 'lib/flt/bigdecimal.rb', line 124

def plus(x)
  x
end

#precisionObject


65
66
67
# File 'lib/flt/bigdecimal.rb', line 65

def precision
  BigDecimal.limit
end

#radixObject


40
41
42
# File 'lib/flt/bigdecimal.rb', line 40

def radix
  10
end

#rationalize(x, tol = nil) ⇒ Object


136
137
138
139
140
141
142
143
144
# File 'lib/flt/bigdecimal.rb', line 136

def rationalize(x, tol = nil)
  tol ||= Flt::Tolerance([x.precs[0], Float::DIG].max,:sig_decimals)
  case tol
  when Integer
    Rational(*Support::Rationalizer.max_denominator(x, tol, BigDecimal))
  else
    Rational(*Support::Rationalizer[tol].rationalize(x))
  end
end

#roundingObject


79
80
81
# File 'lib/flt/bigdecimal.rb', line 79

def rounding
  ROUNDING_MODES[BigDecimal.mode(BigDecimal::ROUND_MODE, nil)]
end

#sign(x) ⇒ Object

Sign: -1 for minus, +1 for plus, nil for nan (note that BigDecimal zero is signed)


84
85
86
87
88
89
90
91
# File 'lib/flt/bigdecimal.rb', line 84

def sign(x)
  big_dec_sign = x.sign
  if big_dec_sign < 0
    -1
  elsif big_dec_sign > 0
    +1
  end
end

#special?(x) ⇒ Boolean

Returns:

  • (Boolean)

120
121
122
# File 'lib/flt/bigdecimal.rb', line 120

def special?(x)
  x.nan? || x.infinite?
end

#split(x) ⇒ Object


108
109
110
111
# File 'lib/flt/bigdecimal.rb', line 108

def split(x)
  sgn, d, _b, e = x.split
  [sgn<0 ? -1 : +1, d.to_i, e-d.size]
end

#to_int_scale(x) ⇒ Object

Return the value of the number as an signed integer and a scale.


114
115
116
117
118
# File 'lib/flt/bigdecimal.rb', line 114

def to_int_scale(x)
  sgn, d, _b, e = x.split
  c = d.to_i
  [sgn<0 ? -1 : c, -c, e-d.size]
end

#to_r(x) ⇒ Object


132
133
134
# File 'lib/flt/bigdecimal.rb', line 132

def to_r(x)
  Support::Rationalizer.to_r(x)
end

#zero(sign = +1) ⇒ Object


53
54
55
# File 'lib/flt/bigdecimal.rb', line 53

def zero(sign=+1)
  BigDecimal("#{(sign < 0) ? '-' : ''}0")
end