Included Modules

Class Index [+]





the DefaultContext is the base for new contexts; it can be changed. DefaultContext =

                           :emin=> -16382, :emax=>+16383,
                           :traps=>[DivisionByZero, Overflow, InvalidOperation],

Public Class Methods

base_coercible_types() click to toggle source
    # File lib/flt/bin_num.rb, line 72
72:     def base_coercible_types
73:       unless defined? @base_coercible_types
74:         @base_coercible_types = super.merge(
75:           Float=>lambda{|x, context|
76:             if x.nan?
77:               BinNum.nan
78:             elsif x.infinite?
79:               BinNum.infinity(x<0 ? 1 : 1)
80:             elsif
82:             else
83:               Float.context.split(x)
84:             end
85:           }
86:         )
87:       end
88:       @base_coercible_types
89:     end
from_decimal(x, binfloat_context=nil) click to toggle source

DecNum to BinNum conversion.

     # File lib/flt/bin_num.rb, line 235
235:   def BinNum.from_decimal(x, binfloat_context=nil)
236:     Flt.BinNum(x.to_s, binfloat_context)
237:   end
int_div_radix_power(x,n) click to toggle source

Divide by an integral power of the base: x/(radix**n) for x,n integer; returns an integer.

    # File lib/flt/bin_num.rb, line 31
31:     def int_div_radix_power(x,n)
32:       n < 0 ? (x * (1<<(-n))) : (x / (1<<n))
33:     end
int_mult_radix_power(x,n) click to toggle source

Multiply by an integral power of the base: x*(radix**n) for x,n integer; returns an integer.

    # File lib/flt/bin_num.rb, line 25
25:     def int_mult_radix_power(x,n)
26:       n < 0 ? (x / (1<<(-n))) : (x * (1<<n))
27:     end
int_radix_power(n) click to toggle source

Integral power of the base: radix**n for integer n; returns an integer.

    # File lib/flt/bin_num.rb, line 19
19:     def int_radix_power(n)
20:       (n < 0) ? (2**n) : (1<<n)
21:     end
new(*args) click to toggle source

A BinNum value can be defined by:

  • A String containing a decimal text representation of the number

  • An Integer

  • A Rational

  • A Float

  • Another BinNum value.

  • A sign, coefficient and exponent (either as separate arguments, as an array or as a Hash with symbolic keys). This is the internal representation of DecNum, as returned by DecNum#split. The sign is +1 for plus and -1 for minus; the coefficient and exponent are integers, except for special values which are defined by :inf, :nan or :snan for the exponent.

  • Any other type for which custom conversion is defined in the context.

An optional Context can be passed as the last argument to override the current context; also a hash can be passed to override specific context parameters.

Except for custome defined conversions and text (String) input, BinNums are constructed with the precision specified by the input parameters (i.e. with the exact value specified by the parameters) and the context precision is ignored. If the BinNum is defined by a decimal text numeral, it is converted to a binary BinNum using the context precision.

The Flt.BinNum() constructor admits the same parameters and can be used as a shortcut for DecNum creation.

     # File lib/flt/bin_num.rb, line 198
198:   def initialize(*args)
199:     super(*args)
200:   end
one_half() click to toggle source
    # File lib/flt/bin_num.rb, line 14
14:     def one_half
15:       new [1, 1, 1]
16:     end
radix() click to toggle source

Numerical base.

    # File lib/flt/bin_num.rb, line 10
10:     def radix
11:       2
12:     end

Public Instance Methods

number_of_digits() click to toggle source
     # File lib/flt/bin_num.rb, line 202
202:   def number_of_digits
203:     @coeff.is_a?(Integer) ? _nbits(@coeff) : 0
204:   end
to_decimal(*args) click to toggle source

Approximate BinNum to DecNum conversion.

Convert to decimal so that if the decimal is converted to a BinNum of the same precision and with same rounding (i.e. BinNum.from_decimal(x, context)) the value of the BinNum is preserved, but use as few decimal digits as possible.

     # File lib/flt/bin_num.rb, line 230
230:   def to_decimal(*args)
231:     Num.convert(self, DecNum, *args)
232:   end
to_decimal_exact(dec_context=nil) click to toggle source

Exact BinNum to DecNum conversion: preserve BinNum value.

The current DecNum.context determines the valid range and the precision (if its is not :exact the result will be rounded)

     # File lib/flt/bin_num.rb, line 221
221:   def to_decimal_exact(dec_context=nil)
222:     Num.convert_exact(self, DecNum, dec_context)
223:   end
to_f() click to toggle source
     # File lib/flt/bin_num.rb, line 208
208:     def to_f
209:       if special?
210:         super
211:       else
212:         ::Math.ldexp(@sign*@coeff, @exp)
213:       end
214:     end
ulp(context=nil, mode=:low) click to toggle source

Unit in the last place: see Flt::Num#ulp()

For BinNum the result is normalized

     # File lib/flt/bin_num.rb, line 242
242:   def ulp(context=nil, mode=:low)
243:     super(context, mode).normalize(context)
244:   end

Disabled; run with --debug to generate this.


Generated with the Darkfish Rdoc Generator 1.1.6.