Skip to content

Commit

Permalink
fix comparing complex numbers #248
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 15, 2024
1 parent b87624f commit 07e62ac
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* fix `scheme-report-environment` [#268](https://github.com/jcubic/lips/issues/268)
* fix writing to binary ports
* fix unboxing object literals
* throw error on comparing complex numbers [#248](https://github.com/jcubic/lips/issues/248)

## 1.0.0-beta.17
### Breaking
Expand Down
50 changes: 22 additions & 28 deletions dist/lips.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/lips.esm.min.js

Large diffs are not rendered by default.

50 changes: 22 additions & 28 deletions dist/lips.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/lips.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import nodeResolve from "@rollup/plugin-node-resolve";
import babel from "@rollup/plugin-babel";
import nodePolyfills from 'rollup-plugin-node-polyfills';


function base() {
return {
input: "dist/version.js",
Expand Down Expand Up @@ -39,6 +38,7 @@ function base() {
]
};
}

export default [
{
output: {
Expand Down
33 changes: 18 additions & 15 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -5642,15 +5642,7 @@ LComplex.prototype._op = function(op, n) {
};
// -------------------------------------------------------------------------
LComplex.prototype.cmp = function(n) {
const [a, b] = this.coerce(n);
const [re_a, re_b] = a.__re__.coerce(b.__re__);
const re_cmp = re_a.cmp(re_b);
if (re_cmp !== 0) {
return re_cmp;
} else {
const [im_a, im_b] = a.__im__.coerce(b.__im__);
return im_a.cmp(im_b);
}
throw new Error(`Can't compare compex numbers`);

Check failure on line 5645 in src/lips.js

View workflow job for this annotation

GitHub Actions / Check for spelling errors

compex ==> complex
};
// -------------------------------------------------------------------------
LComplex.prototype.valueOf = function() {
Expand Down Expand Up @@ -9414,31 +9406,31 @@ var global_env = new Environment({
all equal.`),
// ------------------------------------------------------------------
'>': doc('>', function(...args) {
typecheck_args('>', args, 'number');
typecheck_numbers('>', args, ['bigint', 'float', 'rational']);
return seq_compare((a, b) => LNumber(a).cmp(b) === 1, args);
}, `(> x1 x2 x3 ...)
Function that compares its numerical arguments and checks if they are
monotonically decreasing, i.e. x1 > x2 and x2 > x3 and so on.`),
// ------------------------------------------------------------------
'<': doc('<', function(...args) {
typecheck_args('<', args, 'number');
typecheck_numbers('<', args, ['bigint', 'float', 'rational']);
return seq_compare((a, b) => LNumber(a).cmp(b) === -1, args);
}, `(< x1 x2 ...)
Function that compares its numerical arguments and checks if they are
monotonically increasing, i.e. x1 < x2 and x2 < x3 and so on.`),
// ------------------------------------------------------------------
'<=': doc('<=', function(...args) {
typecheck_args('<=', args, 'number');
typecheck_numbers('<=', args, ['bigint', 'float', 'rational']);
return seq_compare((a, b) => [0, -1].includes(LNumber(a).cmp(b)), args);
}, `(<= x1 x2 ...)
Function that compares its numerical arguments and checks if they are
monotonically nondecreasing, i.e. x1 <= x2 and x2 <= x3 and so on.`),
// ------------------------------------------------------------------
'>=': doc('>=', function(...args) {
typecheck_args('>=', args, 'number');
typecheck_numbers('>=', args, ['bigint', 'float', 'rational']);
return seq_compare((a, b) => [0, 1].includes(LNumber(a).cmp(b)), args);
}, `(>= x1 x2 ...)
Expand Down Expand Up @@ -9747,11 +9739,22 @@ function typeErrorMessage(fn, got, expected, position = null) {
expected = 'a' + ('aeiou'.includes(first) ? 'n ' : ' ') + expected[0];
} else {
const last = expected[expected.length - 1];
expected = expected.slice(0, -1).join(', ') + ' or ' + last;
expected = expected.slice(0, -1).join(', ') + ', or ' + last;
}
}
return `Expecting ${expected}, got ${got}${postfix}`;
return `Expecting ${expected} got ${got}${postfix}`;
}

// -------------------------------------------------------------------------
function typecheck_numbers(fn, args, expected) {
args.forEach((arg, i) => {
typecheck(fn, arg, 'number', i + 1);
if (!expected.includes(arg.__type__)) {
throw new Error(typeErrorMessage(fn, arg.__type__, expected, i));
}
});
}

// -------------------------------------------------------------------------
function typecheck_args(fn, args, expected) {
args.forEach((arg, i) => {
Expand Down

0 comments on commit 07e62ac

Please sign in to comment.