Rational, complex and mathn

e$B0[5D$,$J$1$l$P!"<c43$N$7$N8e!"<!=5Kv$K$G$be(B trunk e$B$K%3%_%C%H$7$h$&e(B
e$B$H;W$$$^$9!#e(B

e$B$*$=$i$/!"L@F|:n6H$7$^$9!#@PDM$5$s$,e(B rational.rbe$B!"e(Bcomplex.rb
mathn.rb
e$B$r8x3+$5$l$F0JMh!"e(B4121
e$BF|$V$j$NBg2~D{$K$J$k$N$G$O$J$$$+$H;W$$$^$9!#e(B

e$B$*$+$7$J$H$3$m!"<u$1F~$l$kB&$+$i8+$F5$;}$N0-$$$H$3$m$J$I$O!"$I$s$I$sD>e(B
e$B$7$F$b$i$$$?$$$H;W$$$^$9!#:G0-!"2?;~$G$be(B revert
e$B$G$-$k$N$G$d$C$F$7$^$$e(B
e$B$^$9!#e(B

e$B$=$N8e$NM=Dj$O!"e(B

  • e$B;MB’1i;;$NB.EY2~A1e(B (e$BAa$1$l$PMh=50J9_$K$be(B)

  • e$B86$5$s$,e(B GCD e$B$r2~A1$9$k$+$be(B

complex.rb
e$B$,Ds6!$9$k?t3X4X?t$NJQ99HG$K$D$$$F$N0U8+$O0z$-B3$-Jg=8Cf$G$9!#e(B

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

At Sat, 15 Mar 2008 12:52:19 +0900,
Tadayoshi F. wrote in [ruby-dev:34060]:

e$B$*$=$i$/!"L@F|:n6H$7$^$9!#@PDM$5$s$,e(B rational.rbe$B!"e(Bcomplex.rb mathn.rb
e$B$r8x3+$5$l$F0JMh!"e(B4121 e$BF|$V$j$NBg2~D{$K$J$k$N$G$O$J$$$+$H;W$$$^$9!#e(B

e$B$*$+$7$J$H$3$m!"<u$1F~$l$kB&$+$i8+$F5$;}$N0-$$$H$3$m$J$I$O!"$I$s$I$sD>e(B
e$B$7$F$b$i$$$?$$$H;W$$$^$9!#:G0-!"2?;~$G$be(B revert e$B$G$-$k$N$G$d$C$F$7$^$$e(B
e$B$^$9!#e(B

e$B?tCM%j%F%i%k$G$OO"B3$9$k%"%s%@!<%9%3%"$O5v$5$l$J$/$J$C$F$$$^$9$+e(B
e$B$i!"e(BComplexe$B$de(BRationale$B$G$bB7$($?$[$&$,$$$$$s$8$c$J$$$G$7$g$&$+!#e(B

e$B%$%s%G%s%H$,B>$H0c$&$N$,$A$g$C$H5$$K$J$k$H$$$&$N$b$"$k$1$I!#e(B

Index: complex.c

— complex.c (revision 15792)
+++ complex.c (working copy)
@@ -203,4 +203,5 @@ nucomp_s_canonicalize_internal(VALUE kla
}

+#if 0
static VALUE
nucomp_s_canonicalize(int argc, VALUE *argv, VALUE klass)
@@ -236,4 +237,5 @@ nucomp_s_canonicalize(int argc, VALUE *a
return nucomp_s_canonicalize_internal(klass, real, image);
}
+#endif

static VALUE
@@ -460,4 +462,5 @@ m_atan2_bang(VALUE y, VALUE x)
}

+#if 0
static VALUE
m_hypot(VALUE x, VALUE y)
@@ -467,4 +470,5 @@ m_hypot(VALUE x, VALUE y)
}
#endif
+#endif

static VALUE
@@ -627,4 +631,5 @@ nucomp_rdiv(VALUE self, VALUE other)
}

+#if 0
static VALUE
nucomp_fdiv(VALUE self, VALUE other)
@@ -636,4 +641,5 @@ nucomp_fdiv(VALUE self, VALUE other)
f_to_f(dat->image)), other);
}
+#endif

static VALUE
@@ -789,4 +795,5 @@ nucomp_conjugate(VALUE self)
}

+#if 0
static VALUE
nucomp_real_p(VALUE self)
@@ -813,4 +820,5 @@ nucomp_inexact_p(VALUE self)
return f_boolcast(!nucomp_exact_p(self));
}
+#endif

inline static long
@@ -979,15 +987,15 @@ nucomp_to_s(VALUE self)
else {
s = f_to_s(dat->real);

  • rb_str_concat(s, rb_str_new2(!impos ? “-” : “+”));
  • rb_str_cat2(s, (!impos ? “-” : “+”));
    }

if (k_rational_p(dat->image) &&
!f_one_p(f_denominator(dat->image))) {

  • rb_str_concat(s, rb_str_new2("("));
  • rb_str_cat2(s, “(”);
    rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
  • rb_str_concat(s, rb_str_new2(")i"));
  • rb_str_cat2(s, “)i”);
    } else {
    rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
  • rb_str_concat(s, rb_str_new2(“i”));
  • rb_str_cat2(s, “i”);
    }

@@ -1004,7 +1012,7 @@ nucomp_inspect(VALUE self)
s = rb_str_new2(“Complex(”);
rb_str_concat(s, f_inspect(dat->real));

  • rb_str_concat(s, rb_str_new2(", "));
  • rb_str_cat2(s, ", ");
    rb_str_concat(s, f_inspect(dat->image));
  • rb_str_concat(s, rb_str_new2(")"));
  • rb_str_cat2(s, “)”);

    return s;
    @@ -1110,5 +1118,5 @@ numeric_to_c(VALUE self)

static VALUE comp_pat1, comp_pat2, a_slash, a_dot_and_an_e,

  • image_garbages_pat, null_string, underscores_pat, an_underscore;
  • image_garbages_pat, null_string;

#define DIGITS “(?:\d(?:_\d|\d)*)”
@@ -1123,13 +1131,13 @@ static void
make_patterns(void)
{

  • static char *comp_pat1_source = PATTERN1;
  • static char *comp_pat2_source = PATTERN2;
  • static char *image_garbages_pat_source = “[+\(\)iIjJ]”;
  • static char *underscores_pat_source = “_+”;
  • static const char comp_pat1_source[] = PATTERN1;
  • static const char comp_pat2_source[] = PATTERN2;
  • static const char image_garbages_pat_source[] = “[+\(\)iIjJ]”;
    +#define REG_NEW(s) rb_reg_new(s, sizeof(s) - 1, 0)
  • comp_pat1 = rb_reg_new(comp_pat1_source, strlen(comp_pat1_source),
    0);
  • comp_pat1 = REG_NEW(comp_pat1_source);
    rb_global_variable(&comp_pat1);
  • comp_pat2 = rb_reg_new(comp_pat2_source, strlen(comp_pat2_source),
    0);
  • comp_pat2 = REG_NEW(comp_pat2_source);
    rb_global_variable(&comp_pat2);

@@ -1140,6 +1148,5 @@ make_patterns(void)
rb_global_variable(&a_dot_and_an_e);

  • image_garbages_pat = rb_reg_new(image_garbages_pat_source,
  •      strlen(image_garbages_pat_source), 0);
    
  • image_garbages_pat = REG_NEW(image_garbages_pat_source);
    rb_global_variable(&image_garbages_pat);

@@ -1147,10 +1154,5 @@ make_patterns(void)
rb_global_variable(&null_string);

  • underscores_pat = rb_reg_new(underscores_pat_source,
  •         strlen(underscores_pat_source), 0);
    
  • rb_global_variable(&underscores_pat);
  • an_underscore = rb_str_new2("_");
  • rb_global_variable(&an_underscore);
    +#undef REG_NEW
    }

@@ -1247,6 +1249,5 @@ static VALUE
string_to_c(VALUE self)
{

  • VALUE s = f_gsub(self, underscores_pat, an_underscore);
  • VALUE a = string_to_c_internal(s);
  • VALUE a = string_to_c_internal(self);
    if (!NIL_P(RARRAY_PTR(a)[0]))
    return RARRAY_PTR(a)[0];
    Index: rational.c
    ===================================================================
    — rational.c (revision 15792)
    +++ rational.c (working copy)
    @@ -231,4 +231,5 @@ nurat_s_canonicalize_internal(VALUE klas
    }

+#if 0
static VALUE
nurat_s_canonicalize(int argc, VALUE *argv, VALUE klass)
@@ -260,4 +261,5 @@ nurat_s_canonicalize(int argc, VALUE *ar
return nurat_s_canonicalize_internal(klass, num, den);
}
+#endif

static VALUE
@@ -585,4 +587,5 @@ nurat_divmod(VALUE self, VALUE other)
}

+#if 0
static VALUE
nurat_quot(VALUE self, VALUE other)
@@ -590,4 +593,6 @@ nurat_quot(VALUE self, VALUE other)
return f_truncate(f_div(self, other));
}
+#endif
+
static VALUE
nurat_rem(VALUE self, VALUE other)
@@ -597,4 +602,5 @@ nurat_rem(VALUE self, VALUE other)
}

+#if 0
static VALUE
nurat_quotrem(VALUE self, VALUE other)
@@ -603,4 +609,5 @@ nurat_quotrem(VALUE self, VALUE other)
return rb_assoc_new(val, f_sub(self, f_mul(other, val)));
}
+#endif

static VALUE
@@ -613,4 +620,5 @@ nurat_abs(VALUE self)
}

+#if 0
static VALUE
nurat_true(VALUE self)
@@ -618,4 +626,5 @@ nurat_true(VALUE self)
return Qtrue;
}
+#endif

static VALUE
@@ -775,5 +784,5 @@ float_to_r(VALUE self)
}

-static VALUE rat_pat, an_e_pat, a_dot_pat, underscores_pat,
an_underscore;
+static VALUE rat_pat, an_e_pat, a_dot_pat;

#define DIGITS “(?:\d(?:_\d|\d)*)”
@@ -785,24 +794,19 @@ static void
make_patterns(void)
{

  • static char *rat_pat_source = PATTERN;
  • static char *an_e_pat_source = “[eE]”;
  • static char *a_dot_pat_source = “\.”;
  • static char *underscores_pat_source = “_+”;
  • static const char rat_pat_source[] = PATTERN;
  • static const char an_e_pat_source[] = “[eE]”;
  • static const char a_dot_pat_source[] = “\.”;
    +#define REG_NEW(s) rb_reg_new(s, sizeof(s) - 1, 0)
  • rat_pat = rb_reg_new(rat_pat_source, strlen(rat_pat_source), 0);
  • rat_pat = REG_NEW(rat_pat_source);
    rb_global_variable(&rat_pat);
  • an_e_pat = rb_reg_new(an_e_pat_source, strlen(an_e_pat_source), 0);
  • an_e_pat = REG_NEW(an_e_pat_source);
    rb_global_variable(&an_e_pat);
  • a_dot_pat = rb_reg_new(a_dot_pat_source, strlen(a_dot_pat_source),
    0);
  • a_dot_pat = REG_NEW(a_dot_pat_source);
    rb_global_variable(&a_dot_pat);
  • underscores_pat = rb_reg_new(underscores_pat_source,
  •         strlen(underscores_pat_source), 0);
    
  • rb_global_variable(&underscores_pat);
  • an_underscore = rb_str_new2("_");
  • rb_global_variable(&an_underscore);
    +#undef REG_NEW
    }

