From 7cb15a71912e8ed5937b10567c36f571cc93b143 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sun, 5 Jun 2016 22:25:18 +0900 Subject: [PATCH] Handling exact zero imaginary parts in complex asin. Fixes issue #350. --- bignum.c | 16 ++++++++++------ tests/r7rs-tests.scm | 3 +++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/bignum.c b/bignum.c index fc7e3624..6478c5de 100644 --- a/bignum.c +++ b/bignum.c @@ -1023,17 +1023,21 @@ sexp sexp_complex_asin (sexp ctx, sexp z) { sexp_gc_preserve2(ctx, res, tmp); res = sexp_complex_mul(ctx, z, z); tmp = sexp_make_complex(ctx, SEXP_ONE, SEXP_ZERO); - res = sexp_complex_sub(ctx, tmp, res); - res = sexp_complex_sqrt(ctx, res); + res = sexp_sub(ctx, tmp, res); + res = sexp_sqrt(ctx, NULL, 1, res); /* tmp = iz */ sexp_complex_real(tmp) = sexp_complex_imag(z); sexp_negate(sexp_complex_real(tmp)); sexp_complex_imag(tmp) = sexp_complex_real(z); - res = sexp_complex_add(ctx, tmp, res); - tmp = sexp_complex_log(ctx, res); + res = sexp_add(ctx, tmp, res); + tmp = sexp_log(ctx, NULL, 1, res); /* res = -i*tmp */ - res = sexp_complex_copy(ctx, tmp); - sexp_negate(sexp_complex_imag(res)); + if (sexp_complexp(tmp)) { + res = sexp_complex_copy(ctx, tmp); + sexp_negate(sexp_complex_imag(res)); + } else { + res = tmp; + } sexp_gc_release2(ctx); return res; } diff --git a/tests/r7rs-tests.scm b/tests/r7rs-tests.scm index 5752222d..1c93c278 100644 --- a/tests/r7rs-tests.scm +++ b/tests/r7rs-tests.scm @@ -848,6 +848,9 @@ (test 0.0 (inexact (acos 1))) ;; may return exact number (test 3.14159265358979 (acos -1)) +(test 0.0 (asin 0+0.0i)) +(test 1.5707963267948966 (acos 0+0.0i)) + (test 0.0 (atan 0.0 1.0)) (test -0.0 (atan -0.0 1.0)) (test 0.785398163397448 (atan 1.0 1.0))