[#33567] rational, complex and nuby — Tadayoshi Funaba <tadf@...>

ruby に rational と complex を組みこもうと試していて nuby という派生物

21 messages 2008/02/02

[#33580] Re: cgi.rb再構築案 — "Makoto Kuwata" <kwa@...>

桑田といいます。

17 messages 2008/02/03

[#33611] Solaris で timeout.rb が Segmentation fault する。 — shiiya@...

はじめまして。椎屋と申します。

15 messages 2008/02/06
[#33612] Re: Solaris で timeout.rb が Segmentation fault する。 — Nobuyoshi Nakada <nobu@...> 2008/02/06

なかだです。

[#33613] Re: Solaris で timeout.rb が Segmentation fault する。 — shiiya yoshitaka <shiiya@...> 2008/02/06

椎屋です。反応ありがとうございます。

[#33650] Re: Solaris で timeout.rb が Segmentation fault する。 — Nobuyoshi Nakada <nobu@...> 2008/02/08

なかだです。

[#33652] Re: Solaris で timeout.rb が Segmentation fault する。 — SATOH Fumiyasu <fumiyas@...> 2008/02/08

さとうふみやす @ OSS テクノロジです。

[#33621] EUC-KR <-> UTF-8 transition table — "Park Ji-In" <tisphie@...>

朴 芝印です。

15 messages 2008/02/06

[#33628] encdet.rb — Tanaka Akira <akr@...>

前から考えていたのですが、ファイル先頭の magic comment や

18 messages 2008/02/07

[#33662] rational, complex and mathn — Tadayoshi Funaba <tadf@...>

rational は floor、truncate、ceil、round を定義していません。Numeric

66 messages 2008/02/08
[#33663] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/08

他にも問題、課題はあると思います。すぐに解決できるものと、そうでないも

[#33664] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/08

ひとつ書き忘れました。

[#33707] Re: rational, complex and mathn — Yukihiro Matsumoto <matz@...> 2008/02/12

まつもと ゆきひろです

[#33714] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/12

> 原さんのrationalは導入予定がありますので、この機会にもう一度

[#33727] Re: rational, complex and mathn — Shin-ichiro HARA <sinara@...> 2008/02/13

原です。

[#33761] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/13

> 前にふなばさんと個人的なメールのやりとりで、結局また私がrationalをまと

[#33788] Re: rational, complex and mathn — Shin-ichiro HARA <sinara@...> 2008/02/15

原です。

[#33795] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/15

> > それなりに速くはなるし、単純なところでそれなりに満足していますが、一度、

[#33806] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/16

nurat 0.0.2 を出しました (ついでに nucomp も)。

[#33812] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/16

仕様を確認していきたいと思います。

[#33815] Re: rational, complex and mathn — Yukihiro Matsumoto <matz@...> 2008/02/16

まつもと ゆきひろです

[#33818] Re: rational, complex and mathn — Shin-ichiro HARA <sinara@...> 2008/02/16

原です。

[#33819] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/17

> > new!はRubyで実装しているためにだけ必要なので、Cで実装するな

[#33821] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/17

> Rational() は、1つか2つの引数をとる。

[#33827] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/17

> 実際的に重要な機能が Rational() という名前で固定されるのはクラスの定義

[#33845] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/18

もうあまり手を入れないでおこうと思ったのです、つい手を入れてしまいまし

[#33886] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/21

ちょっと実験してみました。原さんの rational は、かけ算割り算が速いので、

[#33888] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/21

で、考えていたんですが、目的は、最速の rational を作ることではなくて、

[#33903] Re: rational, complex and mathn — Shin-ichiro HARA <sinara@...> 2008/02/22

原です。

[#33905] Re: rational, complex and mathn — "NARUSE, Yui" <naruse@...> 2008/02/22

成瀬です。

[#33908] Re: rational, complex and mathn — Yukihiro Matsumoto <matz@...> 2008/02/22

まつもと ゆきひろです

[#33914] Re: rational, complex and mathn — Tadayoshi Funaba <tadf@...> 2008/02/23

> はい。Complexについても1.9の間に組み込んでよいと思います。

[#33679] bigdecimal — Tadayoshi Funaba <tadf@...>

bigdecimal/math.rb の BigMath は、利用者が include してつかうことを前

23 messages 2008/02/09
[#33680] Re: bigdecimal — Tadayoshi Funaba <tadf@...> 2008/02/09

Integer や Float に比べると、BigDicimal() は、1 や 1.1 を受けつけない、

[#33686] Re: bigdecimal — Tadashi Saito <shiba@...2.accsnet.ne.jp> 2008/02/10

斎藤と申します。

[#33698] Re: bigdecimal — Tadayoshi Funaba <tadf@...> 2008/02/11

> 仮にBigDecimal(1.1)を、(二進小数として)受け付けると、「BigDecimalでは、

[#33705] Re: bigdecimal — Yukihiro Matsumoto <matz@...> 2008/02/12

まつもと ゆきひろです

[#33726] Re: [ruby-cvs:22680] Ruby:r15443 (trunk): * bootstraptest/runner.rb, bootstraptest/test_method.rb, enc/depend, — "U.Nakamura" <usa@...>

こんにちは、なかむら(う)です。

14 messages 2008/02/13
[#33730] Re: [ruby-cvs:22680] Ruby:r15443 (trunk): * bootstraptest/runner.rb, bootstraptest/test_method.rb, enc/depend, — "NARUSE, Yui" <naruse@...> 2008/02/13

成瀬です。

[#33889] Re: [ ruby-Bugs-17454 ] irb crash while iterating over all objects — Urabe Shyouhei <shyouhei@...>

卜部です。ちょっとお知恵を拝借したく。

22 messages 2008/02/21
[#33892] Re: [ ruby-Bugs-17454 ] irb crash while iterating over all objects — Nobuyoshi Nakada <nobu@...> 2008/02/21

なかだです。

[#33909] Re: [ ruby-Bugs-17454 ] irb crash while iterating over all objects — Urabe Shyouhei <shyouhei@...> 2008/02/22

Nobuyoshi Nakada さんは書きました:

[#36081] Re: [ ruby-Bugs-17454 ] irb crash while iterating over all objects — TOYOFUKU Chikanobu <nobu_toyofuku@...> 2008/09/01

豊福です。

[#36085] Re: [ ruby-Bugs-17454 ] irb crash while iterating over all objects — Yukihiro Matsumoto <matz@...> 2008/09/01

まつもと ゆきひろです

[ruby-dev:33658] BigDecimal#to_f precision issue

From: Nobuyoshi Nakada <nobu@...>
Date: 2008-02-08 11:24:29 UTC
List: ruby-dev #33658
なかだです。

[ruby-talk:290296]の話ですが、BigDecimal#to_fの精度が不十分なようです。

  $ ruby18 -v -rbigdecimal -e 'printf "%.15f\n", BigDecimal.new("15.25").to_f'
  ruby 1.8.6 (2008-02-08 revision 15402) [i686-linux]
  15.250000000000002
  $ ruby19 -v -rbigdecimal -e 'printf "%.15f\n", BigDecimal.new("15.25").to_f'
  ruby 1.9.0 (2008-02-08 revision 15402) [i686-linux]
  15.250000000000002

要するに10進→2進変換の問題なので、strtod()から持ってこようかと
も思いましたが、strtod()をそのまま使うのが一番簡単じゃないでしょ
うか。

また、DBLE_FIGを内部で計算していますが、レジスタ間の演算に最適化
された場合、doubleではなくてlong doubleで計算されてしまう場合が
あります。DBL_DIGを使うべきでしょう。

もう一点、BASEやBASE_FIGも実行時に求めるよりも定数にしたほうがい
いのではないかと思います。


Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 15405)
+++ ext/bigdecimal/bigdecimal.c	(working copy)
@@ -586,17 +586,20 @@ BigDecimal_to_f(VALUE self)
     ENTER(1);
     Real *p;
-    double d, d2;
+    double d;
     S_LONG e;
+    char *buf;
 
     GUARD_OBJ(p,GetVpValue(self,1));
     if(VpVtoD(&d, &e, p)!=1) return rb_float_new(d);
+    buf = ALLOCA_N(char,(unsigned int)VpNumOfChars(p,"E"));
+    VpToString(p, buf, 0, 0);
     errno = 0;
-    d2 = pow(10.0,(double)e);
-    if((errno == ERANGE && e>0) || (d2>1.0 && (fabs(d) > (DBL_MAX / d2)))) {
+    d = strtod(buf, 0);
+    if(errno == ERANGE) {
        VpException(VP_EXCEPTION_OVERFLOW,"BigDecimal to Float conversion",0);
        if(d>0.0) return rb_float_new(DBL_MAX);
        else      return rb_float_new(-DBL_MAX);
     }
-    return rb_float_new(d*d2);
+    return rb_float_new(d);
 }
 
@@ -1051,5 +1054,5 @@ BigDecimal_remainder(VALUE self, VALUE r
 {
     VALUE  f;
-    Real  *d,*rv;
+    Real  *d,*rv=0;
     f = BigDecimal_divremain(self,r,&d,&rv);
     if(f!=(VALUE)0) return f;
@@ -1505,5 +1508,5 @@ BigDecimal_to_s(int argc, VALUE *argv, V
                 fPlus = 2; psz++;
             }
-            while(ch=*psz++) {
+            while((ch=*psz++)!=0) {
                 if(ISSPACE(ch)) continue;
                 if(!ISDIGIT(ch)) {
@@ -1962,4 +1965,5 @@ static U_LONG gnPrecLimit = 0;  /* Globa
 static U_LONG gfRoundMode = VP_ROUND_HALF_UP; /* Mode for general rounding operation   */
 
+#ifndef BASE_FIG
 static U_LONG BASE_FIG = 4;     /* =log10(BASE)  */
 static U_LONG BASE = 10000L;    /* Base value(value must be 10**BASE_FIG) */
@@ -1967,10 +1971,19 @@ static U_LONG BASE = 10000L;    /* Base 
                 /* within one U_LONG. */
 static U_LONG HALF_BASE = 5000L;/* =BASE/2  */
-static S_LONG DBLE_FIG = 8;    /* figure of double */
 static U_LONG BASE1 = 1000L;    /* =BASE/10  */
+#else
+#ifndef BASE
+#error BASE_FIG is defined but BASE is not
+#endif
+#define HALF_BASE (BASE/2)
+#define BASE1 (BASE/10)
+#endif
+#ifndef DBLE_FIG
+#define DBLE_FIG (DBL_DIG+1)    /* figure of double */
+#endif
 
 static Real *VpConstOne;    /* constant 1.0 */
 static Real *VpPt5;        /* constant 0.5 */
-static U_LONG maxnr = 100;    /* Maximum iterations for calcurating sqrt. */
+#define maxnr 100UL    /* Maximum iterations for calcurating sqrt. */
                 /* used in VpSqrt() */
 
@@ -2089,6 +2102,6 @@ VpSetRoundMode(unsigned long n)
  *    (... but not actually..)).
  */
-volatile double gZero_ABCED9B1_CE73__00400511F31D = 0.0;
-volatile double gOne_ABCED9B4_CE73__00400511F31D  = 1.0;
+volatile const double gZero_ABCED9B1_CE73__00400511F31D = 0.0;
+volatile const double gOne_ABCED9B4_CE73__00400511F31D  = 1.0;
 static double
 Zero(void)
@@ -2362,7 +2375,4 @@ VP_EXPORT U_LONG
 VpInit(U_LONG BaseVal)
 {
-    U_LONG w;
-    double v;
-
     /* Setup +/- Inf  NaN -0 */
     VpGetDoubleNaN();
@@ -2371,5 +2381,7 @@ VpInit(U_LONG BaseVal)
     VpGetDoubleNegZero();
 
+#ifndef BASE_FIG
     if(BaseVal <= 0) {
+        U_LONG w;
         /* Base <= 0, then determine Base by calcuration. */
         BASE = 1;
@@ -2388,4 +2400,6 @@ VpInit(U_LONG BaseVal)
     BASE_FIG = 0;
     while(BaseVal /= 10) ++BASE_FIG;
+#endif
+
     /* Allocates Vp constants. */
     VpConstOne = VpAlloc((U_LONG)1, "1");
@@ -2396,13 +2410,4 @@ VpInit(U_LONG BaseVal)
 #endif /* _DEBUG */
 
-    /* Determine # of digits available in one 'double'. */
-
-    v = 1.0;
-    DBLE_FIG = 0;
-    while(v + 1.0 > 1.0) {
-        ++DBLE_FIG;
-        v /= 10;
-    }
-
 #ifdef _DEBUG
     if(gfDebug) {
@@ -2507,5 +2512,5 @@ VpAlloc(U_LONG mx, const char *szVal)
     i   = 0;
     ipn = 0;
-    while(psz[i]=szVal[ipn]) {
+    while((psz[i]=szVal[ipn])!=0) {
         if(ISDIGIT(psz[i])) ++ni;
         if(psz[i]=='_') {
@@ -2550,5 +2555,5 @@ VpAlloc(U_LONG mx, const char *szVal)
     /* Skip digits */
     ni = 0;            /* digits in mantissa */
-    while(v = szVal[i]) {
+    while((v = szVal[i]) != 0) {
         if(!ISDIGIT(v)) break;
         ++i;
@@ -2564,5 +2569,5 @@ VpAlloc(U_LONG mx, const char *szVal)
             ++i;
             ipf = i;
-            while(v = szVal[i]) {    /* get fraction part. */
+            while((v = szVal[i]) != 0) {    /* get fraction part. */
                 if(!ISDIGIT(v)) break;
                 ++i;
@@ -2582,5 +2587,5 @@ VpAlloc(U_LONG mx, const char *szVal)
             v = szVal[i];
             if((v == '-') ||(v == '+')) ++i;
-            while(v=szVal[i]) {
+            while((v=szVal[i])!=0) {
                 if(!ISDIGIT(v)) break;
                 ++i;
@@ -4440,5 +4445,5 @@ VpLeftRound(Real *y, int f, int nf)
     v = y->frac[0];
     nf -= VpExponent(y)*BASE_FIG;
-    while(v=v/10) nf--;
+    while((v /= 10) != 0) nf--;
     nf += (BASE_FIG-1);
     return VpMidRound(y,f,nf);
Index: ext/bigdecimal/extconf.rb
===================================================================
--- ext/bigdecimal/extconf.rb	(revision 15405)
+++ ext/bigdecimal/extconf.rb	(working copy)
@@ -1,2 +1,12 @@
 require 'mkmf'
+
+base_fig = 0
+src = ("(BASE > 0) && "
+       "(BASE * (BASE+1)) > BASE && "
+       "(BASE * (BASE+1)) / BASE == (BASE+1)")
+while try_static_assert(src, nil, "-DBASE=10#{'0'*base_fig}UL")
+  base_fig += 1
+end
+$defs << "-DBASE=1#{'0'*base_fig}UL" << "-DBASE_FIG=#{base_fig}"
+
 create_makefile('bigdecimal')



-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

In This Thread

Prev Next