From 7dc1f9e1792f04cfa9ad3968ee3fd2ee80cfa1eb Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 11 Sep 2023 19:02:28 -0700 Subject: [PATCH] Issue #510 - Exact support for complex nums Allow `exact` to properly handle complex numbers --- include/cyclone/runtime.h | 11 +++++++++-- tests/base.scm | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/cyclone/runtime.h b/include/cyclone/runtime.h index a97414ec..508702ad 100644 --- a/include/cyclone/runtime.h +++ b/include/cyclone/runtime.h @@ -517,7 +517,10 @@ int Cyc_have_mstreams(); } else if (type_of(z) == bignum_tag) { \ return_closcall1(data, cont, z); \ } else if (type_of(z) == complex_num_tag) { \ - return_closcall1(data, cont, z); \ + double dreal = OP(creal(((complex_num_type *) z)->value)); \ + double dimag = OP(cimag(((complex_num_type *) z)->value)); \ + make_complex_num(num, dreal, dimag); \ + return_closcall1(data, cont, &num); \ } else { \ double d = ((double_type *)z)->value; \ if (isnan(d)) { \ @@ -548,7 +551,11 @@ int Cyc_have_mstreams(); } else if (type_of(z) == bignum_tag) { \ return z; \ } else if (type_of(z) == complex_num_tag) { \ - return z; \ + double dreal = OP(creal(((complex_num_type *) z)->value)); \ + double dimag = OP(cimag(((complex_num_type *) z)->value)); \ + double complex unboxed = dreal + (dimag * I); \ + assign_complex_num(ptr, unboxed); \ + return ptr; \ } else { \ double d = ((double_type *)z)->value; \ if (isnan(d)) { \ diff --git a/tests/base.scm b/tests/base.scm index e3e4f5fe..63e6dd91 100644 --- a/tests/base.scm +++ b/tests/base.scm @@ -65,6 +65,7 @@ (test -1 (exact -1.0)) (test -1 (exact -1.1)) (test -1 (exact -1.1)) + (test 1.0+1.0i (exact 1.1+1.2i)) (test #t (bignum? (exact 111111111111111111111111111.0))) (test #t (bignum? (exact -111111111111111111111111111.0))) ;(test +inf.0 (exact +inf.0))