大きな数の大まかな割り算

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

[ruby-core:11069]e$B$GJs9p$,$"$C$?$N$G$9$,!e(Brationale$B$NJ,;RJ,Jle(B
e$B$,$$^$j$KBg$-$$$He(BFloate$B$KJQ49$G$-$:!"K\MhM-8B$NCM$J$N$K7k2Le(B
e$B$,e(BNaNe$B$K$J$C$F$7$^$&$h$&$G$9!#e(B

e$BNce(B

require mathn
(((522 / 100 / 365) + 1) ** 365).to_f

e$B$G!":,K\E*$J860x$Oe(BRational#to_fe$B$N<BAu$,e(B

def to_f
@numerator.to_f/@denominator.to_f
end

e$B$H$$$&%7%s%W%k$J$b$N$G$"$k$;$$$J$N$G$9$,!e(BFloat(double)e$B$NHOe(B
e$B0OFb$G<}$^$k$h$&$J3d$j;;$r9T$&%%k%4%j%:%`$r$4B8$8$N?M$O$$$^e(B
e$B$;$s$+!)e(B

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

In [ruby-dev:30743] the message: [ruby-dev:30743]
e$BBg$-$J?t$NBg$^$+$Je(B
e$B3d$j;;e(B, on May/02 08:56(JST) Yukihiro M. writes:

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

[ruby-core:11069]e$B$GJs9p$,$"$C$?$N$G$9$,!e(Brationale$B$NJ,;RJ,Jle(B
e$B$,$$^$j$KBg$-$$$He(BFloate$B$KJQ49$G$-$:!"K\MhM-8B$NCM$J$N$K7k2Le(B
e$B$,e(BNaNe$B$K$J$C$F$7$^$&$h$&$G$9!#e(B

e$B$H$$$&%7%s%W%k$J$b$N$G$"$k$;$$$J$N$G$9$,!e(BFloat(double)e$B$NHOe(B
e$B0OFb$G<}$^$k$h$&$J3d$j;;$r9T$&%%k%4%j%:%`$r$4B8$8$N?M$O$$$^e(B
e$B$;$s$+!)e(B

e$B;d$NJ}$NHG$G$Oe(B,

class Rational
def to_f
m = Float.prec_size
e = m - abs.log2floor
Math.ldexp((self * 2**e).floor.to_f,-e)
end
end

class Integer
def log2floor
raise RangeError, not defined for self <= 0 if self <= 0
return 0 if self==1
return self.div(2).log2floor + 1 if size==4
self.div(2**(8*(size-4))).log2floor + 8*(size-4)
end
end

e$B$H$J$C$F$$$^$7$?e(B.

[email protected]$He(B,
e$B7k2L$N;X?tIt$,e(BFloate$B$NHO0O$KF~$C$F$$$l$PF0:n$7$^$9e(B.

[email protected](B, Fload.prec_size e$B$,$I$3$K$"$k$N$+$o$+$i$J$$e(B

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

[email protected]$G$9!#e(B

At Wed, 2 May 2007 11:11:47 +0900,
[email protected]=<ye(B wrote in [ruby-dev:30746]:

class Rational
def to_f
m = Float.prec_size
e = m - abs.log2floor
Math.ldexp((self * 2**e).floor.to_f,-e)
end
end

prec_sizee$B$Oe(BFloat::MANT_DIGe$B$G$$$$$N$G$O$J$$$+$H;W$$$^$9$,!"e(B

irb(main):017:0> (((522 / 100 / 365) + 1) ** 365).to_f
NoMethodError: undefined method log2floor' for #<Rational:0xb78070c0> from (irb):4:into_f
from (irb):17

e$B$H%(%i!<$K$J$j$^$7$?!#e(B

e$B$=$l$H!"e(BRational#floore$B$O$J$$$_$?$$$J$N$G!"e(B(self *
2**e).floor
e$B$G$d$O$j%*!<%P!<%U%m!<$7$=$&$J5$$,$9$k$N$G$9$,!#e(B

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

In [ruby-dev:30746] the message: [ruby-dev:30746] Re:
e$BBg$-$J?t$NBg$^e(B
e$B$+$J3d$j;;e(B, on May/02 11:11(JST) [email protected]=<ye(B writes:

[email protected](B, Fload.prec_size e$B$,$I$3$K$"$k$N$+$o$+$i$J$$e(B

def Float.prec_size
return @PREC_SIZE if defined?(@PREC_SIZE)

i = 0
i += 1  while 1.0 + Math.ldexp(1.0,-i) != 1.0
@PREC_SIZE = i - 1

end

e$B$G$7$?e(B

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

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

In message Re: [ruby-dev:30749] Re: e$BBg$-$J?t$NBg$^$+$J3d$j;;e(B
on Wed, 2 May 2007 13:15:20 +0900, [email protected]
([email protected]=<ye(B) writes:

|>e$B$=$l$H!"e(BRational#floore$B$O$J$$$_$?$$$J$N$G!"e(B(self * 2**e).floor
|>e$B$G$d$O$j%*!<%P!<%U%m!<$7$=$&$J5$$,$9$k$N$G$9$,!#e(B
|
|
|class Rational
| def floor
| @numerator.div(@denominator)
| end
|end
|
|e$B$K$J$C$F$$$^$9e(B. Bignum#div e$B$,$A$c$s$HDj5A$5$l$F$$$l$PBg>fIW$=$&e(B.

e$B$A$c$s$HDj5A$5$l$F$$$J$1$l$P%P%0$G$9$+$i!D>$7$^$9!#e(B
[email protected]$5$s$N<[email protected]$$$VD>$7$F$$k$h$&$G$9$+$i%3%_%C%H$7e(B
[email protected]$1$^$;$s$+!#$G$-$l$Pe(Btrunke$B$He(Bruby_1_8e$BN>J}$K!#LLE]$J$ie(B
[email protected]$1$G$b9=$$$^$;$s!#e(B

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

In [ruby-dev:30748] the message: [ruby-dev:30748] Re:
e$BBg$-$J?t$NBg$^e(B
e$B$+$J3d$j;;e(B, on May/02 12:35(JST) Nobuyoshi N. writes:

[email protected]$G$9!#e(B

prec_sizee$B$Oe(BFloat::MANT_DIGe$B$G$$$$$N$G$O$J$$$+$H;W$$$^$9$,!"e(B

e$B$J$k$[$Ie(B. e$B2?$+$"$k$s$8$c$J$$$+$H;W$C$F$$$^$7$?e(B.

irb(main):017:0> (((522 / 100 / 365) + 1) ** 365).to_f
NoMethodError: undefined method log2floor' for #<Rational:0xb78070c0> from (irb):4:into_f
from (irb):17

e$B$H%(%i!<$K$J$j$^$7$?!#e(B

class Rational
def log2floor
raise RangeError, not defined for self <= 0 if self <= 0
return floor.log2floor if self >= 1
m=(1/self).floor.log2floor + 1
(self*2**m).floor.log2floor - m
end
end

e$B$G$7$?e(B.

e$B$=$l$H!"e(BRational#floore$B$O$J$$$_$?$$$J$N$G!"e(B(self * 2**e).floor
e$B$G$d$O$j%*!<%P!<%U%m!<$7$=$&$J5$$,$9$k$N$G$9$,!#e(B

class Rational
def floor
@numerator.div(@denominator)
end
end

e$B$K$J$C$F$$$^$9e(B. Bignum#div
e$B$,$A$c$s$HDj5A$5$l$F$$$l$PBg>fIW$=$&e(B.

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

[email protected]$G$9!#e(B

At Wed, 2 May 2007 08:56:56 +0900,
Yukihiro M. wrote in [ruby-dev:30743]:

[ruby-core:11069]e$B$GJs9p$,$"$C$?$N$G$9$,!e(Brationale$B$NJ,;RJ,Jle(B
e$B$,$$^$j$KBg$-$$$He(BFloate$B$KJQ49$G$-$:!"K\MhM-8B$NCM$J$N$K7k2Le(B
e$B$,e(BNaNe$B$K$J$C$F$7$^$&$h$&$G$9!#e(B

Bignum#quoe$B$GBP1~$9$k$H$$$&$N$b2DG=$+$b!#e(B

$ ./ruby -e num = 18511365; den = 18250365; p num.quo(den)
178.221209978501

Index: bignum.c

bignum.c (revision 12240)
+++ bignum.c (working copy)
@@ -14,4 +14,5 @@

#include <math.h>
+#include <float.h>
#include <ctype.h>
#ifdef HAVE_IEEEFP_H
@@ -853,6 +854,6 @@ rb_dbl2big(double d)
}

-double
-rb_big2dbl(VALUE x)
+static double
+big2dbl(VALUE x)
{
double d = 0.0;
@@ -863,9 +864,17 @@ rb_big2dbl(VALUE x)
d = ds[i] + BIGRAD*d;
}

  • if (!RBIGNUM(x)->sign) d = -d;
  • return d;
    +}

+double
+rb_big2dbl(VALUE x)
+{

  • double d = big2dbl(x);
  • if (isinf(d)) {
    rb_warn(Bignum out of Float range);
    d = HUGE_VAL;
    }
  • if (!RBIGNUM(x)->sign) d = -d;
    return d;
    }
    @@ -1497,4 +1506,27 @@ rb_big_divmod(VALUE x, VALUE y)
    }

+static int
+bdigbitsize(BDIGIT x)
+{

  • int size = 1;
  • int nb = BITSPERDIG / 2;
  • BDIGIT bits = (~0 << nb);
  • if (!x) return 0;
  • while (x > 1) {
  • if (x & bits) {
  •  size += nb;
    
  •  x >>= nb;
    
  • }
  • x &= ~bits;
  • nb /= 2;
  • bits >>= nb;
  • }
  • return size;
    +}

+static VALUE rb_big_rshift(VALUE,VALUE);
+
/*

  • call-seq:
    @@ -1512,7 +1544,35 @@ static VALUE
    rb_big_quo(VALUE x, VALUE y)
    {
  • double dx = rb_big2dbl(x);
  • double dx = big2dbl(x);
    double dy;

  • if (isinf(dx)) {
    +#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)

  • VALUE z;

  • int ex, ey;

  • ex = (RBIGNUM(bigtrunc(x))->len - 1) * BITSPERDIG;

  • ex += bdigbitsize(BDIGITS(x)[RBIGNUM(x)->len - 1]);

  • ex -= 2 * DBL_BIGDIG * BITSPERDIG;

  • if (ex) x = rb_big_rshift(x, INT2FIX(ex));

  • switch (TYPE(y)) {

  • case T_FIXNUM:

  •  y = rb_int2big(FIX2LONG(y));
    
  • case T_BIGNUM: {

  •  ey = (RBIGNUM(bigtrunc(y))->len - 1) * BITSPERDIG;
    
  •  ey += bdigbitsize(BDIGITS(y)[RBIGNUM(y)->len - 1]);
    
  •  ey -= DBL_BIGDIG * BITSPERDIG;
    
  •  if (ey) y = rb_big_rshift(y, INT2FIX(ey));
    
  • bignum:

  •  bigdivrem(x, y, &z, 0);
    
  •  return rb_float_new(ldexp(big2dbl(z), ex - ey));
    
  • }

  • case T_FLOAT:

  •  y = dbl2big(ldexp(frexp(RFLOAT(y)->value, &ey), DBL_MANT_DIG));
    
  •  ey -= DBL_MANT_DIG;
    
  •  goto bignum;
    
  • }

  • }
    switch (TYPE(y)) {
    case T_FIXNUM:
    @@ -1819,6 +1879,4 @@ rb_big_xor(VALUE xx, VALUE yy)
    }

-static VALUE rb_big_rshift(VALUE,VALUE);

/*

  • call-seq:

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

In message Re: [ruby-dev:30753] Re: e$BBg$-$J?t$NBg$^$+$J3d$j;;e(B
on Wed, 2 May 2007 14:45:28 +0900, Nobuyoshi N.
[email protected] writes:

|At Wed, 2 May 2007 08:56:56 +0900,
|Yukihiro M. wrote in [ruby-dev:30743]:
|> [ruby-core:11069]e$B$GJs9p$,$"$C$?$N$G$9$,!e(Brationale$B$NJ,;RJ,Jle(B
|> e$B$,$$^$j$KBg$-$$$He(BFloate$B$KJQ49$G$-$:!"K\MhM-8B$NCM$J$N$K7k2Le(B
|> e$B$,e(BNaNe$B$K$J$C$F$7$^$&$h$&$G$9!#e(B
|
|Bignum#quoe$B$GBP1~$9$k$H$$$&$N$b2DG=$+$b!#e(B

quoe$B$K$3$N$h$&$J%Q%C%A$rEv$F$k$3$H$=$N$b$N$K$O;[email protected]$G$9$,!"e(B
rational.rbe$B$r%m!<%I$7$A$c$&$He(Bquoe$B$Oe(BRationale$B$rJV$9$b$N$KCV$-49e(B
e$B$($i$l$A$c$&$s$8$c$J$$$+$H!#e(B

[email protected]$G$9!#e(B

At Wed, 2 May 2007 14:51:40 +0900,
Yukihiro M. wrote in [ruby-dev:30754]:

rational.rbe$B$r%m!<%I$7$A$c$&$He(Bquoe$B$Oe(BRationale$B$rJV$9$b$N$KCV$-49e(B
e$B$($i$l$A$c$&$s$8$c$J$$$+$H!#e(B

**e$B$_$?$$$Ke(Baliase$B$r:n$C$F$*$1$P:Q$`$N$G$O!#e(B

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

In message Re: [ruby-dev:30755] Re: e$BBg$-$J?t$NBg$^$+$J3d$j;;e(B
on Wed, 2 May 2007 15:14:46 +0900, Nobuyoshi N.
[email protected] writes:

|At Wed, 2 May 2007 14:51:40 +0900,
|Yukihiro M. wrote in [ruby-dev:30754]:
|> rational.rbe$B$r%m!<%I$7$A$c$&$He(Bquoe$B$Oe(BRationale$B$rJV$9$b$N$KCV$-49e(B
|> e$B$($i$l$A$c$&$s$8$c$J$$$+$H!#e(B
|
|**e$B$_$?$$$Ke(Baliase$B$r:n$C$F$*$1$P:Q$`$N$G$O!#e(B

trunke$B$K%3%_%C%H$7$F$/[email protected]$5$$!#e(Brational.rbe$B$G$3$l$r;H$&$+$I$&$+e(B
[email protected]$5$s$K$*G$$;$7$^$7$g$&!#e(B

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

In [ruby-dev:30757] the message: [ruby-dev:30757] Re:
e$BBg$-$J?t$NBg$^e(B
e$B$+$J3d$j;;e(B, on May/02 16:32(JST) Yukihiro M. writes:

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

trunke$B$K%3%_%C%H$7$F$/[email protected]$5$$!#e(Brational.rbe$B$G$3$l$r;H$&$+$I$&$+e(B
[email protected]$5$s$K$*G$$;$7$^$7$g$&!#e(B

e$BN;2r$G$9e(B. e$B$?$V$se(B,
e$B%*%j%8%J%ke(Bquoe$B$r;H$&$3$H$K$J$k$H;W$$$^$9e(B.

e$B$G$be(B, e$B99?7$Oe(BGWe$B4|4V$,=*$o$C$F$+$i$K$J$k$+$J$!e(B

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

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

In message Re: [ruby-dev:30771] Re: e$BBg$-$J?t$NBg$^$+$J3d$j;;e(B
on Tue, 8 May 2007 19:40:43 +0900, [email protected]
([email protected]=<ye(B) writes:

|ruby-trunk e$B$Ke(B ci e$B$7$^$7$?e(B.
|1.8e$B$NJ}$Oe(B, e$B$^[email protected](B, Bignum#quoe$B$,JQ99$5$l$F$$$J$$$N$Ge(B, e$B$=$N$^$^$G$9e(B.

[email protected]$5$s!"e(Bknue$B$5$s!"e(B1.8e$B$K$bEv$F$^$;$s$+!)e(B

|e$B$H$3$m$Ge(B,
|
|Fixnum#quo, Bignum#quo e$B$re(B quof e$B$Ke(B alias e$B$7$?$N$G$9$,e(B, e$B%G%U%)%k%H$G$=$Ne(B
|e$B$h$&$Je(Baliase$B$,$"$C$F$bNI$$5$$,$7$^$9e(B.

quofe$B$H$$$&L>A0$=$N$b$N$O5$$KF~$j$^$;$s$,!e(Baliase$B$O$$C$F$bNIe(B
e$B$$$H;W$$$^$9!#e(Bidive$B$KJo$C$Fe(Bfdive$B$H$+$G$7$g$&$+!#e(B

[email protected]$G$9!#e(B

At Tue, 8 May 2007 22:52:09 +0900,
Yukihiro M. wrote in [ruby-dev:30772]:

|ruby-trunk e$B$Ke(B ci e$B$7$^$7$?e(B.
|1.8e$B$NJ}$Oe(B, e$B$^[email protected](B, Bignum#quoe$B$,JQ99$5$l$F$$$J$$$N$Ge(B, e$B$=$N$^$^$G$9e(B.

[email protected]$5$s!"e(Bknue$B$5$s!"e(B1.8e$B$K$bEv$F$^$;$s$+!)e(B

e$B$I$&$;$J$i!"e(B[ruby-dev:30726]e$B$+$i$NN_>h$N2~A1$b0l=o$K$I$&$G$7$ge(B
e$B$&$+!#e(B

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

ruby-trunk e$B$Ke(B ci e$B$7$^$7$?e(B.
1.8e$B$NJ}$Oe(B, e$B$^[email protected](B, Bignum#quoe$B$,JQ99$5$l$F$$$J$$$N$Ge(B,
e$B$=$N$^$^$G$9e(B.

e$B$H$3$m$Ge(B,

Fixnum#quo, Bignum#quo e$B$re(B quof e$B$Ke(B alias e$B$7$?$N$G$9$,e(B,
e$B%G%U%)%k%H$G$=$Ne(B
e$B$h$&$Je(Baliase$B$,$"$C$F$bNI$$5$$,$7$^$9e(B.

In [ruby-dev:30762] the message: [ruby-dev:30762] Re:
e$BBg$-$J?t$NBg$^e(B
e$B$+$J3d$j;;e(B, on May/03 22:26(JST) [email protected]=<ye(B writes:

e$BN;2r$G$9e(B. e$B$?$V$se(B, e$B%*%j%8%J%ke(Bquoe$B$r;H$&$3$H$K$J$k$H;W$$$^$9e(B.

e$B$G$be(B, e$B99?7$Oe(BGWe$B4|4V$,=*$o$C$F$+$i$K$J$k$+$J$!e(B

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

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

In message Re: [ruby-dev:30773] Re: e$BBg$-$J?t$NBg$^$+$J3d$j;;e(B
on Tue, 8 May 2007 23:13:52 +0900, Nobuyoshi N.
[email protected] writes:

|> [email protected]$5$s!"e(Bknue$B$5$s!"e(B1.8e$B$K$bEv$F$^$;$s$+!)e(B
|
|e$B$I$&$;$J$i!"e(B[ruby-dev:30726]e$B$+$i$NN_>h$N2~A1$b0l=o$K$I$&$G$7$ge(B
|e$B$&$+!#e(B

e$B;d$OF~$l$?$$$G$9!#e(B1.8e$B$Oe(Bknue$B$5$s$KG$$;$^$9!#e(B

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

In [ruby-dev:30772] the message: [ruby-dev:30772] Re:
e$BBg$-$J?t$NBg$^e(B
e$B$+$J3d$j;;e(B, on May/08 22:52(JST) Yukihiro M. writes:

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

|Fixnum#quo, Bignum#quo e$B$re(B quof e$B$Ke(B alias e$B$7$?$N$G$9$,e(B, e$B%G%U%)%k%H$G$=$Ne(B
|e$B$h$&$Je(Baliase$B$,$"$C$F$bNI$$5$$,$7$^$9e(B.

quofe$B$H$$$&L>A0$=$N$b$N$O5$$KF~$j$^$;$s$,!e(Baliase$B$O$$C$F$bNIe(B
e$B$$$H;W$$$^$9!#e(Bidive$B$KJo$C$Fe(Bfdive$B$H$+$G$7$g$&$+!#e(B

e$B$=$l$GNI$$$G$9e(B. Rational e$B$be(B rdiv e$B$G$9$7e(B.

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

At Tue, 8 May 2007 23:13:52 +0900,
Nobuyoshi N. wrote:



/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp

Different eyes see different things,
Different hearts beat on different strings
But there are times for you and me when all such things agree

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs