[#28653] create header (Re: Re: ossl_cipher.c:124: warning: control reaches end of non-void function) — "Nobuyoshi Nakada" <nobu@...>

なかだです。

8 messages 2006/05/18

[ruby-dev:28642] Re: Float#div and Float#divmod [AGAIN]

From: Shin-ichiro HARA <sinara@...>
Date: 2006-05-16 09:15:15 UTC
List: ruby-dev #28642
原です。

>ふなばです。
>
>> 次に、現在(1.8.4 snapshot)で、
>> 
>>   -3.4.divmod(1.1) #=> [-4.0, 1.0]
>> となっていますが、これは [-4, 1.0] とすべきです。
>> 理由は、この配列は div と % の結果であるべきだから
>> です。
>
>みたところ 1.9 では、[-4.0, 1.0] になりますね。1.8 でそうすべきなら、
>1.9 でもそうでないとおかしいように思うのですが。

その通りですね。

殆ど前と同じですが、一応パッチの形にしておきます。
まつもとさん、よろしくお願いします。


Tue May 16 17:23:19 2006    <sinara@blade.nagaokaut.ac.jp>

        * numeric.c (flo_divmod): the first element of Float#divmod should
          be an integer. [ruby-dev:28589]

        * test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.


--- numeric.c.orig      2006-05-16 17:31:44.071989816 +0900
+++ numeric.c   2006-05-16 17:37:20.466850024 +0900
@@ -284,21 +284,21 @@
  *    ------+-----+---------------+---------+-------------+---------------
  *    -13   | -4  |   3,   -1     |   3     |   -1        |    -1
  *    ------+-----+---------------+---------+-------------+---------------
- *     11.5 |  4  |   2.0,  3.5   |   2.875 |    3.5      |     3.5
+ *     11.5 |  4  |   2,    3.5   |   2.875 |    3.5      |     3.5
  *    ------+-----+---------------+---------+-------------+---------------
- *     11.5 | -4  |  -3.0, -0.5   |  -2.875 |   -0.5      |     3.5
+ *     11.5 | -4  |  -3,   -0.5   |  -2.875 |   -0.5      |     3.5
  *    ------+-----+---------------+---------+-------------+---------------
- *    -11.5 |  4  |  -3.0   0.5   |  -2.875 |    0.5      |    -3.5
+ *    -11.5 |  4  |  -3,    0.5   |  -2.875 |    0.5      |    -3.5
  *    ------+-----+---------------+---------+-------------+---------------
- *    -11.5 | -4  |   2.0  -3.5   |   2.875 |   -3.5      |    -3.5
+ *    -11.5 | -4  |   2,   -3.5   |   2.875 |   -3.5      |    -3.5
  *
  *
  *  Examples
  *     11.divmod(3)         #=> [3, 2]
  *     11.divmod(-3)        #=> [-4, -1]
- *     11.divmod(3.5)       #=> [3.0, 0.5]
- *     (-11).divmod(3.5)    #=> [-4.0, 3.0]
- *     (11.5).divmod(3.5)   #=> [3.0, 1.0]
+ *     11.divmod(3.5)       #=> [3, 0.5]
+ *     (-11).divmod(3.5)    #=> [-4, 3.0]
+ *     (11.5).divmod(3.5)   #=> [3, 1.0]
  */
 
 static VALUE
@@ -696,7 +696,7 @@
 static VALUE
 flo_divmod(VALUE x, VALUE y)
 {
-    double fy, div, mod;
+    double fy, div, mod, val;
     volatile VALUE a, b;
 
     switch (TYPE(y)) {
@@ -713,7 +713,13 @@
        return rb_num_coerce_bin(x, y);
     }
     flodivmod(RFLOAT(x)->value, fy, &div, &mod);
-    a = rb_float_new(div);
+    if (FIXABLE(div)) {
+        val = div;
+        a = LONG2FIX(val);
+    }
+    else {
+        a = rb_dbl2big(div);
+    }
     b = rb_float_new(mod);
     return rb_assoc_new(a, b);
 }



--- test/ruby/test_float.rb.orig        2006-05-16 17:39:56.453136504 +0900
+++ test/ruby/test_float.rb     2006-05-16 17:40:33.460510528 +0900
@@ -87,4 +87,32 @@
     assert_raise(ArgumentError){Float("1e")}
     # add expected behaviour here.
   end
+
+  def test_divmod
+    assert_equal([2, 3.5], 11.5.divmod(4))
+    assert_equal([-3, -0.5], 11.5.divmod(-4))
+    assert_equal([-3, 0.5], (-11.5).divmod(4))
+    assert_equal([2, -3.5], (-11.5).divmod(-4))
+  end
+
+  def test_div
+    assert_equal(2, 11.5.div(4))
+    assert_equal(-3, 11.5.div(-4))
+    assert_equal(-3, (-11.5).div(4))
+    assert_equal(2, (-11.5).div(-4))
+  end
+
+  def test_modulo
+    assert_equal(3.5, 11.5.modulo(4))
+    assert_equal(-0.5, 11.5.modulo(-4))
+    assert_equal(0.5, (-11.5).modulo(4))
+    assert_equal(-3.5, (-11.5).modulo(-4))
+  end
+
+  def test_remainder
+    assert_equal(3.5, 11.5.remainder(4))
+    assert_equal(3.5, 11.5.remainder(-4))
+    assert_equal(-3.5, (-11.5).remainder(4))
+    assert_equal(-3.5, (-11.5).remainder(-4))
+  end
 end


In This Thread