[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 84/85: Have log and log10(real nan) return real nan rega
From: |
Andy Wingo |
Subject: |
[Guile-commits] 84/85: Have log and log10(real nan) return real nan regardless of sign |
Date: |
Thu, 13 Jan 2022 03:40:27 -0500 (EST) |
wingo pushed a commit to branch main
in repository guile.
commit 19bc021e349f4aaff53daa42032025929ee1c0f2
Author: Daniel Llorens <lloda@sarc.name>
AuthorDate: Mon Jan 10 13:26:00 2022 +0100
Have log and log10(real nan) return real nan regardless of sign
* libguile/numbers.c (log_of_shifted_double, scm_log10): Avoid complex
extension when the argument is a real nan.
* test-suite/tests/numbers.test: Tests for nans of either sign.
---
libguile/numbers.c | 17 +++++++++--------
test-suite/tests/numbers.test | 11 ++++++++++-
2 files changed, 19 insertions(+), 9 deletions(-)
diff --git a/libguile/numbers.c b/libguile/numbers.c
index 83fe027a9..4417f861f 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -6967,12 +6967,12 @@ scm_is_number (SCM z)
static SCM
log_of_shifted_double (double x, long shift)
{
+ /* cf scm_log10 */
double ans = log (fabs (x)) + shift * M_LN2;
-
- if (copysign (1.0, x) > 0.0)
- return scm_i_from_double (ans);
- else
+ if (signbit (x) && SCM_LIKELY (!isnan (x)))
return scm_c_make_rectangular (ans, M_PI);
+ else
+ return scm_i_from_double (ans);
}
/* Returns log(n), for exact integer n */
@@ -7081,10 +7081,11 @@ SCM_PRIMITIVE_GENERIC (scm_log10, "log10", 1, 0, 0,
{
double re = scm_to_double (z);
double l = log10 (fabs (re));
- if (copysign (1.0, re) > 0.0)
- return scm_i_from_double (l);
- else
- return scm_c_make_rectangular (l, M_LOG10E * M_PI);
+ /* cf log_of_shifted_double */
+ if (signbit (re) && SCM_LIKELY (!isnan (re)))
+ return scm_c_make_rectangular (l, M_LOG10E * M_PI);
+ else
+ return scm_i_from_double (l);
}
}
else if (SCM_BIGP (z))
diff --git a/test-suite/tests/numbers.test b/test-suite/tests/numbers.test
index 51263f0ac..0b80c0356 100644
--- a/test-suite/tests/numbers.test
+++ b/test-suite/tests/numbers.test
@@ -21,7 +21,8 @@
#:use-module (ice-9 documentation)
#:autoload (system base compile) (compile)
#:use-module (srfi srfi-1) ; list library
- #:use-module (srfi srfi-11)) ; let-values
+ #:use-module (srfi srfi-11) ; let-values
+ #:use-module (rnrs bytevectors))
;;;
;;; miscellaneous
@@ -4762,6 +4763,10 @@
(pass-if-exception "(log 0)" exception:numerical-overflow
(log 0))
+ ; result of log(nan) is real nan regardless of the sign of the nan.
+ (pass-if (test-eqv? +nan.0 (log (bytevector-ieee-double-ref #vu8(0 0 0 0 0 0
248 127) 0 'little))))
+ (pass-if (test-eqv? +nan.0 (log (bytevector-ieee-double-ref #vu8(0 0 0 0 0 0
248 255) 0 'little))))
+
(pass-if (test-eqv? -inf.0 (log 0.0)))
(pass-if (test-eqv? +inf.0 (log +inf.0)))
(pass-if (test-eqv? -inf.0+3.14159265358979i (log -0.0)))
@@ -4811,6 +4816,10 @@
(pass-if-exception "(log10 0)" exception:numerical-overflow
(log10 0))
+ ; result of log10(nan) is real nan regardless of the sign of the nan.
+ (pass-if (test-eqv? +nan.0 (log10 (bytevector-ieee-double-ref #vu8(0 0 0 0 0
0 248 127) 0 'little))))
+ (pass-if (test-eqv? +nan.0 (log10 (bytevector-ieee-double-ref #vu8(0 0 0 0 0
0 248 255) 0 'little))))
+
(pass-if (test-eqv? -inf.0 (log10 0.0)))
(pass-if (test-eqv? +inf.0 (log10 +inf.0)))
(pass-if (test-eqv? -inf.0+1.36437635384184i (log10 -0.0)))
- [Guile-commits] 37/85: Reimplement = on integer lib, clean up scm_num_eq_p, (continued)
- [Guile-commits] 37/85: Reimplement = on integer lib, clean up scm_num_eq_p, Andy Wingo, 2022/01/13
- [Guile-commits] 40/85: Simplify implementation of min, max, Andy Wingo, 2022/01/13
- [Guile-commits] 42/85: Simplify scm_difference, use integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 47/85: Fix scm_integer_to_double_z to always round; clean ups, Andy Wingo, 2022/01/13
- [Guile-commits] 50/85: Reimplement scm_{to,from}_{int32,uint32}, Andy Wingo, 2022/01/13
- [Guile-commits] 67/85: scm_to_ipv6 uses scm_to_mpz, Andy Wingo, 2022/01/13
- [Guile-commits] 68/85: Bignums avoid both custom GMP allocator and finalizers, Andy Wingo, 2022/01/13
- [Guile-commits] 69/85: take_mpz optimization, Andy Wingo, 2022/01/13
- [Guile-commits] 71/85: Re-rewrite integer-expt in C, Andy Wingo, 2022/01/13
- [Guile-commits] 73/85: Optimize scm_integer_mul_zz., Andy Wingo, 2022/01/13
- [Guile-commits] 84/85: Have log and log10(real nan) return real nan regardless of sign,
Andy Wingo <=
- [Guile-commits] 85/85: Remove dead code in scm_integer_inexact_sqrt_z, Andy Wingo, 2022/01/13
- [Guile-commits] 24/85: Implement scm_logtest with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 13/85: Implement centered-quotient with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 23/85: Implement scm_logxor with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 19/85: Implement gcd with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 57/85: Expose frexp from integers lib, Andy Wingo, 2022/01/13
- [Guile-commits] 59/85: divide2double refactor, Andy Wingo, 2022/01/13
- [Guile-commits] 60/85: Simplify scm_exact_integer_quotient, Andy Wingo, 2022/01/13
- [Guile-commits] 61/85: Remove last non-admin SCM_I_BIG_MPZ uses in numbers.c, Andy Wingo, 2022/01/13
- [Guile-commits] 64/85: Avoid scm_i_mkbig outside numbers.c., Andy Wingo, 2022/01/13