Class: Flt::BinNum

Inherits:
Num show all
Defined in:
lib/flt/trigonometry.rb,
lib/flt/bin_num.rb

Overview

Num

Defined Under Namespace

Modules: CMath, Math, Trigonometry Classes: Context

Constant Summary collapse

DefaultContext =

the DefaultContext is the base for new contexts; it can be changed. DefaultContext = BinNum::Context.new(

:precision=>113,
:emin=> -16382, :emax=>+16383,
:rounding=>:half_even,
:flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)
BinNum::Context.new(
:exact=>false, :precision=>53, :rounding=>:half_even,
:emin=> -1025, :emax=>+1023,                             :flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)
ExtendedContext =
BinNum::Context.new(DefaultContext,
:traps=>[], :flags=>[], :clamp=>false)
IEEEHalfContext =
BinNum::Context.new(
:precision=>1,
:emin=> -14, :emax=>+15,
:rounding=>:half_even,
:flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)
IEEESingleContext =
BinNum::Context.new(
:precision=>24,
:emin=> -126, :emax=>+127,
:rounding=>:half_even,
:flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)
IEEEDoubleContext =
BinNum::Context.new(
:precision=>53,
:emin=> -1022, :emax=>+1023,
:rounding=>:half_even,
:flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)
IEEEQuadContext =
BinNum::Context.new(
:precision=>113,
:emin=> -16382, :emax=>+16383,
:rounding=>:half_even,
:flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)
IEEEExtendedContext =
BinNum::Context.new(
:precision=>64,
:emin=> -16382, :emax=>+16383,
:rounding=>:half_even,
:flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)
FloatContext =
BinNum::Context.new(
:precision=>Float::MANT_DIG,
:rounding=>Support::AuxiliarFunctions.detect_float_rounding,
:emin=>Float::MIN_EXP-1, :emax=>Float::MAX_EXP-1,
:flags=>[],
:traps=>[DivisionByZero, Overflow, InvalidOperation],
:ignored_flags=>[],
:capitals=>true,
:clamp=>true)

Constants inherited from Num

Num::EXCEPTIONS, Num::ROUND_05UP, Num::ROUND_CEILING, Num::ROUND_DOWN, Num::ROUND_FLOOR, Num::ROUND_HALF_DOWN, Num::ROUND_HALF_EVEN, Num::ROUND_HALF_UP, Num::ROUND_UP

Constants included from Num::AuxiliarFunctions

Num::AuxiliarFunctions::EXP_INC, Num::AuxiliarFunctions::LOG10_LB_CORRECTION, Num::AuxiliarFunctions::LOG10_MULT, Num::AuxiliarFunctions::LOG2_LB_CORRECTION, Num::AuxiliarFunctions::LOG2_MULT, Num::AuxiliarFunctions::LOG_PREC_INC, Num::AuxiliarFunctions::LOG_RADIX_EXTRA, Num::AuxiliarFunctions::LOG_RADIX_INC

Constants included from Support::AuxiliarFunctions

Support::AuxiliarFunctions::NBITS_BLOCK, Support::AuxiliarFunctions::NBITS_LIMIT, Support::AuxiliarFunctions::NDIGITS_BLOCK, Support::AuxiliarFunctions::NDIGITS_LIMIT

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Num

#%, #*, #**, #+, #+@, #-, #-@, #/, #<=>, #==, Context, Flags, Num, [], #_abs, #_check_nans, #_fix, #_fix_nan, #_neg, #_pos, #_rescale, #_watched_rescale, #abs, #add, #adjusted_exponent, base_conversions, ccontext, #ceil, #coefficient, #coerce, #compare, context, context=, #convert_to, #copy_abs, #copy_negate, #copy_sign, define_context, #digits, #div, #divide, #divide_int, #divmod, #divrem, #eql?, #even?, #exp, #exponent, #finite?, #floor, #fma, #fraction_part, #fractional_exponent, #hash, #infinite?, infinity, #inspect, #integer_part, #integral?, #integral_exponent, #integral_significand, #ln, local_context, #log, #log10, #log2, #logb, math, #minus, #modulo, #multiply, nan, #nan?, #next_minus, #next_plus, #next_toward, #nonzero?, #normal?, #normalize, num_class, #num_class, #number_class, #odd?, #plus, #power, #qnan?, #quantize, #reduce, #remainder, #remainder_near, #rescale, #round, #same_quantum?, #scaleb, #scientific_exponent, set_context, #sign, #snan?, #special?, #split, #sqrt, #subnormal?, #subtract, #to_i, #to_int_scale, #to_integral_exact, #to_integral_value, #to_r, #to_s, #truncate, zero, #zero?

