Class: Rational

Inherits:
Object
  • Object
show all
Includes:
Nio::Formattable
Defined in:
lib/nio/fmt.rb,
lib/nio/rtnlzr.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Nio::Formattable

append_features, #nio_round, #nio_write

Class Method Details

.nio_read_neutral(neutral) ⇒ Object



1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
# File 'lib/nio/fmt.rb', line 1553

def self.nio_read_neutral(neutral)
  x = nil
  
  if neutral.special?
    case neutral.special
      when :nan
        x = Rational(0,0)
      when :inf
        x = Rational((neutral.sign=='-' ? -1 : +1),0)
    end
  else
    x = Rational(*neutral.to_RepDec.getQ)
  end
  
  return x
end

Instance Method Details

#nio_num_denObject

helper method to return both the numerator and denominator



99
100
101
# File 'lib/nio/rtnlzr.rb', line 99

def nio_num_den
  return [numerator,denominator]
end

#nio_write_neutral(fmt) ⇒ Object



1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
# File 'lib/nio/fmt.rb', line 1569

def nio_write_neutral(fmt)
  neutral = Nio::NeutralNum.new
  x = self
  
  if x.denominator==0
    if x.numerator>0
      neutral.set_special(:inf)
    elsif x.numerator<0
      neutral.set_special(:inf,'-')
    else
      neutral.set_special(:nan)
    end
  else
    if fmt.get_base==10
      rd = Nio::RepDec.new.setQ(x.numerator,x.denominator)
    else
      opt = Nio::RepDec::DEF_OPT.dup.set_digits(fmt.get_base_digits)
      rd = Nio::RepDec.new.setQ(x.numerator,x.denominator, opt)
    end
    neutral = rd.to_NeutralNum(fmt.get_base_digits)
    neutral.rounding = fmt.get_round
  end
  
  return neutral
end

#nio_xrObject



94
95
96
# File 'lib/nio/rtnlzr.rb', line 94

def nio_xr
  return self
end