Rational


#1

e$B$U$J$P$G$9!#e(B

ruby e$B$NI8=`%i%$%V%i%je(B Rational
e$B$rJXMx$K$D$+$o$;$F$b$i$C$F$$$^$9$,!“B.e(B
e$BEYLL$G2]Bj$,$J$$$o$1$G$O$”$j$^$;$s!#$=$3$G!"86$5$s$Ne(B rational
(http://raa.ruby-lang.org/project/rational/)
e$B$r:NMQ$9$k$H$$$&$N$O$I$&e(B
e$B$G$7$g$&!#e(B

e$BI8=`%i%$%V%i%j$N$J$+$G$b!“e(BRational
e$B$O=EMW$J$b$N$G!”$h$/$D$+$o$l$k$b$Ne(B
e$B$@$H;W$$$^$9!#$b$7B.EYLL$G$N2~A1$,L50UL#$G$J$$$J$i$P!"8!F$$7$F$_$F$[$7e(B
e$B$$$H;W$$$^$9!#e(B

e$B$D$$$G$G$9$,!"$$$/$D$+5$$,$D$$$?$3$H$r=q$-$^$9!#e(B

Rational(-13, 4).to_i e$B$O!"e(B-4 e$B$K$J$j$^$9$,!"e(Bruby e$B$Ne(B
to_i e$B$Oe(B truncate e$B$Ne(B
e$B$O$:$J$N$G!"e(B-3 e$B$K$J$i$J$$$H$$$1$J$$$N$G$O$J$$$+$H;W$$$^$9!#e(B

r = Rational(-13, 4)
r.to_i => -4
r.to_f => -3.25
r.to_f.to_i => -3

e$B0J2<!“e(Bdivmod e$B$O>2$r$H$C$?$b$N$HM>$j$K$J$k!”$?$H$($Pe(B Common
Lisp e$B$G$$$(e(B
e$B$P!"e(B

(multiple-value-list (floor -13/4 1) => (-4 3/4)

e$B$G!"$J$*$+$D!"e(Ba.divmod(b) == [a.div(b), a % b]
e$B$H$$$&A0Ds$G=q$-$^$9!#e(B

e$B86$5$s$Ne(B rational e$B$O!"e(Br.divmod(1) e$B$,e(B [-3, -1/4]
e$B$K$J$j$^$9e(B (div e$B$He(B % e$B$be(B)e$B!#e(B

ruby 1.8 e$B$G$O!"e(B-3.25.div(1) e$B$,e(B -3 e$B$K$J$j$^$9!#e(B1.9
e$B$G$O!"e(B-4 e$B$K$J$j$^$9!#e(B


#2

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

In [ruby-dev:28509] the message: “[ruby-dev:28509] Rational”, on
Apr/05 23:42(JST) Tadayoshi F. writes:

e$B$U$J$P$G$9!#e(B

ruby e$B$NI8=`%i%$%V%i%je(B Rational e$B$rJXMx$K$D$+$o$;$F$b$i$C$F$$$^$9$,!“B.e(B
e$BEYLL$G2]Bj$,$J$$$o$1$G$O$”$j$^$;$s!#$=$3$G!"86$5$s$Ne(B rational
(http://raa.ruby-lang.org/project/rational/) e$B$r:NMQ$9$k$H$$$&$N$O$I$&e(B
e$B$G$7$g$&!#e(B

e$B;d<+?H$O$=$l$G$b$+$^$$$^$;$se(B.

e$BI8=`%i%$%V%i%j$N$J$+$G$b!“e(BRational e$B$O=EMW$J$b$N$G!”$h$/$D$+$o$l$k$b$Ne(B
e$B$@$H;W$$$^$9!#$b$7B.EYLL$G$N2~A1$,L50UL#$G$J$$$J$i$P!"8!F$$7$F$_$F$[$7e(B
e$B$$$H;W$$$^$9!#e(B

e$B=EMW$@$H;W$&?M$O$$$k$H;W$$$^$9$,e(B,
e$B$=$&$O;W$C$F$$$J$$?M$bB?$=$&$G$9$Me(B
(^^;

e$B$D$$$G$G$9$,!"$$$/$D$+5$$,$D$$$?$3$H$r=q$-$^$9!#e(B

Rational(-13, 4).to_i e$B$O!"e(B-4 e$B$K$J$j$^$9$,!"e(Bruby e$B$Ne(B to_i e$B$Oe(B truncate e$B$Ne(B
e$B$O$:$J$N$G!"e(B-3 e$B$K$J$i$J$$$H$$$1$J$$$N$G$O$J$$$+$H;W$$$^$9!#e(B

e$B$U$J$P$5$s$b5$$,IU$$$F$$$k$N$@$H;W$$$^$9$,e(B, ruby 1.8 e$B$He(B ruby
1.9 e$B$G$Oe(B,
rationale$B$N?6$kIq$$$b0[$J$C$F$$$^$9e(B.

ruby 1.8 e$B$G$O!"e(B-3.25.div(1) e$B$,e(B -3 e$B$K$J$j$^$9!#e(B1.9 e$B$G$O!"e(B-4 e$B$K$J$j$^$9!#e(B

e$B$?$7$+e(B, e$B0JA0$3$NJU$j$N5DO@$,$"$C$Fe(B,
to_ie$B$N?6$kIq$$$,JQ$o$C$?$N$G$O$J$$e(B
e$B$G$7$g$&$+e(B?

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: removed_email_address@domain.invalid <<—


#3

e$B$U$J$P$G$9!#e(B

e$B;d<+?H$O$=$l$G$b$+$^$$$^$;$se(B.

e$BI8=`%i%$%V%i%j$N$J$+$G$b!“e(BRational e$B$O=EMW$J$b$N$G!”$h$/$D$+$o$l$k$b$Ne(B
e$B$@$H;W$$$^$9!#$b$7B.EYLL$G$N2~A1$,L50UL#$G$J$$$J$i$P!"8!F$$7$F$_$F$[$7e(B
e$B$$$H;W$$$^$9!#e(B

e$B=EMW$@$H;W$&?M$O$$$k$H;W$$$^$9$,e(B, e$B$=$&$O;W$C$F$$$J$$?M$bB?$=$&$G$9$Me(B
(^^;

irb (rbc) e$B$He(B Rational
e$B$G$+$J$j9,$;$K$J$l$?$h$&$K;W$$$^$9!#8D?ME*$K$O$+e(B
e$B$J$j=EJu$7$F$$$^$9!#e(B

Rational(-13, 4).to_i e$B$O!"e(B-4 e$B$K$J$j$^$9$,!"e(Bruby e$B$Ne(B to_i e$B$Oe(B truncate e$B$Ne(B
e$B$O$:$J$N$G!"e(B-3 e$B$K$J$i$J$$$H$$$1$J$$$N$G$O$J$$$+$H;W$$$^$9!#e(B

e$B$U$J$P$5$s$b5$$,IU$$$F$$$k$N$@$H;W$$$^$9$,e(B, ruby 1.8 e$B$He(B ruby 1.9 e$B$G$Oe(B,
rationale$B$N?6$kIq$$$b0[$J$C$F$$$^$9e(B.

e$B$"!<!"5$$,$D$$$F$$$J$$$+$be(B (e$B:#$b$o$+$C$F$$$J$$e(B)e$B!#e(Bgcd
e$B$,B.$/$J$C$?$N$Oe(B
e$B$o$+$C$?$s$G$9$,!#e(B

ruby 1.8 e$B$G$O!"e(B-3.25.div(1) e$B$,e(B -3 e$B$K$J$j$^$9!#e(B1.9 e$B$G$O!"e(B-4 e$B$K$J$j$^$9!#e(B

e$B$?$7$+e(B, e$B0JA0$3$NJU$j$N5DO@$,$"$C$Fe(B, to_ie$B$N?6$kIq$$$,JQ$o$C$?$N$G$O$J$$e(B
e$B$G$7$g$&$+e(B?

e$B$"$-$i$+$J%P%0$N$h$&$K;W$($^$9!#e(B1.9
e$B$K9g$;$k$Y$-$H;W$&$s$G$9$,!“J|CV$5e(B
e$B$l$F$$$k$N$O!”$J$K$+M}M3$,$"$k$s$G$7$g$&$+$M!#e(B


#4

e$B86$G$9!#e(B

e$B$U$J$P$G$9!#e(B

ruby e$B$NI8=`%i%$%V%i%je(B Rational e$B$rJXMx$K$D$+$o$;$F$b$i$C$F$$$^$9$,!“B.e(B
e$BEYLL$G2]Bj$,$J$$$o$1$G$O$”$j$^$;$s!#$=$3$G!"86$5$s$Ne(B rational
(http://raa.ruby-lang.org/project/rational/) e$B$r:NMQ$9$k$H$$$&$N$O$I$&e(B
e$B$G$7$g$&!#e(B

e$B;d$b$3$Ne(Brational.ce$B$"$k$$$O$=$l$HF1EyIJ$re(B1.9e$B$K<h$j9~$`$Y$-!"$H;W$$$^$9!#e(B
e$B@PDM$5$s$,$=$N!J$I$N!)!K5$$K$J$l$P$=$&$J$k$N$G$O$J$$$G$7$g$&$+!#e(B

e$BI8=`%i%$%V%i%j$N$J$+$G$b!“e(BRational e$B$O=EMW$J$b$N$G!”$h$/$D$+$o$l$k$b$Ne(B
e$B$@$H;W$$$^$9!#$b$7B.EYLL$G$N2~A1$,L50UL#$G$J$$$J$i$P!"8!F$$7$F$_$F$[$7e(B
e$B$$$H;W$$$^$9!#e(B

e$BB.EY>e$N%a%j%C%H$OL@$i$+$@$H;W$$$^$9!#e(B


#5

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

In [ruby-dev:28513] the message: “[ruby-dev:28513] Re: Rational”, on
Apr/06 03:35(JST) Shin-ichiro HARA writes:

e$B86$G$9!#e(B

e$B$U$J$P$G$9!#e(B

e$B;d$b$3$Ne(Brational.ce$B$"$k$$$O$=$l$HF1EyIJ$re(B1.9e$B$K<h$j9~$`$Y$-!"$H;W$$$^$9!#e(B
e$B@PDM$5$s$,$=$N!J$I$N!)!K5$$K$J$l$P$=$&$J$k$N$G$O$J$$$G$7$g$&$+!#e(B

e$B;d$N5$$O$"$^$j4X78$J$$$+$be(B.
e$B$H$$$&$N$Oe(B:

e$B=EMW$@$H;W$&?M$O$$$k$H;W$$$^$9$,e(B, e$B$=$&$O;W$C$F$$$J$$?M$bB?$=$&$G$9$Me(B
(^^;

e$B$NBeI=$,>>K$5$s$@$+$i$G$9e(B(^^;
e$B2?2s$+$3$NOC$O>e$,$C$F$$$k$H;W$$$^$9$,e(B,
e$B$J$/$J$C$F$$$k$N$O$=$N$;$$N)$Ce(B
e$B$?$h$&$J5$$,e(B…

e$BI8=`%i%$%V%i%j$N$J$+$G$b!“e(BRational e$B$O=EMW$J$b$N$G!”$h$/$D$+$o$l$k$b$Ne(B
e$B$@$H;W$$$^$9!#$b$7B.EYLL$G$N2~A1$,L50UL#$G$J$$$J$i$P!"8!F$$7$F$_$F$[$7e(B
e$B$$$H;W$$$^$9!#e(B

e$BB.EY>e$N%a%j%C%H$OL@$i$+$@$H;W$$$^$9!#e(B

e$B$=$l$O$=$N$H$*$j$G$9$Me(B.

e$B$H$3$m$Ge(B, e$B8_49@-$H$$$&LL$G$O$I$&$J$s$G$7$g$&e(B?
Rational#to_i e$B$O>J$/$H$7$Fe(B,
e$BB>$OA4$/F1$8;EMM$K$J$C$F$$$k$N$G$7$g$&$+e(B?
e$B:90[$,$"$k$J$ie(B, e$B$I$&$9$Y$-$+:F8!F$$9$kI,MW$,$"$j$^$9$Me(B.

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: removed_email_address@domain.invalid <<—


#6

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

In [ruby-dev:28512] the message: “[ruby-dev:28512] Re: Rational”, on
Apr/06 01:56(JST) Tadayoshi F. writes:

e$B$U$J$P$G$9!#e(B

irb (rbc) e$B$He(B Rational e$B$G$+$J$j9,$;$K$J$l$?$h$&$K;W$$$^$9!#8D?ME*$K$O$+e(B
e$B$J$j=EJu$7$F$$$^$9!#e(B

e$B%@%V%k$G$"$j$,$H$&$4$6$$$^$9e(B.
e$B$?$7$+$K@N$Oe(B, rbce$B$G$7$?$Me(B. e$B$?$7$+$K:G=i$NL\E*$Oe(B,
e$B4X?tEEBn$G$7$?e(B(^^;

Rational(-13, 4).to_i e$B$O!"e(B-4 e$B$K$J$j$^$9$,!"e(Bruby e$B$Ne(B to_i e$B$Oe(B truncate e$B$Ne(B
e$B$O$:$J$N$G!"e(B-3 e$B$K$J$i$J$$$H$$$1$J$$$N$G$O$J$$$+$H;W$$$^$9!#e(B
e$B$U$J$P$5$s$b5$$,IU$$$F$$$k$N$@$H;W$$$^$9$,e(B, ruby 1.8 e$B$He(B ruby 1.9 e$B$G$Oe(B,
rationale$B$N?6$kIq$$$b0[$J$C$F$$$^$9e(B.

e$B$"!<!"5$$,$D$$$F$$$J$$$+$be(B (e$B:#$b$o$+$C$F$$$J$$e(B)e$B!#e(Bgcd e$B$,B.$/$J$C$?$N$Oe(B
e$B$o$+$C$?$s$G$9$,!#e(B

e$B$=$&$$$&0UL#$G$O$J$+$C$?$G$9e(B(^^;
ruby 1.9e$B$G$Oe(B,

Rational(-13,4).to_i => -3

e$B$K$J$k$he(B. e$B$H$$$&0UL#$G$7$?e(B.
e$B$"e(B, e$B$G$be(B, e$B;d$N4D6-$b$4$A$c$4$A$c$7$F$$$k$N$Ge(B,
e$B4V0c$C$F$$$k$N$+$be(B…

ruby 1.8 e$B$G$O!“e(B-3.25.div(1) e$B$,e(B -3 e$B$K$J$j$^$9!#e(B1.9 e$B$G$O!“e(B-4 e$B$K$J$j$^$9!#e(B
e$B$?$7$+e(B, e$B0JA0$3$NJU$j$N5DO@$,$”$C$Fe(B, to_ie$B$N?6$kIq$$$,JQ$o$C$?$N$G$O$J$$e(B
e$B$G$7$g$&$+e(B?
e$B$”$-$i$+$J%P%0$N$h$&$K;W$($^$9!#e(B1.9 e$B$K9g$;$k$Y$-$H;W$&$s$G$9$,!“J|CV$5e(B
e$B$l$F$$$k$N$O!”$J$K$+M}M3$,$"$k$s$G$7$g$&$+$M!#e(B

e$B$?$V$se(B, [ruby-dev: 27674]e$B$+$i$N%9%l%C%I$G$9$Me(B.
e$B$H$$$C$F$be(B, e$B$3$l7kO@=Pe(B
e$B$F$$$k$N$+$Je(B? e$B$?$V$se(B, e$B%P%0$H$$$&G’<1$G$O$J$/e(B,
e$B;EMMJQ99$H$$$&G’<1$G$O$Je(B
e$B$$$+$He(B…

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: removed_email_address@domain.invalid <<—


#7

In article E1FRDRr-0006WW-00@gentoo,
removed_email_address@domain.invalid (e$B@PDM7=<ye(B) writes:

e$B=EMW$@$H;W$&?M$O$$$k$H;W$$$^$9$,e(B, e$B$=$&$O;W$C$F$$$J$$?M$bB?$=$&$G$9$Me(B
(^^;

e$B$NBeI=$,>>K$5$s$@$+$i$G$9e(B(^^;

YAPC::Asia 2006 e$B$N$H$-$K$A$g$C$H?R$M$F$_$?$s$G$9$,!"$Y$D$KH?e(B
e$BBP$H$$$&46$8$O$7$^$;$s$G$7$?$,!#$^$D$b$H$5$s<+?H$,J]<i$7$?$/e(B
e$B$O$J$$$H$$$&$N$O$"$k$h$&$G$9$1$l$I!#e(B

e$B@PDM$5$s$,$=$N!J$I$N!)!K5$$K$J$l$P$=$&$J$k$N$G$O$J$$$G$7$g$&$+!#e(B

e$B;d$N5$$O$"$^$j4X78$J$$$+$be(B.

e$B$@$l$+B>$N?M$,$H$$$&$N$G$O$J$/!"<+J,$,$d$k$H$$$&$R$H$,8=$l$Fe(B
e$B?J$a$F$$$/$N$G$"$l$P!"E:IU$G$-$J$$M}M3$O;W$$Ev$?$j$^$;$s!#e(B

e$B$^$!!“E:IU$7$?8e$K$=$N$R$H$,$$$J$/$J$C$F$7$^$&$H$=$l$O$=$l$Ge(B
e$BIT9,$G$O$”$k$s$G$9$,!#e(B


#8

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

In message “Re: [ruby-dev:28514] Re: Rational”
on Thu, 6 Apr 2006 04:13:28 +0900, removed_email_address@domain.invalid
(e$B@PDM7=<ye(B) writes:

|e$B;d$N5$$O$"$^$j4X78$J$$$+$be(B.
|e$B$H$$$&$N$Oe(B:
|
|> e$B=EMW$@$H;W$&?M$O$$$k$H;W$$$^$9$,e(B, e$B$=$&$O;W$C$F$$$J$$?M$bB?$=$&$G$9$Me(B
|> (^^;
|
|e$B$NBeI=$,>>K$5$s$@$+$i$G$9e(B(^^;
|e$B2?2s$+$3$NOC$O>e$,$C$F$$$k$H;W$$$^$9$,e(B, e$B$J$/$J$C$F$$$k$N$O$=$N$;$$N)$Ce(B
|e$B$?$h$&$J5$$,e(B…

e$B8m2r$rN.I[$7$J$$$/$@$5$$!#e(Bw

e$B;d$OH?BP$7$?$3$H$O$J$$$H;W$$$^$9!#;d$NG’<1$G$O!“86$5$s$K!V<he(B
e$B$j9~$=Hw$,$G$-$?$iO"Mm$7$F$M!W$HEA$($F!”$=$N$^$^$K$J$C$F$$e(B
e$B$k$O$:$G$9!#e(Brationale$B$He(Bcomplexe$B$M!#e(B


#9

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

In message “Re: [ruby-dev:28520] Re: Rational”
on Thu, 6 Apr 2006 20:35:15 +0900, removed_email_address@domain.invalid
(e$B@PDM7=<ye(B) writes:

|>e$B8m2r$rN.I[$7$J$$$/$@$5$$!#e(Bw
|
|e$B$3$l$O<:Ni$7$^$7$?e(B(__;

e$B5$$K$7$F$^$;$s$+$i!#e(B

e$B$G!“F3F~$7$^$9$+!)e(B 1.8e$B$@$H7y$,$k?M$,$$$k$N$+$J$”!#$"$H!“e(B1.8
e$B$G$Ne(Bmoduloe$B$N%P%0$O@PDM$5$s$,D>$7$^$9$+!”$=$l$H$b$3$A$i$G:n6He(B
e$B$7$^$9$+!)e(B


#10

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

In [ruby-dev:28517] the message: “[ruby-dev:28517] Re: Rational”, on
Apr/06 16:10(JST) Yukihiro M. writes:

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

e$B8m2r$rN.I[$7$J$$$/$@$5$$!#e(Bw

e$B$3$l$O<:Ni$7$^$7$?e(B(__;

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: removed_email_address@domain.invalid <<—


#11

e$B$U$J$P$G$9!#e(B

ruby 1.9e$B$G$Oe(B,

Rational(-13,4).to_i => -3

e$B$K$J$k$he(B. e$B$H$$$&0UL#$G$7$?e(B.
e$B$"e(B, e$B$G$be(B, e$B;d$N4D6-$b$4$A$c$4$A$c$7$F$$$k$N$Ge(B, e$B4V0c$C$F$$$k$N$+$be(B…

e$B$&$A$@$H!"$+$o$i$J$$$h$&$J!#$G$b!"$3$C$A$b$$$m$$$m$d$C$F$k$s$G<+?.$J$$e(B
e$B$+$b!#e(B

e$B$?$V$se(B, [ruby-dev: 27674]e$B$+$i$N%9%l%C%I$G$9$Me(B. e$B$H$$$C$F$be(B, e$B$3$l7kO@=Pe(B
e$B$F$$$k$N$+$Je(B? e$B$?$V$se(B, e$B%P%0$H$$$&G’<1$G$O$J$/e(B, e$B;EMMJQ99$H$$$&G’<1$G$O$Je(B
e$B$$$+$He(B…

e$B$J$k$[$I!#e(B

At 2006-04-06T04:13:28+0900,
removed_email_address@domain.invalid (e$B@PDM7=<ye(B) wrote:

e$B$H$3$m$Ge(B, e$B8_49@-$H$$$&LL$G$O$I$&$J$s$G$7$g$&e(B?
Rational#to_i e$B$O>J$/$H$7$Fe(B, e$BB>$OA4$/F1$8;EMM$K$J$C$F$$$k$N$G$7$g$&$+e(B?
e$B:90[$,$"$k$J$ie(B, e$B$I$&$9$Y$-$+:F8!F$$9$kI,MW$,$"$j$^$9$Me(B.

e$BKM$,$_$?$H$3$m$@$H!“e(B[ruby-dev:28514] e$B$N$”$?$j$H$+e(B inspect
e$B$,0c$&$J$I$Ge(B
e$B$9$+$M!#86$5$s$N$O!"e(B

Rational.new(Rational.new(3,1),3)

e$B$J$s$F=q$$$F$bBg>fIW$G$9$M!#8=>u$G$b!"$3$C$=$jF~$lBX$($F$b!"$[$H$s$I$Ne(B
e$B?M$O5$$,$D$+$J$$$s$8$c$J$$$G$7$g$&$+!#e(B

At 2006-04-06T16:10:49+0900,
removed_email_address@domain.invalid (Yukihiro M.) wrote:

e$B;d$OH?BP$7$?$3$H$O$J$$$H;W$$$^$9!#;d$NG’<1$G$O!“86$5$s$K!V<he(B
e$B$j9~$=Hw$,$G$-$?$iO"Mm$7$F$M!W$HEA$($F!”$=$N$^$^$K$J$C$F$$e(B
e$B$k$O$:$G$9!#e(Brationale$B$He(Bcomplexe$B$M!#e(B

e$B0B?4$7$^$7$?!#K:$l$J$$$&$A$K!"$d$C$A$c$C$F$[$7$$$G$9!#e(B

e$B0B?4$7$?$s$G!"$D$$$G$K$$$&$H!“e(BFloat e$B$Ke(B Common Lisp e$B$Ne(B
rational/rationalize e$B4X?t$_$?$$$J$N$,$”$k$HJXMx$@$H;W$$$^$9!#e(Bto_r
e$B$r$Ie(B
e$B$A$i$+$NJLL>$K$9$k$H$+!#Fq$7$$$N$+$J!#e(B


#12

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

In message “Re: [ruby-dev:28522] Re: Rational”
on Fri, 7 Apr 2006 00:20:38 +0900, Tadayoshi F. removed_email_address@domain.invalid
writes:

|e$B0B?4$7$^$7$?!#K:$l$J$$$&$A$K!"$d$C$A$c$C$F$[$7$$$G$9!#e(B

e$B$^$:$Oe(B1.9e$B$K<h$j9~$s$G$$$?$@$-$?$$$G$9!#e(B1.8e$B$r$I$&$9$k$+$O$=$le(B
e$B$+$i9M$($h$&$H;W$$$^$9$,!"$A$c$s$H%F%9%H$5$(9T$($P8_49@-$NLde(B
e$BBj$b$J$5$=$&$@$7<h$j9~$s$G$b$$$$$s$8$c$J$$$G$7$g$&$+!#e(B

|e$B0B?4$7$?$s$G!"$D$$$G$K$$$&$H!“e(BFloat e$B$Ke(B Common Lisp e$B$Ne(B
|rational/rationalize e$B4X?t$_$?$$$J$N$,$”$k$HJXMx$@$H;W$$$^$9!#e(Bto_r e$B$r$Ie(B
|e$B$A$i$+$NJLL>$K$9$k$H$+!#Fq$7$$$N$+$J!#e(B

CLe$B$G$Oe(Brationale$B$He(Brationalizee$B$O0c$&$s$G$9$M!#e(BRubye$B$G$ON>J}$OMWe(B
e$B$i$J$$$H;W$&$1$I!"$I$C$A$r:NMQ$9$Y$-$J$N$+$J$"!#e(BCLtLe$B$rFI$`$+e(B
e$B$.$j$Oe(Brationalizee$B$NJ}$,K>$^$7$$$h$&$J5$$,$9$k$1$Ie(B(e$B<BAu$OBgJQe(B
e$B$J$s$G$7$g$&$+e(B)e$B!#e(B


#13

e$B$U$J$P$G$9!#e(B

e$B$^$:$Oe(B1.9e$B$K<h$j9~$s$G$$$?$@$-$?$$$G$9!#e(B1.8e$B$r$I$&$9$k$+$O$=$le(B
e$B$+$i9M$($h$&$H;W$$$^$9$,!"$A$c$s$H%F%9%H$5$(9T$($P8_49@-$NLde(B
e$BBj$b$J$5$=$&$@$7<h$j9~$s$G$b$$$$$s$8$c$J$$$G$7$g$&$+!#e(B

e$B:#5$$,$D$$$?$1$I!"$?$V$s!"e(BMarshal e$B$N8_49@-$O%@%a$G$9$M!#e(B


#14

e$B$U$J$P$G$9!#e(B

CLe$B$G$Oe(Brationale$B$He(Brationalizee$B$O0c$&$s$G$9$M!#e(BRubye$B$G$ON>J}$OMWe(B
e$B$i$J$$$H;W$&$1$I!"$I$C$A$r:NMQ$9$Y$-$J$N$+$J$"!#e(BCLtLe$B$rFI$`$+e(B
e$B$.$j$Oe(Brationalizee$B$NJ}$,K>$^$7$$$h$&$J5$$,$9$k$1$Ie(B(e$B<BAu$OBgJQe(B
e$B$J$s$G$7$g$&$+e(B)e$B!#e(B

e$BN>J}$"$l$P$&$l$7$$$G$9$,!"$H$j$"$($:!"86$5$s$N$@$H!"e(B

Rational(Math::PI).to_f => 3.14159265358979

e$B$J$s$F$G$-$k$_$?$$$J$N$G!"e(Bto_r
e$B$,$I$&?6$kIq$&$Y$-$J$N$+$o$+$j$^$;$s$,!"e(B
aFloat.to_r == Rational(aFloat) e$B$,@.$jN)$D$N$O<+A3$+$b$7$l$^$;$s!#e(B

class Float; def to_r() Rational(self) end end

e$B$G:Q$s$8$c$&$7!#e(B

e$B$3$N7k2L$Oe(B rational/rationalize
e$B$I$A$i$G$b$J$$$N$+$b$7$l$^$;$s$,!“e(BCL
e$B$K9g$;$J$1$l$P$J$i$J$$!”$H$$$$$?$$$o$1$G$O$J$$$N$G!"KM$H$7$F$O!"ITET9ge(B
e$B$,$J$/@bL@$G$-$l$P$$$$$+$J$H;W$$$^$9!#?t3X$J?M$?$A$N0U8+$r$-$-$?$$$G$9!#e(B


#15

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

In [ruby-dev:28521] the message: “[ruby-dev:28521] Re: Rational”, on
Apr/06 23:38(JST) Yukihiro M. writes: p >e$B$^$D$b$He(B
e$B$f$-$R$m$G$9e(B

e$B$G!"F3F~$7$^$9$+!)e(B

e$B$H$j$"$($:86$5$s<!Bh$G$9$h$Me(B.
e$B86$5$s$,:#8e$b0z$-B3$$$F%5%]!<%H$7$F$/$@e(B
e$B$5$k$J$i$=$l$Ge(B, OKe$B$@$H;W$$$^$9e(B.
e$B86$5$s$O:G6aK;$7$=$&$G$9$9$Me(B…
e$B$=$&$G$J$1$l$Pe(B, e$B;d$,0z$-B3$-0z$-7Q$$$G$b$h$$$G$9$,e(B…

1.8e$B$@$H7y$,$k?M$,$$$k$N$+$J$"!#e(B

e$BJLJX$G$b$"$j$^$9$,e(B, e$B$H$j$"$($:$Oe(B1.9e$B$G$9$+$Me(B.

e$B$"$H!“e(B1.8e$B$G$Ne(Bmoduloe$B$N%P%0$O@PDM$5$s$,D>$7$^$9$+!”$=$l$H$b$3$A$i$G:n6He(B
e$B$7$^$9$+!)e(B

e$B$3$N7o$Oe(B, e$B%P%0$J$N$+;EMM$J$N$+e(B… e$B$U$J$P$5$s$NJ}$G$Oe(B,
e$B$*$J$8?6$kIq$$$He(B
e$B$$$&$3$H$J$N$Ge(B, e$B%P%0$C$]$$$G$9$,e(B…
e$BL@F|e(B(e$B$$$d:#F|$+e(B)e$BD4$Y$F$_$^$9e(B.

__
---------------------------------------------------->> e$B@PDMe(B
e$B7=<ye(B <<—
---------------------------------->> e-mail: removed_email_address@domain.invalid <<—


#16

e$B86$G$9!#e(B

e$B$J$s$+;0$9$/$>uBV$@$C$?$$?$$$G$9$M!#e(B
e$BBG3+$G$-$=$&$J$N$G$U$J$P$5$s$K46<U$7$J$/$F$O!#e(B

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

e$B$H$j$"$($:86$5$s<!Bh$G$9$h$Me(B. e$B86$5$s$,:#8e$b0z$-B3$$$F%5%]!<%H$7$F$/$@e(B
e$B$5$k$J$i$=$l$Ge(B, OKe$B$@$H;W$$$^$9e(B. e$B86$5$s$O:G6aK;$7$=$&$G$9$9$Me(B…
e$B$=$&$G$J$1$l$Pe(B, e$B;d$,0z$-B3$-0z$-7Q$$$G$b$h$$$G$9$,e(B…

e$B$G$O!";d$,$d$j$^$7$g$&!#7P83$,$J$$$N$Gm4m0$7$F$$$?$N$G$9$,!“e(B
e$B$$$D$^$G$b$3$N$^$^$G$O$$$+$s$N$G!”%H%i%$$7$F$_$^$9!#e(B
e$B!t3N$+$KK!?M2=0J9_K;$7$/$J$C$F$O$$$k$N$G$9$,!#e(B

1.8e$B$@$H7y$,$k?M$,$$$k$N$+$J$"!#e(B

e$BJLJX$G$b$"$j$^$9$,e(B, e$B$H$j$"$($:$Oe(B1.9e$B$G$9$+$Me(B.

e$BE:IU%i%$%V%i%j$G$O$J$/!“AH$9~$%/%i%9$H$$$&$3$H$G$9$h$M!#e(B
1.8e$B$KF~$l$k$3$H$rL\I8$K!”$H$j$"$($:$Oe(B1.9e$B$KF~$l$^$7$g$&!#e(B

Bignum e$B$,AH$9~$$G$"$C$F$&$l$7$$$H$$$&@<$OBg$-$/$J$$$1$Ie(B
e$B3N<B$K$"$C$F!"e(BRational e$B$b$=$l$K6a$$$b$N$K$J$k$N$G$O$J$$$Ge(B
e$B$7$g$&$+!#e(B

e$B$^$:LdBjE@$r@0M}$7$^$9!#!J$D$E$/!Ke(B


#17

e$B86$G$9!#e(B

e$B$U$J$P$G$9!#e(B

rational/rationalize e$B$NOC$r$7$?$N$G!"86$5$s$Ne(B rational 1.00 e$B$Ne(B
Rational(aFloat) e$B$HHf$Y$F$_$^$7$?!#e(B

e$B$$!"$"$j$,$H$&$4$6$$$^$9!#$U$J$P$5$s$N%3!<%I$O;29M$K$5$;$Fe(B
e$B$$$?$@$-$^$9!#e(B

rationalize e$B$N$[$&$O!"%b%N$K$b$h$j$^$9$,!"e(Brational e$B$Ne(B10e$BG\A08eCY$$$$?e(B
e$B$$$G$9$M!#$J$s$+Fq$7$$$3$H$r$7$F$$$k$N$G$7$g$&$+!#$3$l$re(B to_r e$B$K$9$k$Ne(B
e$B$O>/$787$7$$$N$+$b!#$H$j$"$($:!"KM$,$
$?$+$.$j$G$O!"e(Brational e$B$N$[$&$,e(B
e$BIaDL$C$]$$$h$&$G$9!#e(B

e$B$9$$$^$;$s!"e(Brationalize e$B$H$$$&$N$rCN$i$J$$$N$G$9$,!"e(B
e$B$3$l$O2?$r5a$a$k4X?t$J$N$G$7$g$&!)e(B


#18

e$B$U$J$P$G$9!#e(B

rational/rationalize e$B$NOC$r$7$?$N$G!"86$5$s$Ne(B rational 1.00
e$B$Ne(B
Rational(aFloat) e$B$HHf$Y$F$_$^$7$?!#e(B

e$B86$5$s$Ne(B Rational 1.00 e$B$G$O$3$&$J$j$^$9$,!"e(B

Rational(0.3) => 2702159776422297/9007199254740992

e$B$$$/$D$+$Ne(B Common Lisp e$B$de(B Haskell
e$B=hM}7O$J$I$G!"IbF0>.?tE@?t$N=q<0$,F1e(B
e$B$8>l9g!“e(Brational e$B$de(B toRational
e$B$N7k2L$O$A$c$s$H0lCW$7$F$$$k$h$&$G$9!#e(B
e$B$H$$$&$+!”$=$&$8$c$J$$$b$N$O$_$D$1$i$l$^$;$s$G$7$?!#e(B

(rational 0.3f0) => 5033165/16777216
(rational 0.3d0) => 5404319552844595/18014398509481984

toRational (0.3::Float) => 5033165 % 16777216
toRational (0.3::Double) => 5404319552844595 % 18014398509481984

(inexact->exact 0.3) => 5404319552844595/18014398509481984
0.3 asFraction => (5404319552844595/18014398509481984)

e$B$J$I!#e(B

e$B$H$j$"$($:!"86$5$s$Ne(B Rational 1.00 e$B$be(B rb_rat_flo_to_rat()
e$B$r$D$.$N$h$&e(B
e$B$K$9$k$H!"F1MM$K$J$j$=$&$G$9!#e(B

static VALUE
rb_rat_flo_to_rat(klass, x)
double x;
{
double f;
int e;
f = frexp(x, &e);
#if 1 /* dbl /
f = ldexp(f, DBL_MANT_DIG);
e -= DBL_MANT_DIG;
#else /
flt */
f = (float)ldexp(f, FLT_MANT_DIG);
e -= FLT_MANT_DIG;
#endif

return RT_S_NEW2(klass, RT_MUL(rb_dbl2big(f),
		    RT_POW(rb_int2big(2), INT2FIX(e))), INT2FIX(1));

}

e$B%3!<%I>e$N?t;ze(B 2 e$B$O!"e(BFLT_RADIX e$B$K$9$Y$-$+$b!#e(B

CL e$B$N;EMM=q$K$h$l$P!VIbF0>.?tE@?t$Oe(B (e$B?t3XE*$K$Oe(B) sfb^(e-p)
e$B$N7A<0$Ne(B
e$BM-M}?t$G$"$k!W$i$7$$$N$G!#$3$l$K=>$($P!"LdBj$J$/Cf?H$,<h$j=P$;$F$$$k$Je(B
e$B$i$P!"7k2L$bDj$^$k$h$&$K;W$$$^$9!#e(B

e$B:#$N$H$3$m!"$I$&$9$Y$-$+!"$H$$$&$3$H$O$J$$$N$G$9$,!"$J$<0c$&$N$+$O$A$g$Ce(B
e$B$H5?Ld$G$9!#e(B

rationalize e$B$N$[$&$O!"%b%N$K$b$h$j$^$9$,!"e(Brational
e$B$Ne(B10e$BG\A08eCY$$$$?e(B
e$B$$$G$9$M!#$J$s$+Fq$7$$$3$H$r$7$F$$$k$N$G$7$g$&$+!#$3$l$re(B to_r
e$B$K$9$k$Ne(B
e$B$O>/$787$7$$$N$+$b!#$H$j$"$($:!"KM$,$
$?$+$.$j$G$O!"e(Brational
e$B$N$[$&$,e(B
e$BIaDL$C$]$$$h$&$G$9!#e(B


#19

In message “Re: [ruby-dev:28531] Re: Rational”
on Mon, 10 Apr 2006 01:26:10 +0900, Shin-ichiro HARA
removed_email_address@domain.invalid writes:
|
|e$B86$G$9!#e(B
|
|>e$B$U$J$P$G$9!#e(B
|>
|>rational/rationalize e$B$NOC$r$7$?$N$G!“86$5$s$Ne(B rational 1.00 e$B$Ne(B
|>Rational(aFloat) e$B$HHf$Y$F$$^$7$?!#e(B
|
|e$B$$!"$"$j$,$H$&$4$6$$$^$9!#$U$J$P$5$s$N%3!<%I$O;29M$K$5$;$Fe(B
|e$B$$$?$@$-$^$9!#e(B
|
|>rationalize e$B$N$[$&$O!"%b%N$K$b$h$j$^$9$,!"e(Brational e$B$Ne(B10e$BG\A08eCY$$$
$?e(B
|>e$B$$$G$9$M!#$J$s$+Fq$7$$$3$H$r$7$F$$$k$N$G$7$g$&$+!#$3$l$re(B to_r e$B$K$9$k$Ne(B
|>e$B$O>/$787$7$$$N$+$b!#$H$j$”$($:!"KM$,$_$?$+$.$j$G$O!"e(Brational e$B$N$[$&$,e(B
|>e$BIaDL$C$]$$$h$&$G$9!#e(B
|
|e$B$9$$$^$;$s!"e(Brationalize e$B$H$$$&$N$rCN$i$J$$$N$G$9$,!"e(B
|e$B$3$l$O2?$r5a$a$k4X?t$J$N$G$7$g$&!)e(B

CommonLispe$B$K$OIbF0>.?tE@?t$rM-M}?t$KJQ49$9$k4X?t$,Fs$D$"$C$F!"e(B
rationale$B$O9bB.$@$,IT@53Ne(B(e$B$H$$$&$+$J$s$H$$$&$+e(B)e$B$G!"e(B
rationalizee$B$ODcB.$@$,$h$jD>4QE*e(B(e$B$H$$$&$+$J$s$H$$$&$+e(B)e$B$J7k2Le(B
e$B$rJV$9$=$&$G$9!#0J2<!"e(BCLtL2e$B$+$i$N0zMQ$G$9!#e(B


rational number
rationalize number

Each of these functions converts any non-complex number to a rational
number. If the
argument is already rational, it is returned. The two functions differ
in their treatment
of floating-point numbers.

rational assumes that the floating-point number is completely accurate
and returns a
rational number mathematically equal to the precise value of the
floating-point number.

rationalize assumes that the floating-point number is accurate only to
the precision of the
floating-point representation and may return any rational number for
which the
floating-point number is the best available approximation of its format;
in doing this it
attempts to keep both numerator and denominator small.

It is always the case that

(float (rational x) x) == x

and

(float (rationalize x) x) == x

That is, rationalizing a floating-point number by either method and then
converting it back
to a floating-point number of the same format produces the original
number. What
distinguishes the two functions is that rational typically has a simple,
inexpensive
implementation, whereas rationalize goes to more trouble to produce a
result that is more
pleasant to view and simpler to compute with for some purposes.


#20

e$B$U$J$P$G$9!#e(B

e$B$9$$$^$;$s!"e(Brationalize e$B$H$$$&$N$rCN$i$J$$$N$G$9$,!"e(B
e$B$3$l$O2?$r5a$a$k4X?t$J$N$G$7$g$&!)e(B

(rational 0.3d0) => 5404319552844595/18014398509481984
(rationalize 0.3d0) => 3/10

e$B$H$$$&0c$$$G$9!#e(B

scheme e$B$Ne(B rationalize e$B$Oe(B

(rationalize (inexact->exact 0.3) 1/100) => 3/10
(rationalize (inexact->exact 0.3) 1/10) => 1/3
(rationalize (inexact->exact 0.3) 0/1) =>
5404319552844595/18014398509481984

haskell e$B$@$H$3$s$J$N$,$"$k$_$?$$$G$9!#e(B

approxRational 0.3 0.01 => 3 % 10
approxRational 0.3 0.1 => 1 % 3
approxRational 0.3 0 => 5404319552844595 % 18014398509481984