Module: Nio

Defined in:
lib/nio/flttol.rb,
lib/nio/fmt.rb,
lib/nio/sugar.rb,
lib/nio/tools.rb,
lib/nio/repdec.rb,
lib/nio/rtnlzr.rb,
lib/nio/version.rb

Overview

:nodoc:

Defined Under Namespace

Modules: BurgerDybvig, Clinger, Formattable, StateEquivalent, VERSION Classes: BigTolerance, DigitsDef, Fmt, NeutralNum, RepDec, RepDecError, Rtnlzr, Tolerance

Class Method Summary collapse

Class Method Details

.BigDec(x, prec = nil) ⇒ Object

BigDec(x) -> a BigDecimal

BigDec(x,precision) -> a BigDecimal
BigDec(x,:exact) -> a BigDecimal

This is a shortcut to define a BigDecimal without using quotes and a general conversion to BigDecimal method.

The second parameter can be :exact to try for an exact conversion

Conversions from Float have issues that should be understood; :exact conversion will use the exact internal value of the Float, and when no precision is specified, a value as simple as possible expressed as a fraction will be used.



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/nio/flttol.rb', line 636

def BigDec(x,prec=nil) # :doc:
  if x.respond_to?(:to_str)
    x = BigDecimal(x.to_str, prec||0)
  else
    case x
      when Integer
      x = BigDecimal(x.to_s)
    when Rational
      if prec && prec!=:exact
        x = BigDecimal.new(x.numerator.to_s).div(x.denominator,prec)
      else
        x = BigDecimal.new(x.numerator.to_s)/BigDecimal.new(x.denominator.to_s)
      end
    when BigDecimal
    when Float
      x = nio_float_to_bigdecimal(x,prec)
    end
  end
  x
end

.BigTol(x) ⇒ Object

BigTol(x) -> a BigTolerance This module function will convert its argument to a Noi::BigTolerance

Values of type BigTolerance or Numeric are accepted.



611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/nio/flttol.rb', line 611

def BigTol(x) # :doc:
  case x
    when BigTolerance
      x
    when Integer
      BigTolerance.sig_decimals(x)
    when Rational
      x
    else
      BigTolerance.new(BigDec(x))
  end
end

.convert(x, type, mode = :approx) ⇒ Object

This is not a module function: this provides a shorthand access to Nio::Fmt.convert



26
27
28
# File 'lib/nio/sugar.rb', line 26

def Nio.convert(x, type, mode=:approx)
  Nio::Fmt.convert x, type, mode
end

.gcd(a, b) ⇒ Object



500
501
502
503
504
505
# File 'lib/nio/repdec.rb', line 500

def gcd(a,b)
  while b!=0 do
    a,b = b, a.modulo(b)
  end
  return a.abs;  
end

.nio_convert(x, type, mode = :approx) ⇒ Object

This module function can be used after import Nio



33
34
35
# File 'lib/nio/sugar.rb', line 33

def nio_convert(x, type, mode=:approx)
  Nio::Fmt.convert x, type, mode
end

.nio_float_to_bigdecimal(x, prec) ⇒ Object

:nodoc:



1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
# File 'lib/nio/fmt.rb', line 1263

def nio_float_to_bigdecimal(x,prec) # :nodoc:
  if prec.nil?
    x = Fmt.convert(x,BigDecimal,:approx)          
  elsif prec==:exact
    x = Fmt.convert(x,BigDecimal,:exact) 
  else
    x = BigDecimal(x.nio_write(Nio::Fmt.new.prec(prec,:sig)))
  end
  x
end

.numeric_cast(value, type) ⇒ Object



657
658
659
660
661
662
663
664
665
666
667
# File 'lib/nio/flttol.rb', line 657

def numeric_cast(value, type)
  if value.respond_to?(:prec)
    value.prec(type)
   else
     if type.kind_of?(BigDecimal)
       BigDec(value)
     else
       Object.send type.to_s, value
     end
   end
end

.Tol(x) ⇒ Object

Tol(x) -> a Tolerance This module function will convert its argument to a Noi::Tolerance or a Noi::BigTolerance depending on its argument;

Values of type Tolerance,Float,Integer (for Tolerance) or BigTolerance,BigDecimal (for BigTolerance) are accepted.



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/nio/flttol.rb', line 590

def Tol(x) # :doc:
  case x
    when Tolerance
      x
    when BigTolerance
      x
    when BigDecimal
      BigTolerance.new(x)
    when Float
      Tolerance.new(x)
    when Integer
      Tolerance.sig_decimals(x)
    else # e.g. Rational
      x 
  end
end