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
- #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.
- #zero(sign = +1) ⇒ Object
Class Method Details
.big_decimal_method(*methods) ⇒ Object
:nodoc:
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/flt/bigdecimal.rb', line 131 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
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/flt/bigdecimal.rb', line 91 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)
17 18 19 |
# File 'lib/flt/bigdecimal.rb', line 17 def eval yield self end |
#exact? ⇒ Boolean
63 64 65 |
# File 'lib/flt/bigdecimal.rb', line 63 def exact? BigDecimal.limit == 0 end |
#infinity(sign = +1) ⇒ Object
51 52 53 |
# File 'lib/flt/bigdecimal.rb', line 51 def infinity(sign=+1) BigDecimal(sign.to_s)/BigDecimal('0') end |
#int_radix_power(n) ⇒ Object
59 60 61 |
# File 'lib/flt/bigdecimal.rb', line 59 def int_radix_power(n) 10**n end |
#minus(x) ⇒ Object
125 126 127 |
# File 'lib/flt/bigdecimal.rb', line 125 def minus(x) -x end |
#nan ⇒ Object
NaN (not a number value)
47 48 49 |
# File 'lib/flt/bigdecimal.rb', line 47 def nan BigDecimal('0')/BigDecimal('0') end |
#Num(*args) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/flt/bigdecimal.rb', line 25 def Num(*args) args = *args if args.size==1 && args.first.is_a?(Array) if args.size > 1 BigDecimal.new(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
21 22 23 |
# File 'lib/flt/bigdecimal.rb', line 21 def num_class BigDecimal end |
#plus(x) ⇒ Object
121 122 123 |
# File 'lib/flt/bigdecimal.rb', line 121 def plus(x) x end |
#precision ⇒ Object
67 68 69 |
# File 'lib/flt/bigdecimal.rb', line 67 def precision BigDecimal.limit end |
#radix ⇒ Object
42 43 44 |
# File 'lib/flt/bigdecimal.rb', line 42 def radix 10 end |
#rounding ⇒ Object
81 82 83 |
# File 'lib/flt/bigdecimal.rb', line 81 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)
86 87 88 |
# File 'lib/flt/bigdecimal.rb', line 86 def sign(x) x.sign < 0 ? -1 : +1 end |
#special?(x) ⇒ Boolean
117 118 119 |
# File 'lib/flt/bigdecimal.rb', line 117 def special?(x) x.nan? || x.infinite? end |
#split(x) ⇒ Object
105 106 107 108 |
# File 'lib/flt/bigdecimal.rb', line 105 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.
111 112 113 114 115 |
# File 'lib/flt/bigdecimal.rb', line 111 def to_int_scale(x) sgn, d, b, e = x.split c = d.to_i [sgn<0 ? -1 : c, -c, e-d.size] end |
#zero(sign = +1) ⇒ Object
55 56 57 |
# File 'lib/flt/bigdecimal.rb', line 55 def zero(sign=+1) BigDecimal("#{(sign < 0) ? '-' : ''}0") end |