Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIPS compares complex numbers for order #248

Closed
jpellegrini opened this issue Aug 18, 2023 · 3 comments
Closed

LIPS compares complex numbers for order #248

jpellegrini opened this issue Aug 18, 2023 · 3 comments

Comments

@jpellegrini
Copy link

Hello,
It seems that LIPS accepts <, >, <=, >= for complex numbers:

lips> (< 1+1i 2+2i)
#t
lips> (< 2+2i 1+1i)
#f
lips> (< 1+2i 2+1i)
#t
lips> (< 1+2i 0)
#f
@jpellegrini
Copy link
Author

(Does it implement a partial order? Then, does it use the norm or is it a lexicographic order? It would then be nice to document this -- or perhaps not allow the comparison)

@jcubic
Copy link
Collaborator

jcubic commented Aug 18, 2023

Yes, I think that it should throw an error for comparing complex numbers. I was asking (on StackOverflow or Math Exchange) about comparing complex numbers and long ago someone replied that it doesn't make sense to compare them.

I think that the reason why comparison works is because all operators use polymorphism so every number type gets those for free even if the result doesn't make sense.

jcubic added a commit that referenced this issue Jan 15, 2024
@jcubic
Copy link
Collaborator

jcubic commented Jan 15, 2024

Added type checking to the compare operations. Also, the comparison was implemented for complex numbers so I've deleted it and thrown an error instead.

It looked like this:

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);
    }
};

It was a naive complex comparison that I'd implemented before I knew that it didn't make sense to compare complex numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants