Skip to content

Commit

Permalink
fix inexact->exact + unit tests #340
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Mar 16, 2024
1 parent b58b1c2 commit a77fdab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions tests/numbers.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a77fdab

Please sign in to comment.