@@ -909,6 +913,5 @@ static VALUE
string_to_r(VALUE self)
{

  • VALUE s = f_gsub(self, underscores_pat, an_underscore);
  • VALUE a = string_to_r_internal(s);
  • VALUE a = string_to_r_internal(self);
    if (!NIL_P(RARRAY_PTR(a)[0]))
    return RARRAY_PTR(a)[0];

e$B?tCM%j%F%i%k$G$OO"B3$9$k%"%s%@!<%9%3%"$O5v$5$l$J$/$J$C$F$$$^$9$+e(B
e$B$i!"e(BComplexe$B$de(BRationale$B$G$bB7$($?$[$&$,$$$$$s$8$c$J$$$G$7$g$&$+!#e(B

‘1__1/3’.to_r #=> Rational(11, 3)

e$B$3$N$"$?$j$G$9$+$M!#e(B

e$B$7$+$7!"e(B

‘1__1’.to_i #=> 11
‘1__1’.to_f #=> 11.0

e$B$N$h$&$K!"e(BString#to_[if] e$B$G$O!"L5;k$7$^$9$,!“e(BInteger()
e$B$de(B Float() e$B$G$Oe(B
e$B$=$&$G$O$J$$!”$N$HF1$8$K$7$?$D$b$j$G$9!#$G$9$+$i!"e(Bto_r
e$B$O5v$7$^$9$,!"e(B
Rational() e$B$G$O5v$7$^$;$s!#e(B

e$BB>$O$?$V$s$h$$$H;W$&$N$Ge(B
(e$B;n$7$F$J$$$G$9$,e(B)e$B!"D>$7$F$*$$$F$b$i$($^$9$+!#e(B

e$B%$%s%G%s%H$O!“B7$($?$[$&$,$$$$$G$9$M!#e(Bruby e$BI8=`$Ne(B emacs
e$B$Ne(B C e$B%b!<%I@_e(B
e$BDj$C$F$I$C$+$K$”$j$^$7$?$C$1!#e(B

e$BB>!">iD9$J$H$3$m$d!":Y!9$7$?$H$3$m$O!"$^$?:#EY$N5Y$_$"$?$j$K$$$m$$$m<je(B
e$B$rF~$l$h$&$H;W$$$^$9!#B.EY2~A1$bL\ES$,$?$C$?5$$,$9$k$N$G!“Aa$1$l$P:#EYe(B
e$B$N=5Kv$”$?$j$+$i$d$j$?$$!#e(B

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

In message “Re: [ruby-dev:34087] Re: rational, complex and mathn”
on Tue, 18 Mar 2008 20:29:35 +0900, Tadayoshi F.
[email protected] writes:
|
|> e$B?tCM%j%F%i%k$G$OO"B3$9$k%“%s%@!<%9%3%”$O5v$5$l$J$/$J$C$F$$$^$9$+e(B
|> e$B$i!“e(BComplexe$B$de(BRationale$B$G$bB7$($?$[$&$,$$$$$s$8$c$J$$$G$7$g$&$+!#e(B
|
|‘1__1/3’.to_r #=> Rational(11, 3)
|
|e$B$3$N$”$?$j$G$9$+$M!#e(B
|
|e$B$7$+$7!"e(B
|
|‘1__1’.to_i #=> 11
|‘1__1’.to_f #=> 11.0
|
|e$B$N$h$&$K!"e(BString#to_[if] e$B$G$O!"L5;k$7$^$9$,!“e(BInteger() e$B$de(B Float() e$B$G$Oe(B
|e$B$=$&$G$O$J$$!”$N$HF1$8$K$7$?$D$b$j$G$9!#$G$9$+$i!"e(Bto_r e$B$O5v$7$^$9$,!"e(B
|Rational() e$B$G$O5v$7$^$;$s!#e(B

e$B$o$6$o$6B7$($F$/$@$5$C$F$“$j$,$H$&$4$6$$$^$9!#$G$b!”$J$s$H$Je(B
e$B$/e(B"1__2".to_ie$B$Oe(B12e$B$G$O$J$/e(B1e$B$rJV$9e(B(e$B$D$^$j!"%j%F%i%k$HB7$($ke(B)e$BJ}e(B
e$B$,NI$$$h$&$J5$$,$7$F$-$^$7$?!#$I$&;W$$$^$9$+!)e(B

|e$B%$%s%G%s%H$O!“B7$($?$[$&$,$$$$$G$9$M!#e(Bruby e$BI8=`$Ne(B emacs e$B$Ne(B C e$B%b!<%I@_e(B
|e$BDj$C$F$I$C$+$K$”$j$^$7$?$C$1!#e(B

e$B;d$O$3$s$J$N$r;H$C$F$^$9!#e(B

(c-add-style
“ruby”
'(“bsd”
(c-basic-offset . 4)
(c-offsets-alist
(case-label . 2)
(label . 2)
(statement-case-intro . 2)
)))

e$B$3$l$K$I$&$$$&0UL#$,$“$C$?$N$+$OK:$l$A$c$$$^$7$?$,!”$H$j$"$(e(B
e$B$:F0$$$F$$$^$9!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

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

In message “Re: [ruby-dev:34090] Re: rational, complex and mathn”
on Wed, 19 Mar 2008 14:17:56 +0900, Nobuyoshi N.
[email protected] writes:

|> e$B$o$6$o$6B7$($F$/$@$5$C$F$“$j$,$H$&$4$6$$$^$9!#$G$b!”$J$s$H$Je(B
|> e$B$/e(B"1__2".to_ie$B$Oe(B12e$B$G$O$J$/e(B1e$B$rJV$9e(B(e$B$D$^$j!"%j%F%i%k$HB7$($ke(B)e$BJ}e(B
|> e$B$,NI$$$h$&$J5$$,$7$F$-$^$7$?!#$I$&;W$$$^$9$+!)e(B
|
|e$B;?@.!#e(B

e$B%3%_%C%H$7$F$/$@$5$$$^$;$s$+e(B

|> |e$B%$%s%G%s%H$O!“B7$($?$[$&$,$$$$$G$9$M!#e(Bruby e$BI8=`$Ne(B emacs e$B$Ne(B C e$B%b!<%I@_e(B
|> |e$BDj$C$F$I$C$+$K$”$j$^$7$?$C$1!#e(B
|
|misc/ruby-style.rbe$B$,$"$j$^$9!#e(B

e$B$=$s$J$b$N$,!#e(B

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

At Tue, 18 Mar 2008 23:56:36 +0900,
Yukihiro M. wrote in [ruby-dev:34089]:

|‘1__1’.to_i #=> 11
|‘1__1’.to_f #=> 11.0
|
|e$B$N$h$&$K!"e(BString#to_[if] e$B$G$O!"L5;k$7$^$9$,!“e(BInteger() e$B$de(B Float() e$B$G$Oe(B
|e$B$=$&$G$O$J$$!”$N$HF1$8$K$7$?$D$b$j$G$9!#$G$9$+$i!"e(Bto_r e$B$O5v$7$^$9$,!"e(B
|Rational() e$B$G$O5v$7$^$;$s!#e(B

e$B$o$6$o$6B7$($F$/$@$5$C$F$"$j$,$H$&$4$6$$$^$9!#$G$b!"$J$s$H$Je(B
e$B$/e(B"1__2".to_ie$B$Oe(B12e$B$G$O$J$/e(B1e$B$rJV$9e(B(e$B$D$^$j!"%j%F%i%k$HB7$($ke(B)e$BJ}e(B
e$B$,NI$$$h$&$J5$$,$7$F$-$^$7$?!#$I$&;W$$$^$9$+!)e(B

e$B;?@.!#e(B

|e$B%$%s%G%s%H$O!“B7$($?$[$&$,$$$$$G$9$M!#e(Bruby e$BI8=`$Ne(B emacs e$B$Ne(B C e$B%b!<%I@_e(B
|e$BDj$C$F$I$C$+$K$”$j$^$7$?$C$1!#e(B

misc/ruby-style.rbe$B$,$"$j$^$9!#e(B

)))

e$B$3$l$K$I$&$$$&0UL#$,$"$C$?$N$+$OK:$l$A$c$$$^$7$?$,!"$H$j$"$(e(B
e$B$:F0$$$F$$$^$9!#e(B

e$B$3$l$@$1$@$H!":G6a$N$5$5$@$5$s$N%9%?%$%ke(B(casee$B%i%Y%k$N8e$m$K%V%m%Ce(B
e$B%/$rB3$1$ke(B)e$B$G%$%s%G%s%H$,H>C<$K$J$C$F$7$^$$$^$9!#e(B

Index: bignum.c

— bignum.c (revision 15796)
+++ bignum.c (working copy)
@@ -533,8 +533,9 @@ rb_cstr_to_inum(const char *str, int bas
while ((c = *str++) != 0) {
if (c == ‘_’) {

  •  if (badcheck) {
    
  • if (nondigit) goto bad;
  • nondigit = c;
  •  if (nondigit) {
    
  • if (badcheck) goto bad;
  • break;
    }
  •  nondigit = c;
     continue;
    
    }