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

+(-1 - 1.i) return (1 +1.i) #9709

Closed
cristian-lsdb opened this issue Aug 29, 2020 · 6 comments · Fixed by #9719
Closed

+(-1 - 1.i) return (1 +1.i) #9709

cristian-lsdb opened this issue Aug 29, 2020 · 6 comments · Fixed by #9719

Comments

@cristian-lsdb
Copy link
Contributor

Adding + in front of a complex number return a new complex with a positive value of the real and imaginary part.

require "complex"
pp! +( 1 + 1.i) # => (1.0 + 1.0i)
pp! +(-1 + 1.i) # => (1.0 + 1.0i)
pp! +( 1 - 1.i) # => (1.0 + 1.0i)
pp! +(-1 - 1.i) # => (1.0 + 1.0i)

This is caused by the instance method Complex#+

In Ruby, Python and C++, +(-1 - 1i) return (-1 - 1i).

@naqvis
Copy link
Contributor

naqvis commented Sep 1, 2020

Complex.+ methods returns the absolute value of self. Believe you are treating or expecting it to behave like normal unary operator on other primitives. Please refer to API documentation

@asterite
Copy link
Member

asterite commented Sep 1, 2020

@naqvis That's how it works but I think + should always return self. I'll fix this now.

@naqvis
Copy link
Contributor

naqvis commented Sep 1, 2020

Thanks @asterite. One question after change though: shouldn't it return a copy instead of returning self?

@asterite
Copy link
Member

asterite commented Sep 1, 2020

It's a struct (a copy is naturally returned) and it's immutable so a copy is never necessary (if it were a class).

@jhass
Copy link
Member

jhass commented Sep 1, 2020

It looks like Complex#+ was added by @Sija through #5440. Was there any reasoning for the current implementation?

@Sija
Copy link
Contributor

Sija commented Sep 1, 2020

@jhass Can't remember.

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

Successfully merging a pull request may close this issue.

5 participants