fix sqrt of complex negative zero (issue #785)

This commit is contained in:
Alex Shinn 2021-11-07 09:55:24 +09:00
parent bf881b3e61
commit 1f0f07114b
2 changed files with 2 additions and 1 deletions

View file

@ -1110,7 +1110,7 @@ sexp sexp_complex_sqrt (sexp ctx, sexp z) {
r = sqrt(x*x + y*y);
res = sexp_make_complex(ctx, SEXP_ZERO, SEXP_ZERO);
sexp_complex_real(res) = sexp_make_flonum(ctx, sqrt((x+r)/2));
sexp_complex_imag(res) = sexp_make_flonum(ctx, ((y<0||(y==0&&1/y<0))?-1:1)*sqrt((-x+r)/2));
sexp_complex_imag(res) = sexp_make_flonum(ctx, ((y<-0.0)?-1:1)*sqrt((-x+r)/2));
sexp_gc_release1(ctx);
return res;
}

View file

@ -994,6 +994,7 @@
(test 3.0 (inexact (sqrt 9)))
(test 1.4142135623731 (sqrt 2))
(test 0.0+1.0i (inexact (sqrt -1)))
(test 0.0+1.0i (sqrt -1.0-0.0i))
(test '(2 0) (call-with-values (lambda () (exact-integer-sqrt 4)) list))
(test '(2 1) (call-with-values (lambda () (exact-integer-sqrt 5)) list))