Skip to content

Commit

Permalink
Issue #530 - First cut at improving sqrt
Browse files Browse the repository at this point in the history
Improving sqrt to properly handle negative parameter values
  • Loading branch information
justinethier committed Mar 12, 2024
1 parent 37b3969 commit fa6213b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
31 changes: 30 additions & 1 deletion scheme/inexact.sld
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
(/ (c-log z1) (c-log z2*)))))
(define-inexact-op c-log "log" "clog")
(define-inexact-op exp "exp" "cexp")
(define-inexact-op sqrt "sqrt" "csqrt")
(define-inexact-op sin "sin" "csin")
(define-inexact-op cos "cos" "ccos")
(define-inexact-op tan "tan" "ctan")
Expand All @@ -93,4 +92,34 @@
(* (if (eqv? y -0.0) -1 1)
(if (eqv? x -0.0) 3.141592653589793 x))
(atan1 (/ y x))))))))

(define-c
sqrt
"(void *data, int argc, closure _, object k, object z)"
" double complex result;
Cyc_check_num(data, z);
if (obj_is_int(z)) {
result = csqrt(obj_obj2int(z));
} else if (type_of(z) == integer_tag) {
result = csqrt(((integer_type *)z)->value);
} else if (type_of(z) == bignum_tag) {
result = csqrt(mp_get_double(&bignum_value(z)));
} else if (type_of(z) == complex_num_tag) {
result = csqrt(complex_num_value(z));
} else {
result = csqrt(((double_type *)z)->value);
}
if (cimag(result) == 0.0) {
make_double(d, creal(result));
return_closcall1(data, k, &d);
} else {
complex_num_type cn;
assign_complex_num((&cn), result);
return_closcall1(data, k, &cn);
} "
; "(void *data, object ptr, object z)"
; " return_inexact_double_or_cplx_op_no_cps(data, ptr, sqrt, csqrt, z);"
)

))
10 changes: 10 additions & 0 deletions tests/base.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

(import
(scheme base)
(scheme inexact)
(cyclone test))


Expand Down Expand Up @@ -102,6 +103,15 @@
(test 2.0 (denominator (inexact (/ 6 4))))
)

(test-group
"sqrt"
(test #t (sqrt -1))
(test #t (sqrt -1.0))
; TODO: (test 2 (sqrt 4))
(test 2.0 (sqrt 4.0))
(test 2i (sqrt -4.0))
)

(test-group
"exact"
(test -1 (exact -1))
Expand Down

0 comments on commit fa6213b

Please sign in to comment.