Class Index [+]

Quicksearch

Flt::FloatingTolerance

Implementation of floating tolerances

Public Class Methods

float_minimum_normalized_fraction() click to toggle source
     # File lib/flt/tolerance.rb, line 330
330:     def self.float_minimum_normalized_fraction
331:       @float_minimum_normalized_fraction
332:     end
new(value, radix=:native) click to toggle source
     # File lib/flt/tolerance.rb, line 316
316:     def initialize(value, radix=:native)
317:       super(value)
318:       @radix = radix
319:     end
ref_adjusted_exp() click to toggle source
     # File lib/flt/tolerance.rb, line 334
334:     def self.ref_adjusted_exp
335:       1
336:     end

Public Instance Methods

relative_to_many(mode, *xs) click to toggle source
     # File lib/flt/tolerance.rb, line 338
338:     def relative_to_many(mode, *xs)
339:       exp = nil
340: 
341:       num_class = xs.first.class
342:       context = num_class.context
343:       xs = xs.map{|x| x = context.Num(x); x.zero? ? context.minimum_normal(context.sign(x)) : x}
344:       v = cast_value(num_class)
345: 
346:       # TODO: simplify using context
347:       case xs.first
348:       when Flt::Num
349:         # TODO: handle special values
350:         if @radix == :native || @radix == num_class.radix
351:           exp = xs.map do |x|
352:             x = x.normalize
353:             exp = x.adjusted_exponent
354:             exp -= 1 if x.coefficient == x.num_class.context.minimum_normalized_coefficient # if :low mode
355:             exp -= FloatingTolerance.ref_adjusted_exp
356:             exp
357:           end.send(mode)
358:           r = num_class.Num(1, v.coefficient, v.exponent+exp)
359:           r = r.normalize if num_class.radix == 2
360:           r
361:         elsif @radix==10
362:           # assert x.class==BinNum
363:           # TODO: optimize (implement log10 for BinNum)
364:           exp = xs.map do |x|
365:             x = x.to_decimal_exact(:exact=>true).normalize
366:             exp = x.adjusted_exponent
367:             exp -= 1 if x.coefficient == x.num_class.context.minimum_normalized_coefficient # if :low mode
368:             exp -= FloatingTolerance.ref_adjusted_exp
369:             exp
370:           end.send(mode)
371:           num_class.from_decimal(Flt.DecNum(1, 1, exp)*v.to_decimal_exact)
372:         else
373:           # assert num_class==DecNum && @radix==2
374:           exp = xs.map do |x|
375:             exp = (x.ln/DecNum(2).ln).ceil.to_i - 1 # (x.ln/DecNum(2).ln).floor+1 - 1 if :high mode
376:             exp -= FloatingTolerance.ref_adjusted_exp
377:             exp
378:           end.send(mode)
379:           v*num_class.Num(2)**exp
380:         end
381:       when Float
382:         if @radix == :native || @radix == Float::RADIX
383:           exp = xs.map do |x|
384:             f,e = Math.frexp(x)
385:             exp = e-1
386:             exp -= 1 if f==FloatingTolerance.float_minimum_normalized_fraction # if :low mode
387:             exp -= FloatingTolerance.ref_adjusted_exp
388:           end.send(mode)
389:           Math.ldexp(v.to_f, exp)
390:         else
391:           # assert @radix==10
392:           exp = xs.map do |x|
393:             exp = Math.log10(x.abs).ceil - 1 # Math.log10(x.abs).floor+1 - 1 if :high mode
394:             exp -= FloatingTolerance.ref_adjusted_exp
395:           end.send(mode)
396:           v*10.0**exp
397:         end
398:       when BigDecimal
399:         if @radix == :native || @radix == 10
400:           exp = xs.map do |x|
401:             sign,digits,base,exp = x.split
402:             exp -= 1
403:             exp -= 1 if digits=="1" # if :low mode
404:             exp -= FloatingTolerance.ref_adjusted_exp
405:             exp
406:           end.send(mode)
407:           sign, digits, base, vexp = v.split
408:           BigDecimal.new("0.#{digits}E#{vexp+exp}")
409:         else
410:           # assert num_class==BigDecimal && @radix==2
411:           prec = 10
412:           exp = xs.map do |x|
413:             exp = (Flt::DecNum(x.to_s).ln/Flt::DecNum(2).ln).ceil - 1 # ... if :high mode
414:             exp -= FloatingTolerance.ref_adjusted_exp
415:             exp
416:           end.send(mode)
417:           context.Num(v)*context.Num(2)**exp
418:         end
419:       end
420:     end
to_s() click to toggle source
     # File lib/flt/tolerance.rb, line 321
321:     def to_s
322:       if @radix==:native
323:         "#{descr_value} flt."
324:       else
325:         "#{descr_value} flt.(#{radix})"
326:       end
327:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.