0**2 causes division by zero

e$B1sF#$H?=$7$^$9!#e(B

ruby 1.9 e$B$Ge(B 0**2 e$B$rI>2A$9$k$He(B SIGFPE e$B$GMn$A$^$9!#e(B

$ ./ruby -e ‘0**2’
e$BIbF0>.?tE@1i;;Nc30$G$9e(B

e$B$3$s$J46$8$G$7$g$&$+!#$D$$$G$KM>7W$J:GE,2=$bF~$l$F$_$^$7$?$,!"e(B
e$BM>7W$G$7$?$i$O$:$7$F$/$@$5$$!#e(B

Index: numeric.c

— numeric.c (revision 12589)
+++ numeric.c (working copy)
@@ -2280,6 +2280,9 @@
int neg = x < 0;
long z = 1;

  • if (x == 0) return INT2FIX(0);
  • if (y == 0) return INT2FIX(1);
  • if (y == 1) return LONG2NUM(x);
    if (neg) x = -x;
    if (y & 1) z = x;
    y &= ~1;

e$B$=$&$$$($Pe(B 0**0 e$B$Oe(B 1 e$B$G$$$$$s$G$7$g$&$+!#e(B

e$B$J$+$@$G$9!#e(B

At Sat, 23 Jun 2007 23:57:11 +0900,
Yusuke ENDOH wrote in [ruby-dev:31040]:

ruby 1.9 e$B$Ge(B 0**2 e$B$rI>2A$9$k$He(B SIGFPE e$B$GMn$A$^$9!#e(B

e$B$9$$$^$;$s!#%A%’%C%/$r8e2s$7$K$7$FK:$l$F$^$7$?!#e(B

e$B$3$s$J46$8$G$7$g$&$+!#$D$$$G$KM>7W$J:GE,2=$bF~$l$F$_$^$7$?$,!"e(B
e$BM>7W$G$7$?$i$O$:$7$F$/$@$5$$!#e(B

ye$B$N%A%’%C%/$Oe(Bfix_pow()e$B$G$d$C$F$^$9$+$i!"ITMW$@$H;W$$$^$9!#e(Bxe$B$b$=$Ce(B
e$B$A$G$7$?$[$&$,$$$$$+$b!#e(B

e$B$D$$$G$Ke(Bint_round()e$B$r8+$F$$$?$N$G$9$,!"e(Bndigits==LONG_MINe$B$N$H$-e(B
e$B$K;~4V$,$+$+$j$9$.$FJV$C$F$3$J$/$J$j$^$9$M!#%(%i!<$K$9$k$+e(B0e$B$rJVe(B
e$B$9$h$&$K$7$?$[$&$,$h$5$=$&$G$9!#e(B

e$B$=$&$$$($Pe(B 0**0 e$B$Oe(B 1 e$B$G$$$$$s$G$7$g$&$+!#e(B

e$B$^$!$$$$$s$8$c$J$$$G$7$g$&$+!#e(B

Index: numeric.c

— numeric.c (revision 12591)
+++ numeric.c (working copy)
@@ -2331,4 +2331,5 @@ fix_pow(VALUE x, VALUE y)
if (b == 1) return x;
a = FIX2LONG(x);

  • if (a == 0) return INT2FIX(0);
    if (b > 0) {
    return int_pow(a, b);
    @@ -2902,7 +2903,5 @@ int_round(int argc, VALUE* argv, VALUE n
    int ndigits;
  • if (argc == 0) return num;
  • if (FIXNUM_P(num)) return num_round(argc, argv, num);
  • if (argc == 0) return 0;
    rb_scan_args(argc, argv, “1”, &n);
    ndigits = NUM2INT(n);
    @@ -2910,6 +2909,20 @@ int_round(int argc, VALUE* argv, VALUE n
    return rb_Float(num);
    }
  • if (ndigits == 0) {
  • return num;
  • }
    ndigits = -ndigits;
  • if (ndigits < 0) {
  • rb_raise(rb_eArgError, “ndigits out of range”);
  • }
    f = int_pow(10, ndigits);
  • if (FIXNUM_P(num) && FIXNUM_P(f)) {
  • SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
  • int neg = x < 0;
  • if (neg) x = -x;
  • x = (x + y / 2) / y * y;
  • if (neg) x = -x;
  • return LONG2NUM(x);
  • }
    h = rb_funcall(f, ‘/’, 1, INT2FIX(2));
    r = rb_funcall(num, ‘%’, 1, f);

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

In message “Re: [ruby-dev:31043] Re: 0**2 causes division by zero”
on Sun, 24 Jun 2007 14:59:25 +0900, Nobuyoshi N.
[email protected] writes:

|At Sat, 23 Jun 2007 23:57:11 +0900,
|Yusuke ENDOH wrote in [ruby-dev:31040]:
|> ruby 1.9 e$B$Ge(B 02 e$B$rI>2A$9$k$He(B SIGFPE e$B$GMn$A$^$9!#e(B
|
|e$B$9$$$^$;$s!#%A%‘%C%/$r8e2s$7$K$7$FK:$l$F$^$7$?!#e(B
|
|> e$B$3$s$J46$8$G$7$g$&$+!#$D$$$G$KM>7W$J:GE,2=$bF~$l$F$_$^$7$?$,!"e(B
|> e$BM>7W$G$7$?$i$O$:$7$F$/$@$5$$!#e(B
|
|ye$B$N%A%’%C%/$Oe(Bfix_pow()e$B$G$d$C$F$^$9$+$i!"ITMW$@$H;W$$$^$9!#e(Bxe$B$b$=$Ce(B
|e$B$A$G$7$?$[$&$,$$$$$+$b!#e(B
|
|e$B$D$$$G$Ke(Bint_round()e$B$r8+$F$$$?$N$G$9$,!"e(Bndigits==LONG_MINe$B$N$H$-e(B
|e$B$K;~4V$,$+$+$j$9$.$FJV$C$F$3$J$/$J$j$^$9$M!#%(%i!<$K$9$k$+e(B0e$B$rJVe(B
|e$B$9$h$&$K$7$?$[$&$,$h$5$=$&$G$9!#e(B
|
|> # e$B$=$&$$$($Pe(B 0
0 e$B$Oe(B 1 e$B$G$$$$$s$G$7$g$&$+!#e(B
|
|e$B$^$!$$$$$s$8$c$J$$$G$7$g$&$+!#e(B

e$B$&$`!"E0Dl$7$F$^$9$M!#$3$C$A$NJ}$,$h$5$=$&$G$9$+$i%3%_%C%H$7e(B
e$B$F$/$@$5$$!#e(B