Methods included from Support

FlagValues, Flags, simplified_round_mode

Methods included from Num::AuxiliarFunctions

_convert, _div_nearest, _exp, _iexp, _ilog, _log, _log_radix_digits, _log_radix_lb, _log_radix_mult, _normalize, _number_of_digits, _parser, _power, _rshift_nearest, _sqrt_nearest, log10_lb, log2_lb

Methods included from Support::AuxiliarFunctions

_nbits, _ndigits, detect_float_rounding

Methods inherited from Numeric

#even?, #odd?, #sign

Constructor Details

#initialize(*args) ⇒ BinNum

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.



198
199
200
# File 'lib/flt/bin_num.rb', line 198

def initialize(*args)
  super(*args)
end

Class Method Details

.base_coercible_typesObject

BinNum class methods



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/flt/bin_num.rb', line 72

def base_coercible_types
  unless defined? @base_coercible_types
    @base_coercible_types = super.merge(
      Float=>lambda{|x, context|
        if x.nan?
          BinNum.nan
        elsif x.infinite?
          BinNum.infinity(x<0 ? -1 : +1)
        elsif x.zero?
          BinNum.zero(Float.context.sign(x))
        else
          Float.context.split(x)
        end
      }
    )
  end
  @base_coercible_types
end

.from_decimal(x, binfloat_context = nil) ⇒ Object

DecNum to BinNum conversion.



235
236
237
# File 'lib/flt/bin_num.rb', line 235

def BinNum.from_decimal(x, binfloat_context=nil)
  Flt.BinNum(x.to_s, binfloat_context)
end

.int_div_radix_power(x, n) ⇒ Object

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



31
32
33
# File 'lib/flt/bin_num.rb', line 31

def int_div_radix_power(x,n)
  n < 0 ? (x * (1<<(-n))) : (x / (1<<n))
end

.int_mult_radix_power(x, n) ⇒ Object

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



25
26
27
# File 'lib/flt/bin_num.rb', line 25

def int_mult_radix_power(x,n)
  n < 0 ? (x / (1<<(-n))) : (x * (1<<n))
end

.int_radix_power(n) ⇒ Object

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



19
20
21
# File 'lib/flt/bin_num.rb', line 19

def int_radix_power(n)
  (n < 0) ? (2**n) : (1<<n)
end

.one_halfObject



14
15
16
# File 'lib/flt/bin_num.rb', line 14

def one_half
  new [+1, 1, -1]
end

.radixObject

Numerical base.



10
11
12
# File 'lib/flt/bin_num.rb', line 10

def radix
  2
end

Instance Method Details

#number_of_digitsObject



202
203
204
# File 'lib/flt/bin_num.rb', line 202

def number_of_digits
  @coeff.is_a?(Integer) ? _nbits(@coeff) : 0
end

#to_decimal(*args) ⇒ Object

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.



230
231
232
# File 'lib/flt/bin_num.rb', line 230

def to_decimal(*args)
  Num.convert(self, DecNum, *args)
end

#to_decimal_exact(dec_context = nil) ⇒ Object

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)



221
222
223
# File 'lib/flt/bin_num.rb', line 221

def to_decimal_exact(dec_context=nil)
  Num.convert_exact(self, DecNum, dec_context)
end

#to_fObject



208
209
210
211
212
213
214
# File 'lib/flt/bin_num.rb', line 208

def to_f
  if special?
    super
  else
    ::Math.ldexp(@sign*@coeff, @exp)
  end
end

#ulp(context = nil, mode = :low) ⇒ Object

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

For BinNum the result is normalized



242
243
244
# File 'lib/flt/bin_num.rb', line 242

def ulp(context=nil, mode=:low)
  super(context, mode).normalize(context)
end