Rational, complex and mathn

rational e$B$Oe(B floore$B!"e(Btruncatee$B!"e(Bceile$B!"e(Bround
e$B$rDj5A$7$F$$$^$;$s!#e(BNumeric
e$B$N$^$^Ds6!$7$F$$$^$9!#$7$+$7!"e(BNumeric
e$B$N$b$N$O!"IbF0>.?tE@?t$KJQ49$7$Fe(B
e$B$7$^$&$?$a$^$H$b$J7k2L$,F@$i$l$k$H$O8B$j$^$;$s!#e(B[ruby-dev:32201]

e$B8=>u!"e(Bfloor e$B$N$+$o$j$Ke(B to_i
e$B$r$D$+$C$FLdBj$,$J$$$N$O!"e(Bto_i e$B$NDj5A$,e(B
floor e$B$K$J$C$F$$$k$+$i$G$9!#e(BFloat#to_i e$B$NDj5A$+$i!“e(BNumeric
e$B$Ne(B to_i e$B$Oe(B
truncate e$B$G$”$k$Y$-$@$H;W$$$^$9!#e(B

Complex(1,2).numerator e$B$,%(%i!<$K$J$j$^$9!#e(Brational
e$B$rFI$s$G$$$k$H%(%i!<e(B
e$B$K$J$j$^$;$s!#e(B

$ ruby19 -r complex -e ‘Complex(1,2).numerator’
/usr/local/ruby19/lib/ruby/1.9.0/complex.rb:345:in denominator': undefined methoddenominator’ for 1:Fixnum (NoMethodError)
from /usr/local/ruby19/lib/ruby/1.9.0/complex.rb:352:in
numerator' from -e:1:in

Complex#quo e$B$,5!G=$7$^$;$s!#IaDL$K@0?t3d$j$7$F$7$^$$$^$9!#e(B

$ ruby19 -r complex -e ‘p Complex(1,2).quo(2)’
Complex(0, 1)

$ ruby19 -r complex -r rational -e ‘p Complex(1,2).quo(2)’
Complex(0, 1)

complex
e$B$G$O!“ITMW$K;W$($k%a%=%C%I$,J|CV$7$F$”$j$^$9!#e(B>e$B!"e(B>=e$B!"e(B<e$B!"e(B<=e$B!"e(B
between?e$B!"e(Bfloore$B!"e(Bceile$B!"e(Brounde$B!"e(Btruncate
e$B$J$I!#e(B1.8 e$B$G$O!"e(Bstepe$B!“e(B<=> e$B$b!#e(B
e$B$”$H!“e(B% e$B$O0UL#$,$”$k$N$G$7$g$&$+!#e(B

mathn e$B$G!“e(BRational#inspect
e$B$r=q$-49$($F$$$k$,!”$5$9$,$KM>7W$J$*@$OC$Ne(B
e$B$h$&$J5$$b$7$^$9!#$I$&$7$F$b$7$?$1$l$P!"$=$&$$$&$3$H$O!“e(Birb
e$B$J$I%”%W%je(B
e$B%1!<%7%g%sB&$G$d$l$P$$$$$h$&$J5$$,$7$^$9!#e(B

e$B0J2<=$@50F!#e(Bfloore$B!"e(Bceile$B!"e(Btruncatee$B!"e(Bround
e$B$O86$5$s$Ne(B rational e$B$+$i%Q%/e(B
e$B$j$^$7$?!#e(B

Index: lib/rational.rb

— lib/rational.rb (revision 15409)
+++ lib/rational.rb (working copy)
@@ -238,6 +238,10 @@
end
end

  • def div(other)
  • (self / other).floor
  • end
  • Returns the remainder when this value is divided by +other+.

@@ -249,7 +253,7 @@

r % 0.26 # -> 0.19

def % (other)

  • value = (self / other).to_i
  • value = (self / other).floor
    return self - other * value
    end

@@ -261,7 +265,7 @@

r.divmod Rational(1,2) # -> [3, Rational(1,4)]

def divmod(other)

  • value = (self / other).to_i
  • value = (self / other).floor
    return value, self - other * value
    end

@@ -270,7 +274,7 @@

def abs
if @numerator > 0

  •  Rational.new!(@numerator, @denominator)
    
  •  self
    
    else
    Rational.new!(-@numerator, @denominator)
    end
    @@ -345,10 +349,37 @@

    Rational(-7,4) == -1.75 # -> true

    Rational(-7,4).to_i == (-1.75).to_i # false

  • def to_i
  • Integer(@numerator.div(@denominator))
  • def floor()

  • @numerator.div(@denominator)
    end

  • def ceil()

  • -((-@numerator).div(@denominator))

  • end

  • def truncate()

  • if @numerator < 0

  •  return -((-@numerator).div(@denominator))
    
  • end

  • @numerator.div(@denominator)

  • end

  • alias_method :to_i, :truncate

  • def round()

  • if @numerator < 0

  •  num = -@numerator
    
  •  num = num * 2 + @denominator
    
  •  den = @denominator * 2
    
  •  -(num.div(den))
    
  • else

  •  num = @numerator * 2 + @denominator
    
  •  den = @denominator * 2
    
  •  num.div(den)
    
  • end

  • end

  • Converts the rational to a Float.

@@ -476,10 +507,11 @@

class Fixnum
alias quof quo

  • undef quo
  • If Rational is defined, returns a Rational number instead of a

Fixnum.

  • remove_method :quo
  • If Rational is defined, returns a Rational number instead of a

Float.
def quo(other)

  • Rational.new!(self,1) / other
  • Rational.new!(self, 1) / other
    end
    alias rdiv quo

@@ -488,26 +520,18 @@
if other >= 0
self.power!(other)
else

  •  Rational.new!(self,1)**other
    
  •  Rational.new!(self, 1)**other
    
    end
    end
  • unless defined? 1.power!
  • alias power! **
  • alias ** rpower
  • end
    end

class Bignum

  • unless defined? Complex
  • alias power! **
  • end
  • alias quof quo
  • remove_method :quo
  • alias quof quo
  • undef quo
  • If Rational is defined, returns a Rational number instead of a

Bignum.

  • If Rational is defined, returns a Rational number instead of a

Float.
def quo(other)

  • Rational.new!(self,1) / other
  • Rational.new!(self, 1) / other
    end
    alias rdiv quo

@@ -519,8 +543,15 @@
Rational.new!(self, 1)**other
end
end
+end

  • unless defined? Complex
    +unless defined? 1.power!
  • class Fixnum
  • alias power! **
    alias ** rpower
    end
  • class Bignum
  • alias power! **
  • alias ** rpower
  • end
    end
    Index: lib/mathn.rb
    ===================================================================
    — lib/mathn.rb (revision 15409)
    +++ lib/mathn.rb (working copy)
    @@ -121,11 +121,6 @@
    class Rational
    Unify = true
  • remove_method :inspect

  • def inspect

  • format “%s/%s”, numerator.inspect, denominator.inspect

  • end

  • alias power! **

    def ** (other)
    Index: lib/complex.rb
    ===================================================================
    — lib/complex.rb (revision 15409)
    +++ lib/complex.rb (working copy)
    @@ -104,6 +104,10 @@
    @RCS_ID=’-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju
    $-’

    undef step

  • undef <, <=, <=>, >, >=

  • undef between?

  • undef divmod, modulo

  • undef floor, truncate, ceil, round

    def scalar?
    false
    @@ -199,6 +203,10 @@
    x/y
    end
    end

  • def quo(other)

  • Complex(@real.quo(1), @image.quo(1)) / other

  • end

    Raise this complex number to the given (real or complex) power.

@@ -248,6 +256,8 @@

Remainder after division by a real or complex number.

+=begin
def % (other)
if other.kind_of?(Complex)
Complex(@real % other.real, @image % other.image)
@@ -258,7 +268,8 @@
x % y
end
end

+=end
+
#–

def divmod(other)

if other.kind_of?(Complex)

@@ -312,8 +323,6 @@
end
alias conj conjugate

  • undef <=>
  • Test for numerical equality (a == a + 0i).

@@ -410,9 +419,35 @@

end

+class Integer

  • unless defined?(1.numerator) # temporal

  • def numerator() self end

  • def denominator() 1 end

  • def gcd(other)

  •  min = self.abs
    
  •  max = other.abs
    
  •  while min > 0
    
  •    tmp = min
    
  •    min = max % min
    
  •    max = tmp
    
  •  end
    
  •  max
    
  • end

  • def lcm(other)

  •  if self.zero? or other.zero?
    
  •    0
    
  •  else
    
  •    (self.div(self.gcd(other)) * other).abs
    
  •  end
    
  • end

  • end

+end
+
module Math
alias sqrt! sqrt
alias exp! exp

e$BB>$K$bLdBj!“2]Bj$O$”$k$H;W$$$^$9!#$9$0$K2r7h$G$-$k$b$N$H!"$=$&$G$J$$$be(B
e$B$N$H$"$j$^$9!#e(B

e$B$=$&$$$&$3$H$r$9$k?M$O$$$J$$$+$b$7$l$^$;$s$,!"@PDM$5$s$Ne(B rational
e$B$O!“e(B
e$B%5%V%/%i%9$r:n$C$?$H$-$K!”$&$^$/5!G=$7$J$$$H$3$m$,$"$k$H;W$$$^$9!#7k6I!"e(B
Rational()
e$B$rDL$5$J$$$H%$%s%9%?%s%9$r$D$/$l$J$$$N$G!"9=B$E*$K2]Bj$,$"e(B
e$B$k$H$$$($k$N$+$b$7$l$^$;$s!#e(Bcomplex e$B$bF1MM$G$9$,!“e(Brational
e$B$K$Oe(B new e$B$,e(B
e$B$J$$$N$K!“e(BComplex e$B$N$[$&$K$O!”$J$<$+e(B new e$B$,$”$j$^$9!#e(B

e$B$b$&$:$$$V$sA0$K;XE&$5$l$F$$$k$3$H$N$h$&$G$9$,!"e(BComplex.generic?
e$B$O$“e(B
e$B$^$j$h$/$J$$$+$b$7$l$^$;$s!#$3$l$,e(B generic
e$B$@$H$$$&0lHLE*$JDj5A$r;}$Ce(B
e$B$F$$$J$$$N$G!”$G$-$l$P%*%V%8%’%/%H$KBP$7$F!"Ld9g$;$k$h$&$J;EAH$,I,MW$+e(B
e$B$b$7$l$^$;$s!#<B:]$N$H$3$me(B Complex e$B0J30$Ne(B Numeric
e$B$O$=$&$@$H9b$r$/$/$Ce(B
e$B$F$b$$$$$h$&$K;W$$$^$9$,!#e(B

Complex(1,2) ** -2 #=> Complex(Rational(-3, 25), Rational(-4, 25))

e$B$G$9$,!"e(BRational e$B$r$D$+$&$HIbF0>.?tE@?t$K$J$C$F$7$^$$$^$9!#e(B

Complex(1,2) ** Rational(-2) #=> Complex(-0.12, -0.16)

e$B$3$l$O;EJ}$,$J$$$3$H$HD|$a$k$Y$-$G$7$g$&$+!#e(B

{Float,String,NilClass}#to_re$B!“e(B{Float,Intetger,String,NilClass}#to_c
e$B$Je(B
e$B$I$O$”$C$F$b$$$$$H;W$$$^$9!#e(BNumeric e$B$Ke(B
quotiente$B!“e(Bquotrem e$B$,$”$C$F$b$$e(B
e$B$$$+$b$7$l$^$;$s!#e(B

1.9 e$B$NI>2A4o$NFC@-$@$H;W$$$^$9$,!“e(BaInteger ** aRational
e$B$,%(%i!<$K$J$je(B
1.e$B$^$9!#e(B8 e$B$G$OLdBj$”$j$^$;$s!#e(B

$ ruby19 -r rational -e ‘1 ** Rational(1)’
/usr/local/ruby19/lib/ruby/1.9.0/rational.rb:489:in power!': super: no superclass methodpower!’ for Rational(1, 1):Rational (NoMethodError)
from /usr/local/ruby19/lib/ruby/1.9.0/rational.rb:489:in
rpower' from -e:1:in

e$B$R$H$D=q$-K:$l$^$7$?!#e(B

rational e$B$OCY$$$G$9!#86$5$s$Ne(B rational
e$B$r$D$+$&$HB.$/$J$j$^$9!#e(B

e$B$=$l$H!“e(BRational() e$B$,0UL#$9$k$N$O!”%3%s%9%H%i%/%?$r;}$?$J$$e(B
Integer e$B$de(B
Float e$B$K$$1$ke(B Integer() e$B$de(B Float()
e$B$HF1$8$/JQ494o$H$7$F$"$k$H$$$&$3$He(B
e$B$G$9!#$7$+$7$J$,$i!"e(BRational() e$B$Oe(B Integer() e$B$de(B Float()
e$B$H0c$$!“J8;zNse(B
e$B$dIbF0>.?tE@?t$r<u$1F~$l$^$;$s!#e(BRational
e$B$r:FEjF~$9$k>l9g$b@)8B$,$”$j!"e(B
e$BHs>o$K8BDjE
$J<BAu$K$J$C$F$$$^$9e(B
(e$B$"$"!"$R$H$D$8$c$J$+$C$?e(B)e$B!#e(B

Numeric e$B$K$"$ke(B floore$B!"e(Bceile$B!“e(Btruncatee$B!“e(Bround
e$B$O!”$”$^$j0UL#$,$J$$$P$+$je(B
e$B$G$J$/!“M-32$@$H;W$&$N$G:o=|$7$F$$$$$H;W$C$F$$$^$9$,!”$A$g$C$HJL$JDj5Ae(B
e$B$r9M$($F$_$^$7$?!#e(B

ruby e$B$G$D$+$o$l$ke(B Complex
e$B$r=|$/?t!"e(BFixnume$B!"e(BBignume$B!"e(BFloate$B!“e(BRationale$B!“e(B
BigDecimal e$B$O!”$=$l$,I=$7$F$$$k?t$,@5$7$$$H$7$F!”@53N$Ke(B Rational
e$B$KJQe(B
e$B492DG=$J$O$:$G$9!#e(B

e$B$h$j0lHLE*$JM-M}?t$H$9$k$3$H$G0lHL2=$9$k$3$H$,$G$-$^$9!#0lJ}!“8=:_$N<Be(B
e$BAu$O!“IbF0>.?tE@?t$K4T85$7$F$7$^$$$^$9$,!”$3$l$O$”$-$i$+$KLdBj$,$"$k$He(B
e$B;W$$$^$9!#e(B

e$B$"$k$$$O!“e(BRational e$B$NB8:_e(B (= to_r e$B$NB8:e(B)
e$B$rA0Ds$K$;$:$K!"$=$l$>$l$N%/e(B
e$B%i%9$G!"e(Bnumerator e$B$He(B denominator
e$B$rDj5A$9$k$3$H$K$7$^$9!#e(BRational e$B$,B8e(B
e$B:
$7$J$$>l9g$b$”$k$7!“5U$Ke(B mathn e$B4D6-$G$O!“e(BInteger
e$B$+$ie(B Rational e$B$KJQe(B
e$B49$7$?$j$9$k0UL#$,$J$$!”$H$$$&$3$H$b$”$j$^$9!#e(B

e$B8=<BE*$K$O!"$"$^$j0UL#$,$J$$$+$b$7$l$J$$$1$I!"$3$N$h$&$K$9$k$3$H$b2DG=e(B
e$B$G$O$J$$$+$H;W$$$^$7$?!#e(B

require ‘rational’
require ‘complex’
require ‘bigdecimal’

class Numeric

def numerator() to_r.numerator end
def denominator() to_r.denominator end

def floor
numerator.div(denominator)
end

def ceil
-((-numerator).div(denominator))
end

def truncate
if numerator < 0
return -((-numerator).div(denominator))
end
numerator.div(denominator)
end

alias_method :to_i, :truncate

def round
if numerator < 0
num = -numerator
num = num * 2 + denominator
den = denominator * 2
-(num.div(den))
else
num = numerator * 2 + denominator
den = denominator * 2
num.div(den)
end
end

end

class Float

def decode
f, n = Math.frexp(self)
f = Math.ldexp(f, Float::MANT_DIG).to_i
n -= Float::MANT_DIG
return f, n
end

private :decode

def to_r
f, n = decode
f * Float::RADIX ** n
end

end

class BigDecimal

def to_r
s, f, b, e = split
Rational(s * f.to_i * b ** (e - f.size))
end

end

class Complex

undef numerator
undef denominator
undef floor
undef ceil
undef truncate
undef round

end

e$B%a%=%C%I$KEO$5$l$??tCM$r@0?t2=$7$F;H$$$?$$>l9g$r9M$($k$H!$e(B
e$B$J$/$J$k$HITJX$G$O$J$$$G$7$g$&$+!)e(B

Fixnum
e$B$J$I$O!"$=$l$>$l<+A0$GDj5A$r;}$C$F$$$k$N$G!"$=$&$$$&$3$H$O$J$$e(B
e$B$H;W$$$^$9!#e(BFixnum e$B$Oe(B self e$B$rJV$9$@$1$G$9$M!#e(B

e$B$"$^$j$A$c$s$H%a%s%F$5$l$F$$$J$$e(B Rational e$B$de(B Complex
e$B$K7Q>5$5$l$k$o$1e(B
e$B$G$9$,!"e(BRational e$B$K$OITE,@Z$JDj5A$G$9!#$^$?!"e(BComplex
e$B$K$O0UL#$,$J$$$Ge(B
e$B$9!#<B:]$N$H$3$m!"e(BFloat e$B0J30$K$O0UL#$N$J$$Dj5A$@$H;W$$$^$9!#e(B

$ ruby19 -r complex -e ‘Complex(1).floor’
-e:1:in floor': can't convert Complex into Float (TypeError) from -e:1:in

Numeric e$B$r7Q>5$9$k%/%i%9$G!"$b$7!“K\Ev$K$3$NDj5Ae(B
(e$B0lC6IbF0>.?tE@?t$KJQe(B
e$B49$7$F$+$i@0?t$K$9$ke(B)
e$B$,$[$7$$$N$G$”$l$P!"$=$N%/%i%9$N<BAu$K$*$$$F!"e(B
Float#{floor,ceil,truncate,round} e$B$r8F$Y$P$h$$$H;W$$$^$9!#e(B

Numeric e$B$K$"$k$J$i!$$o$?$5$l$??tCM$,e(B Float e$B$@$m$&$,e(B Fixnum e$B$@$m$&$,e(B
arg.round e$B$H$+$r0lH/8F$V$@$1$G:Q$^$;$i$l$^$9$,!$e(B
Float e$B$K$O$"$k$,e(B Fixnum e$B$K$O$J$$$H$+$N>u67$N>l9g$Oe(B
e$BEO$5$l$??tCM$N7?$r%A%’%C%/$7$?$jNc30=hM}$GBP1~$7$F$d$C$?$j$9$kI,MW$,e(B
e$B@8$8$k$H;W$$$^$9!%e(B

Numeric e$B$r7Q>5$9$kKX$I$N%/%i%9$G!"e(Bto_i e$B$de(B to_f
e$B$,$D$+$($^$9$,!"e(BNumeric
e$B$GDj5A$5$l$F$$$k$o$1$G$O$J$$$h$&$G$9!#$J$N$G!"Bg>fIW$8$c$J$$$+$H;W$$$^e(B
e$B$9!#e(B

e$B$=$l$+$i!“e(Bdiv e$B$b%(%i!<$K$J$j$^$9!#L5MQ$J$i$3$l$be(B undef
e$B$9$k$J$j!”$J$se(B
e$B$J$j$9$Y$-$G$9!#e(B

$ ruby19 -r complex -e ‘Complex(1).div(1)’
-e:1:in div': can't convert Complex into Float (TypeError) from -e:1:in

e$B1J0f!wCNG=!%6e9)Bg$G$9!%e(B

From: Tadayoshi F. [email protected]
Subject: [ruby-dev:33668] Re: rational, complex and mathn
Date: Sat, 9 Feb 2008 15:41:54 +0900
Message-ID: [email protected]

Numeric e$B$K$"$ke(B floore$B!"e(Bceile$B!“e(Btruncatee$B!“e(Bround e$B$O!”$”$^$j0UL#$,$J$$$P$+$je(B
e$B$G$J$/!“M-32$@$H;W$&$N$G:o=|$7$F$$$$$H;W$C$F$$$^$9$,!”$A$g$C$HJL$JDj5Ae(B
e$B$r9M$($F$_$^$7$?!#e(B

e$B%a%=%C%I$KEO$5$l$??tCM$r@0?t2=$7$F;H$$$?$$>l9g$r9M$($k$H!$e(B
e$B$J$/$J$k$HITJX$G$O$J$$$G$7$g$&$+!)e(B

Numeric e$B$K$“$k$J$i!$$o$?$5$l$??tCM$,e(B Float e$B$@$m$&$,e(B Fixnum
e$B$@$m$&$,e(B
arg.round e$B$H$+$r0lH/8F$V$@$1$G:Q$^$;$i$l$^$9$,!$e(B
Float e$B$K$O$”$k$,e(B Fixnum e$B$K$O$J$$$H$+$N>u67$N>l9g$Oe(B
e$BEO$5$l$??tCM$N7?$r%A%'%C%/$7$?$jNc30=hM}$GBP1~$7$F$d$C$?$j$9$kI,MW$,e(B
e$B@8$8$k$H;W$$$^$9!%e(B

e$B2?$+4*0c$$$r$7$F$$$k$G$7$g$&$+!)e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:33664] Re: rational, complex and mathn”
on Sat, 9 Feb 2008 08:43:16 +0900, Tadayoshi F. [email protected]
writes:

|rational e$B$OCY$$$G$9!#86$5$s$Ne(B rational e$B$r$D$+$&$HB.$/$J$j$^$9!#e(B

e$B86$5$s$Ne(Brationale$B$OF3F~M=Dj$,$“$j$^$9$N$G!”$3$N5!2q$K$b$&0lEYe(B
e$BH`$r$D$D$$$F$_$F$/$@$5$$!#e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:33662] rational, complex and mathn”
on Sat, 9 Feb 2008 08:06:02 +0900, Tadayoshi F. [email protected]
writes:
|
|rational e$B$Oe(B floore$B!"e(Btruncatee$B!"e(Bceile$B!"e(Bround e$B$rDj5A$7$F$$$^$;$s!#e(BNumeric
|e$B$N$^$^Ds6!$7$F$$$^$9!#$7$+$7!"e(BNumeric e$B$N$b$N$O!"IbF0>.?tE@?t$KJQ49$7$Fe(B
|e$B$7$^$&$?$a$^$H$b$J7k2L$,F@$i$l$k$H$O8B$j$^$;$s!#e(B[ruby-dev:32201]
|
|e$B8=>u!"e(Bfloor e$B$N$+$o$j$Ke(B to_i e$B$r$D$+$C$FLdBj$,$J$$$N$O!"e(Bto_i e$B$NDj5A$,e(B
|floor e$B$K$J$C$F$$$k$+$i$G$9!#e(BFloat#to_i e$B$NDj5A$+$i!“e(BNumeric e$B$Ne(B to_i e$B$Oe(B
|truncate e$B$G$”$k$Y$-$@$H;W$$$^$9!#e(B
|
|Complex(1,2).numerator e$B$,%(%i!<$K$J$j$^$9!#e(Brational e$B$rFI$s$G$$$k$H%(%i!<e(B
|e$B$K$J$j$^$;$s!#e(B
|
|$ ruby19 -r complex -e ‘Complex(1,2).numerator’
|/usr/local/ruby19/lib/ruby/1.9.0/complex.rb:345:in denominator': undefined method denominator’ for 1:Fixnum (NoMethodError)
| from /usr/local/ruby19/lib/ruby/1.9.0/complex.rb:352:in numerator' | from -e:1:in
|
|Complex#quo e$B$,5!G=$7$^$;$s!#IaDL$K@0?t3d$j$7$F$7$^$$$^$9!#e(B
|
|$ ruby19 -r complex -e ‘p Complex(1,2).quo(2)’
|Complex(0, 1)
|
|$ ruby19 -r complex -r rational -e ‘p Complex(1,2).quo(2)’
|Complex(0, 1)
|
|complex e$B$G$O!“ITMW$K;W$($k%a%=%C%I$,J|CV$7$F$”$j$^$9!#e(B>e$B!"e(B>=e$B!"e(B<e$B!"e(B<=e$B!"e(B
|between?e$B!"e(Bfloore$B!"e(Bceile$B!"e(Brounde$B!"e(Btruncate e$B$J$I!#e(B1.8 e$B$G$O!“e(Bstepe$B!“e(B<=> e$B$b!#e(B
|e$B$”$H!“e(B% e$B$O0UL#$,$”$k$N$G$7$g$&$+!#e(B
|
|mathn e$B$G!“e(BRational#inspect e$B$r=q$-49$($F$$$k$,!”$5$9$,$KM>7W$J$*@$OC$Ne(B
|e$B$h$&$J5$$b$7$^$9!#$I$&$7$F$b$7$?$1$l$P!”$=$&$$$&$3$H$O!“e(Birb e$B$J$I%”%W%je(B
|e$B%1!<%7%g%sB&$G$d$l$P$$$$$h$&$J5$$,$7$^$9!#e(B
|
|e$B0J2<=$@50F!#e(Bfloore$B!"e(Bceile$B!"e(Btruncatee$B!"e(Bround e$B$O86$5$s$Ne(B rational e$B$+$i%Q%/e(B
|e$B$j$^$7$?!#e(B

e$B$I$&$b%a%s%F%J!<$NH?1~$,0-$$$N$G!“e(Btrunke$B$K%3%_%C%H$7$F$/$@$5$$!#e(B
e$BITK~$,$”$l$P@PDM$5$s<+?H$Ke(Breverte$B$7$F$b$i$$$^$7$g$&!#e(B

e$B86$5$s$Ne(Brationale$B$OF3F~M=Dj$,$"$j$^$9$N$G!"$3$N5!2q$K$b$&0lEYe(B
e$BH`$r$D$D$$$F$_$F$/$@$5$$!#e(B

e$B$H$$$&;v$J$N$G!"6qBNE*$JOC$r$7$^$7$g$&e(B > e$B86$5$se(B

e$B$1$$$8$e!w$$$7$D$+$G$9e(B.

In [ruby-dev :33706 ] the message: "[ruby-dev:33706] Re: rational,
complex and mathn ", on Feb/12 11:04(JST) Yukihiro M. writes:

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

e$B$I$&$b%a%s%F%J!<$NH?1~$,0-$$$N$G!“e(Btrunke$B$K%3%_%C%H$7$F$/$@$5$$!#e(B
e$BITK~$,$”$l$P@PDM$5$s<+?H$Ke(Breverte$B$7$F$b$i$$$^$7$g$&!#e(B

e$B?=$7Lu$J$$e(B.

e$B%3%_%C%H$7$F$/$@$5$C$F$/$@$5$C$F7k9=$G$9e(B.

e$B$?$@e(B,

|mathn e$B$G!“e(BRational#inspect e$B$r=q$-49$($F$$$k$,!”$5$9$,$KM>7W$J$*@$OC$Ne(B
|e$B$h$&$J5$$b$7$^$9!#$I$&$7$F$b$7$?$1$l$P!"$=$&$$$&$3$H$O!“e(Birb e$B$J$I%”%W%je(B
|e$B%1!<%7%g%sB&$G$d$l$P$$$$$h$&$J5$$,$7$^$9!#e(B

e$B$N$H$3$m$O;D$7$F$*$$$F$$$?$@$1$k$H$"$j$,$?$$$J$!e(B.

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

e$B@PDM$5$s$,=P$F$3$i$l$F$h$+$C$?!#e(B

|mathn e$B$G!“e(BRational#inspect e$B$r=q$-49$($F$$$k$,!”$5$9$,$KM>7W$J$*@$OC$Ne(B
|e$B$h$&$J5$$b$7$^$9!#$I$&$7$F$b$7$?$1$l$P!"$=$&$$$&$3$H$O!“e(Birb e$B$J$I%”%W%je(B
|e$B%1!<%7%g%sB&$G$d$l$P$$$$$h$&$J5$$,$7$^$9!#e(B

e$B$N$H$3$m$O;D$7$F$*$$$F$$$?$@$1$k$H$"$j$,$?$$$J$!e(B.

e$B$=$3$r5$$K$5$l$k$H$OM=A[$7$F$$$^$;$s$G$7$?!#$G$-$l$PM}M3$rCN$j$?$$$N$Ge(B
e$B$9$,!#e(B

e$B$1$$$8$e!w$$$7$D$+$G$9e(B.

In [ruby-dev :33717 ] the message: "[ruby-dev:33717] Re: rational,
complex and mathn ", on Feb/12 21:22(JST) Tadayoshi F. writes:

e$B@PDM$5$s$,=P$F$3$i$l$F$h$+$C$?!#e(B

e$B?=$7Lu$J$$e(B. e$B:G6a%P%?%P%?$7$F$$$?$b$N$Ge(B…
e$B$^$@JV;v$7$F$$$J$$$N$bL@F|$K$OJV;v$G$-$k$h$&EXNO$7$^$9e(B.

|mathn e$B$G!“e(BRational#inspect e$B$r=q$-49$($F$$$k$,!”$5$9$,$KM>7W$J$@$OC$Ne(B
|e$B$h$&$J5$$b$7$^$9!#$I$&$7$F$b$7$?$1$l$P!"$=$&$$$&$3$H$O!“e(Birb e$B$J$I%”%W%je(B
|e$B%1!<%7%g%sB&$G$d$l$P$$$$$h$&$J5$$,$7$^$9!#e(B
e$B$N$H$3$m$O;D$7$F$
$$$F$$$?$@$1$k$H$"$j$,$?$$$J$!e(B.

e$B$=$3$r5$$K$5$l$k$H$OM=A[$7$F$$$^$;$s$G$7$?!#$G$-$l$PM}M3$rCN$j$?$$$N$Ge(B
e$B$9$,!#e(B

e$B$He(B. e$B;W$C$?$N$G$9$,e(B,
e$B$3$3$O;d$N%*%j%8%J%k$G$O$J$$$G$9$Me(B.
rivision 1363 e$B$G>>K$5$s$,DI2C$5$l$F$$$^$9$Me(B.
e$B>>K$5$s$NDI2C$J$N$Ge(B, e$B$@e(B
e$B$l$+$+$i$N%j%/%(%9%H$rH?1G$7$?$N$@$H;W$$$^$9$,e(B…

e$B;d$NJ}?K$O$^$5$K2<5-$NDL$j$Ge(B:

|mathn e$B$G!“e(BRational#inspect e$B$r=q$-49$($F$$$k$,!”$5$9$,$KM>7W$J$*@$OC$Ne(B
|e$B$h$&$J5$$b$7$^$9!#$I$&$7$F$b$7$?$1$l$P!"$=$&$$$&$3$H$O!“e(Birb e$B$J$I%”%W%je(B
|e$B%1!<%7%g%sB&$G$d$l$P$$$$$h$&$J5$$,$7$^$9!#e(B

irb -m e$B$GBP1~$9$k$h$&$K$7$F$$$^$9e(B.

e$B$H$$$&$3$H$Ge(B, e$B:o$C$F$+$^$$$^$;$se(B. e$B$H$$$&$+e(B,
e$B$9$G$K:o$C$F$"$j$^$9$Me(B.

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

e$B$=$3$r5$$K$5$l$k$H$OM=A[$7$F$$$^$;$s$G$7$?!#$G$-$l$PM}M3$rCN$j$?$$$N$Ge(B
e$B$9$,!#e(B

e$B$He(B. e$B;W$C$?$N$G$9$,e(B, e$B$3$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#e(B

e$BL@F|$"$?$j0l6h@Z$j$D$1$F!"$"$?$i$7$$$N$r=P$9$D$b$j$G$9!#e(B

e$BHf3SEy$N$?$a$K!“86$5$s$Ne(B rational
e$B$bIiC4$K$J$i$J$$DxEY$K%a%s%F$7$?$[$&e(B
e$B$,$$$$$H;W$$$^$9$N$G!”:#8=:_!"$"$-$i$+$K%P%0$C$]$$$H$3$m$r$CN$i$;$7$Fe(B
e$B$
$-$^$9!#e(B

Rational(-1,3) ** -1 #=> Rational(3, -1)
Rational(-1,3) ** -3 #=> Rational(27, -1)
Rational(1).integer? #=> true

e$BHyL/$J$b$Ne(B:

Rational.new!(1, -2) #=> Rational(1, -2) # e$B%*%j%8%J%k$Oe(B
Rational(-1,2) e$B$K$J$k!#e(B

e$B$?$V$s!"e(BRational(-1,3) e$B$,e(B Rational(3,-1)
e$B$K$J$k860x$+$J$H;W$&$N$G$9$,!#e(B

e$B$1$$$8$e!w$$$7$D$+$G$9e(B.

In [ruby-dev :33797 ] the message: "[ruby-dev:33797] Re: rational,
complex and mathn ", on Feb/15 22:46(JST) Tadayoshi F. writes:

e$BAGD>$KD>$=$&$H$9$k$H!"7k6Ie(B Float#to_r e$B$rDj5A$9$k$3$H$K$J$k$s$8$c$J$$$+e(B
e$B$H;W$$$^$9!#$7$+$7!"J#AG?t$Ne(B numeratore$B!"e(Bdenominator e$B$r<h$k$H$$$&$N$,$he(B
e$B$/$o$+$i$J$/$F!"e(BCommon Lisp e$B$r;O$a!“IaDL$OJ#AG?t$KBP$7$F$O5!G=$7$J$$$Ge(B
e$B$9$7!”@PDM$5$s$K!"0U?^$rJ9$$$?$[$&$,$$$$$G$9$M!#e(B

e$B$?$7$+$Ke(B, numeratore$B!"e(Bdenominator
e$BC1FH$@$H0UL#$,$J$$$+$be(B…

e$BJ#AG?te(B: z = (a/b)+(c/d)i
e$B$re(B z.numerator/z.denominator

e$B$H$$$&J,Jl$r@0?t$G$=$m$($?7A<0$KJQ49$7$?$$$H$-$K;H$&$b$N$@$H;W$$$^e(B
e$B$9e(B.
e$BCf3X$H$+9b9;$N;~$K$O$=$&$d$C$FJ,Jl$r$=$m$($F$$$^$7$?$h$Me(B(^^;
e$B7W;;e(B
e$BCf$K$O;H$&$3$H$O$J$$$H;W$$$^$9$,e(B,e$BI=<($9$k$H$-$K$O;H$&$+$be(B.

e$B$J$/$7$F$bNI$$$G$9$,e(B,
2e$BCM$r%Z%"$GJV$9%a%=%C%I$KJQ$($?J}$,NI$$5$$,$7$Fe(B
e$B$-$^$7$?e(B. Complex#normal_rational_form e$B$H$+e(B.

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—