Class: Flt::BigDecimalContext
- 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
-
#copy_sign(x, y) ⇒ Object
Return copy of x with the sign of y.
-
#eval {|_self| ... } ⇒ Object
TODO: Context class with precision, rounding, etc.
- #exact? ⇒ Boolean
- #infinity(sign = +1) ⇒ Object
- #int_radix_power(n) ⇒ Object
- #minus(x) ⇒ Object
-
#nan ⇒ Object
NaN (not a number value).
- #Num(*args) ⇒ Object
- #num_class ⇒ Object
- #plus(x) ⇒ Object
- #precision ⇒ Object
- #radix ⇒ Object
- #rationalize(x, tol = nil) ⇒ Object
- #rounding ⇒ Object
-
#sign(x) ⇒ Object
Sign: -1 for minus, +1 for plus, nil for nan (note that Float zero is signed).
- #special?(x) ⇒ Boolean
- #split(x) ⇒ Object
-
#to_int_scale(x) ⇒ Object
Return the value of the number as an signed integer and a scale.
- #to_r(x) ⇒ Object
- #zero(sign = +1) ⇒ Object
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)
18 19 20 |
# File 'lib/flt/bigdecimal.rb', line 18 def eval yield self end |
#exact? ⇒ 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 |
#nan ⇒ Object
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_class ⇒ Object
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 |
#precision ⇒ Object
68 69 70 |
# File 'lib/flt/bigdecimal.rb', line 68 def precision BigDecimal.limit end |
#radix ⇒ Object
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 |
#rounding ⇒ Object
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
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 |