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:



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/flt/bigdecimal.rb', line 151

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



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/flt/bigdecimal.rb', line 97

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:



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

def eval
  yield self
end

#exact?Boolean

Returns:

  • (Boolean)


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

def exact?
  BigDecimal.limit == 0
end

#infinity(sign = +1) ⇒ Object



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

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

#int_radix_power(n) ⇒ Object



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

def int_radix_power(n)
  10**n
end

#minus(x) ⇒ Object



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

def minus(x)
  -x
end

#nanObject

NaN (not a number value)



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

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

#Num(*args) ⇒ Object



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

def Num(*args)
  args = *args if args.size==1 && args.first.is_a?(Array)
  if args.size > 1
    BigDecimal.new(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.new(x.to_s)
    end
  end
end

#num_classObject



22
23
24
# File 'lib/flt/bigdecimal.rb', line 22

def num_class
  BigDecimal
end

#plus(x) ⇒ Object



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

def plus(x)
  x
end

#precisionObject



68
69
70
# File 'lib/flt/bigdecimal.rb', line 68

def precision
  BigDecimal.limit
end

#radixObject



43
44
45
# File 'lib/flt/bigdecimal.rb', line 43

def radix
  10
end

#rationalize(x, tol = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/flt/bigdecimal.rb', line 139

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



82
83
84
# File 'lib/flt/bigdecimal.rb', line 82

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)



87
88
89
90
91
92
93
94
# File 'lib/flt/bigdecimal.rb', line 87

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)


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

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

#split(x) ⇒ Object



111
112
113
114
# File 'lib/flt/bigdecimal.rb', line 111

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.



117
118
119
120
121
# File 'lib/flt/bigdecimal.rb', line 117

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



135
136
137
# File 'lib/flt/bigdecimal.rb', line 135

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

#zero(sign = +1) ⇒ Object



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

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