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:



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

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



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

def copy_sign(x, y)
  self_sign = x.sign
  other_sign = y.is_a?(Integer) ? (y < 0 ? -1 : +1) : y.sign
  if self_sign && other_sign
    if self_sign == other_sign
      x.to_f
    else
      -x.to_f
    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



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

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



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

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



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

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 Float zero is signed)



87
88
89
# File 'lib/flt/bigdecimal.rb', line 87

def sign(x)
  x.sign < 0 ? -1 : +1
end

#special?(x) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#split(x) ⇒ Object



106
107
108
109
# File 'lib/flt/bigdecimal.rb', line 106

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.



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

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



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

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