[#30743] 大きな数の大まかな割り算 — Yukihiro Matsumoto <matz@...>
まつもと ゆきひろです
17 messages
2007/05/01
[#30753] Re: 大きな数の大まかな割り算
— Nobuyoshi Nakada <nobu@...>
2007/05/02
なかだです。
[#30754] Re: 大きな数の大まかな割り算
— Yukihiro Matsumoto <matz@...>
2007/05/02
まつもと ゆきひろです
[#30755] Re: 大きな数の大まかな割り算
— Nobuyoshi Nakada <nobu@...>
2007/05/02
なかだです。
[#30757] Re: 大きな数の大まかな割り算
— Yukihiro Matsumoto <matz@...>
2007/05/02
まつもと ゆきひろです
[#30765] Re: [ruby-cvs:19483] Ruby:r12247: * lib/date/format.rb (Format::Bag#method_missing): get rid of — Tadayoshi Funaba <tadf@...>
> Log:
4 messages
2007/05/06
[#30767] Re: [ruby-cvs:19483] Ruby:r12247: * lib/date/format.rb (Format::Bag#method_missing): get rid of
— Yukihiro Matsumoto <matz@...>
2007/05/06
まつもと ゆきひろです
[#30783] Fwd: ruby-mode — Seiji Zenitani <zenitani@...>
こんにちは。
13 messages
2007/05/14
[#30813] Re: Fwd: ruby-mode
— Seiji Zenitani <zenitani@...>
2007/05/25
重ねて失礼いたします。
[#30784] [BUG on Ruby??] Ruby/Tk on Ruby1.8.[56] is too slow on Windows — Hidetoshi NAGAI <nagai@...>
永井@知能.九工大です.
7 messages
2007/05/16
[#30785] Re: [BUG on Ruby??] Ruby/Tk on Ruby1.8.[56] is too slow on Windows
— "U.Nakamura" <usa@...>
2007/05/17
こんにちは、なかむら(う)です。
[#30786] Re: [BUG on Ruby??] Ruby/Tk on Ruby1.8.[56] is too slow on Windows
— Hidetoshi NAGAI <nagai@...>
2007/05/17
永井@知能.九工大です.
[#30800] tail call optimization — SASADA Koichi <ko1@...>
ささだです.
6 messages
2007/05/21
[#30810] Re: FileUtils.rm_rf misfeature? — Nobuyoshi Nakada <nobu@...>
なかだです。
5 messages
2007/05/24
[#30827] Supporting Fiber — SASADA Koichi <ko1@...>
ささだです。
22 messages
2007/05/27
[#30828] Re: Supporting Fiber
— Shugo Maeda <shugo@...>
2007/05/28
前田です。
[#30924] Re: Supporting Fiber
— "Yusuke ENDOH" <mame@...>
2007/06/07
遠藤です。
[#30925] Re: Supporting Fiber
— SASADA Koichi <ko1@...>
2007/06/07
ささだです。
[#30941] Re: Supporting Fiber
— "Yusuke ENDOH" <mame@...>
2007/06/09
遠藤です。
[#30832] callcc, each and local val — sheepman <sheepman@...>
こんばんは、sheepman です。
5 messages
2007/05/29
[ruby-dev:30753] Re: 大きな数の大まかな割り算
From:
Nobuyoshi Nakada <nobu@...>
Date:
2007-05-02 05:45:28 UTC
List:
ruby-dev #30753
なかだです。
At Wed, 2 May 2007 08:56:56 +0900,
Yukihiro Matsumoto wrote in [ruby-dev:30743]:
> [ruby-core:11069]で報告があったのですが、rationalの分子分母
> があまりに大きいとFloatに変換できず、本来有限の値なのに結果
> がNaNになってしまうようです。
Bignum#quoで対応するというのも可能かも。
$ ./ruby -e 'num = 18511**365; den = 18250**365; 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:
--
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
中田 伸悦