[#38563] [Bug #1556] irb does not save history from 1.8.7-p83 and later — Nobuhiro IMAI <redmine@...>

Bug #1556: irb does not save history from 1.8.7-p83 and later

11 messages 2009/06/02

[#38571] [Bug #1582] IO.new Raises Other Errors between 1.8 and 1.9 — "ujihisa ." <redmine@...>

Bug #1582: IO.new Raises Other Errors between 1.8 and 1.9

15 messages 2009/06/05

[#38607] [Feature: trunk] GC.stat — SASADA Koichi <ko1@...>

 ささだです.

21 messages 2009/06/14

[#38608] Fixnum#fdiv — Tadayoshi Funaba <tadf@...>

Bignum#fdiv には大きな数である場合の配慮があるようですが、Fixnum ではな

23 messages 2009/06/14
[#38636] Re: Fixnum#fdiv — Tadayoshi Funaba <tadf@...> 2009/06/15

fdiv では2つの異る解釈が混在しているように見えます。

[#38638] Re: Fixnum#fdiv — Yukihiro Matsumoto <matz@...> 2009/06/15

まつもと ゆきひろです

[#38639] Re: Fixnum#fdiv — Tadayoshi Funaba <tadf@...> 2009/06/15

> えーと、設計者は「fdivは結果がfloatになるdiv」くらいしか考え

[#38640] Re: Fixnum#fdiv — Yukihiro Matsumoto <matz@...> 2009/06/15

まつもと ゆきひろです

[#38641] Re: Fixnum#fdiv — Tadayoshi Funaba <tadf@...> 2009/06/15

> ふむ。「中途半端」というのはfixnumとbignumで食い違うと言う意

[#38657] Re: Fixnum#fdiv — Tadayoshi Funaba <tadf@...> 2009/06/16

> > ふむ。「中途半端」というのはfixnumとbignumで食い違うと言う意

[#38659] Re: Fixnum#fdiv — Yukihiro Matsumoto <matz@...> 2009/06/16

まつもと ゆきひろです

[#38660] Re: Fixnum#fdiv — Tadayoshi Funaba <tadf@...> 2009/06/16

> 私が気にしているのは「挙動の理解しやすさ」ですね。

[#38701] [Bug #1676] only last "return" is traced by set_trace_func — _ wanabe <redmine@...>

Bug #1676: only last "return" is traced by set_trace_func

10 messages 2009/06/22

[ruby-dev:38682] Re: Fixnum#fdiv

From: Tadayoshi Funaba <tadf@...>
Date: 2009-06-18 15:17:50 UTC
List: ruby-dev #38682
> coerceのことを懸念していらっしゃったわけなんですか。すみませ
> ん、私はここでは考えていませんでした。[ruby-dev:38671]で
> 「未知のクラス」とおっしゃってるんだから、当然考えておくべき
> でしたね。

どうも僕の説明も非道いもので、理解が難しいような気がしますが。

無限大との比較が出来れば、それで無限大かどうか判断できるわけですから
infinite? とかなくても取り敢えずなんとかなる、という考えもありましたが
やはり厳しいような気がします。

しかも、無限大かどうか判断するのは効率も悪い上に完全でもないですから。

まつもとさんが考えているのと違うかもしれませんが、infinite? があるとい
う前提であくまで試験的に書いてみましたけど、いかにもとってつけたようです。



Index: numeric.c
===================================================================
--- numeric.c	(revision 23732)
+++ numeric.c	(working copy)
@@ -417,6 +417,24 @@
     return Qfalse;
 }
 
+static VALUE
+num_nan_p(VALUE num)
+{
+    return Qfalse;
+}
+
+static VALUE
+num_infinite_p(VALUE num)
+{
+    return Qnil;
+}
+
+static VALUE
+num_finite_p(VALUE num)
+{
+    return Qtrue;
+}
+
 /*
  *  call-seq:
  *     num.abs   => num or numeric
@@ -673,9 +691,21 @@
     }
 }
 
+#define f_finite_p(x) rb_funcall(x, rb_intern("finite?"), 0)
+#define f_infinite(x) rb_funcall(x, rb_intern("infinite?"), 0)
+#define f_infinite_p(x) (!NIL_P(f_infinite(x)))
+#define f_negative_p(x) rb_funcall(x, '<', 1, INT2FIX(0))
+
 static VALUE
 flo_quo(VALUE x, VALUE y)
 {
+    if (f_infinite_p(x) && f_finite_p(y))
+	return rb_float_new((f_negative_p(x) == f_negative_p(y))
+			    ? HUGE_VAL : -HUGE_VAL);
+    if (f_finite_p(x) && f_infinite_p(y))
+	return rb_float_new((f_negative_p(x) == f_negative_p(y))
+			    ? 0.0 : -0.0);
+
     return rb_funcall(x, '/', 1, y);
 }
 
@@ -3179,6 +3209,9 @@
 
     rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
     rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
+    rb_define_method(rb_cNumeric, "nan?",      num_nan_p, 0);
+    rb_define_method(rb_cNumeric, "infinite?", num_infinite_p, 0);
+    rb_define_method(rb_cNumeric, "finite?",   num_finite_p, 0);
     rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
     rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
 
Index: bignum.c
===================================================================
--- bignum.c	(revision 23732)
+++ bignum.c	(working copy)
@@ -2451,12 +2451,20 @@
  *
  */
 
+#define f_finite_p(x) rb_funcall(x, rb_intern("finite?"), 0)
+#define f_infinite(x) rb_funcall(x, rb_intern("infinite?"), 0)
+#define f_infinite_p(x) (!NIL_P(f_infinite(x)))
+#define f_negative_p(x) rb_funcall(x, '<', 1, INT2FIX(0))
 
 VALUE
 rb_big_fdiv(VALUE x, VALUE y)
 {
     double dx, dy;
 
+    if (f_infinite_p(y))
+	return rb_float_new((f_negative_p(x) == f_negative_p(y))
+			    ? 0.0 : -0.0);
+
     dx = big2dbl(x);
     switch (TYPE(y)) {
       case T_FIXNUM:
Index: rational.c
===================================================================
--- rational.c	(revision 23732)
+++ rational.c	(working copy)
@@ -766,9 +766,17 @@
     }
 }
 
+#define f_finite_p(x) rb_funcall(x, rb_intern("finite?"), 0)
+#define f_infinite(x) rb_funcall(x, rb_intern("infinite?"), 0)
+#define f_infinite_p(x) (!NIL_P(f_infinite(x)))
+
 static VALUE
 nurat_fdiv(VALUE self, VALUE other)
 {
+    if (f_infinite_p(other))
+	return rb_float_new((f_negative_p(self) == f_negative_p(other))
+			    ? 0.0 : -0.0);
+
     return f_to_f(f_div(self, other));
 }
 

In This Thread

Prev Next