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

ruby-changes:66106

From: Jeremy <ko1@a...>
Date: Sun, 9 May 2021 06:45:51 +0900 (JST)
Subject: [ruby-changes:66106] 406ae7fb03 (master): Fix Math.cbrt(0.0) on glibc

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

From 406ae7fb03640e11e200382ef61cd450b952b7aa Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 28 Apr 2021 13:35:22 -0700
Subject: Fix Math.cbrt(0.0) on glibc

This should return 0, but on glibc it returned NaN.

Fixes [Bug #17804]
---
 math.c                 | 2 +-
 test/ruby/test_math.rb | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/math.c b/math.c
index 470979c..3b72149 100644
--- a/math.c
+++ b/math.c
@@ -703,7 +703,7 @@ math_cbrt(VALUE unused_obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L703
     double f = Get_Double(x);
     double r = cbrt(f);
 #if defined __GLIBC__
-    if (isfinite(r)) {
+    if (isfinite(r) && !(f == 0.0 && r == 0.0)) {
 	r = (2.0 * r + (f / r / r)) / 3.0;
     }
 #endif
diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb
index 5cc12bc..6c2b257 100644
--- a/test/ruby/test_math.rb
+++ b/test/ruby/test_math.rb
@@ -201,6 +201,7 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L201
     check(-2, Math.cbrt(-8))
     check(3, Math.cbrt(27))
     check(-0.1, Math.cbrt(-0.001))
+    check(0.0, Math.cbrt(0.0))
     assert_nothing_raised { assert_infinity(Math.cbrt(1.0/0)) }
     assert_operator(Math.cbrt(1.0 - Float::EPSILON), :<=, 1.0)
   end
-- 
cgit v1.1


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

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