From a77fdab4f9c1f27e822916e0c2456cff87ea469b Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Sat, 16 Mar 2024 20:58:39 +0100 Subject: [PATCH] fix inexact->exact + unit tests #340 --- src/lips.js | 12 +++++++----- tests/numbers.scm | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/lips.js b/src/lips.js index 0e70ece1a..1c3b06cbb 100644 --- a/src/lips.js +++ b/src/lips.js @@ -5777,12 +5777,14 @@ LComplex.prototype.serialize = function() { }; // ------------------------------------------------------------------------- LComplex.prototype.toRational = function(n) { - if (LNumber.isFloat(this.__im__) && LNumber.isFloat(this.__re__)) { - const im = LFloat(this.__im__).toRational(n); - const re = LFloat(this.__re__).toRational(n); - return LComplex({ im, re }); + let im = this.__im__, re = this.__re__; + if (LNumber.isFloat(this.__im__)) { + im = LFloat(this.__im__).toRational(n); } - return this; + if (LNumber.isFloat(this.__re__)) { + re = LFloat(this.__re__).toRational(n); + } + return LComplex({ im, re }); }; // ------------------------------------------------------------------------- LComplex.prototype.pow = function(n) { diff --git a/tests/numbers.scm b/tests/numbers.scm index 4ea25d904..1fbf29659 100644 --- a/tests/numbers.scm +++ b/tests/numbers.scm @@ -929,6 +929,30 @@ (t.is (exact->inexact 1/2+10i) 0.5+10.0i) (t.is (exact->inexact 1/2+10.0i) 0.5+10.0i))) +(test "numbers: inexact->exact" + (lambda (t) + (t.is (inexact->exact 0.1) 1/10) + (t.is (inexact->exact 1/10) 1/10) + + (t.is (inexact->exact 0.1+0.1i) 1/10+1/10i) + (t.is (inexact->exact 1/10+1/10i) 1/10+1/10i) + + (t.is (inexact->exact 0.1+1/10i) 1/10+1/10i) + (t.is (inexact->exact 1/10+0.1i) 1/10+1/10i) + + (t.is (inexact->exact 1/10+1/10i) 1/10+1/10i) + (t.is (inexact->exact 1/10+1/10i) 1/10+1/10i) + + (t.is (inexact->exact 0.1+10i) 1/10+10i) + (t.is (inexact->exact 10+0.1i) 10+1/10i) + + (t.is (inexact->exact 1/10+10i) 1/10+10i) + (t.is (inexact->exact 10+1/10i) 10+1/10i) + + (t.is (inexact->exact +0.1i) +1/10i) + (t.is (inexact->exact +10i) +10i) + (t.is (inexact->exact +1/10i) +1/10i))) + (test "numbers: operation exp" (lambda (t) ;; big int