[前][次][番号順一覧][スレッド一覧]

ruby-changes:62139

From: Kenta <ko1@a...>
Date: Mon, 6 Jul 2020 08:56:22 +0900 (JST)
Subject: [ruby-changes:62139] f00efef30a (master): [ruby/bigdecimal] Support a Complex in Kernel.BigDecimal()

https://git.ruby-lang.org/ruby.git/commit/?id=f00efef30a

From f00efef30ad6eca30e5674493dacbd25b425a4eb Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Wed, 9 Oct 2019 10:27:08 +0900
Subject: [ruby/bigdecimal] Support a Complex in Kernel.BigDecimal()

https://github.com/ruby/bigdecimal/commit/00795cb01f

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 23851ee..3a46bd1 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -2629,6 +2629,7 @@ VpNewVarArg(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2629
         }
     }
 
+  retry:
     switch (TYPE(iniValue)) {
       case T_DATA:
 	if (is_kind_of_BigDecimal(iniValue)) {
@@ -2666,6 +2667,18 @@ VpNewVarArg(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2667
 	}
 	return GetVpValueWithPrec(iniValue, mf, 1);
 
+      case T_COMPLEX:
+        {
+            VALUE im;
+            im = rb_complex_imag(iniValue);
+            if (!is_zero(im)) {
+                rb_raise(rb_eArgError,
+                         "Unable to make a BigDecimal from non-zero imaginary number");
+            }
+            iniValue = rb_complex_real(iniValue);
+            goto retry;
+        }
+
       case T_STRING:
 	/* fall through */
       default:
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index 6928834..80ef39a 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -137,6 +137,14 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L137
     end
   end
 
+  def test_BigDecimal_with_complex
+    assert_equal(BigDecimal("1"), BigDecimal(Complex(1, 0)))
+    assert_equal(BigDecimal("0.333333333333333333333"), BigDecimal(Complex(1.quo(3), 0), 21))
+    assert_equal(BigDecimal("0.1235"), BigDecimal(Complex(0.1234567, 0), 4))
+
+    assert_raise_with_message(ArgumentError, "Unable to make a BigDecimal from non-zero imaginary number") { BigDecimal(Complex(1, 1)) }
+  end
+
   def test_BigDecimal_with_big_decimal
     assert_equal(BigDecimal(1), BigDecimal(BigDecimal(1)))
     assert_equal(BigDecimal('+0'), BigDecimal(BigDecimal('+0')))
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]