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

[math] TComplex arithmetic operators don't work with integral types #13730

Closed
meiyasan opened this issue Sep 27, 2023 · 7 comments · Fixed by #13746
Closed

[math] TComplex arithmetic operators don't work with integral types #13730

meiyasan opened this issue Sep 27, 2023 · 7 comments · Fixed by #13746

Comments

@meiyasan
Copy link

meiyasan commented Sep 27, 2023

Feature description

Hello,

This sounds obvious but, can we hope to have some cast compatibility with int and float for TComplex class ?
Just typing the following line is failing: 2*TComplex(2,2)
but 2.0*TComplex(2,2) is fine.

I dont think this would be a lot of work, but very helpful to use.

Cheers,
M.

@couet
Copy link
Member

couet commented Sep 27, 2023

I confirm. On Mac we get:

root [0] 2.*TComplex(2,2)
(TComplex) (4,4i)

root [1] 2*TComplex(2,2)
ROOT_prompt_1:1:2: error: use of overloaded operator '*' is ambiguous (with operand types 'int' and 'TComplex')
2*TComplex(2,2)
~^~~~~~~~~~~~~~
/Users/couet/git/couet-root-bin/include/TComplex.h:88:20: note: candidate function
   friend TComplex operator *(Double_t d, const TComplex & c)
                   ^
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, float)
2*TComplex(2,2)
 ^
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, double)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, long double)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, int)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, long)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, long long)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, __int128)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, unsigned int)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, unsigned long)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, unsigned long long)
ROOT_prompt_1:1:2: note: built-in candidate operator*(int, unsigned __int128)
root [2] 

@vepadulano
Copy link
Member

For my own curiosity, what is the advantage of using TComplex over std::complex?

guitargeek added a commit to guitargeek/root that referenced this issue Sep 28, 2023
Generalize the arithmetic operator overloads of TComplex to general
arithmetic types with templates.

Closes root-project#13730.
@guitargeek
Copy link
Contributor

Probably compatibility with other ROOT interfaces. But indeed I don't think we need to support this TComplex class that much. I have opened a PR to address this issue though, so it can be closed.

guitargeek added a commit to guitargeek/root that referenced this issue Sep 28, 2023
Generalize the arithmetic operator overloads of TComplex to general
arithmetic types with templates.

I'm using some `enable_if` for arithmetic types only, so we can be sure
that nothing unexpected will happen for non-arithmetic types that
implement arithmetic operators with `double` themselves.

Closes root-project#13730.
@meiyasan
Copy link
Author

Thank you I will follow your thread.

@guitargeek
Copy link
Contributor

You're welcome! Let's keep this issue open as long as the PR is not merged though :)

@guitargeek guitargeek reopened this Sep 28, 2023
@guitargeek guitargeek assigned guitargeek and unassigned lmoneta Sep 28, 2023
@guitargeek guitargeek changed the title TComplex [math] TComplex arithmetic operators don't work with integral types Sep 28, 2023
@Axel-Naumann
Copy link
Member

Alternatively and / or additionally it might make a lot of sense to remove the lines

   operator Float_t  () const {return static_cast<Float_t>(fRe);}
   operator Int_t    () const {return static_cast<Int_t>(fRe);}

and keep only the conversion to double.

@guitargeek
Copy link
Contributor

I agree that this should be considered in the future. Implicit type conversions always come back to bite you at some point and should be forbidden 🙂

guitargeek added a commit that referenced this issue Oct 2, 2023
Generalize the arithmetic operator overloads of TComplex to general
arithmetic types with templates.

I'm using some `enable_if` for arithmetic types only, so we can be sure
that nothing unexpected will happen for non-arithmetic types that
implement arithmetic operators with `double` themselves.

Closes #13730.
maksgraczyk pushed a commit to maksgraczyk/root that referenced this issue Jan 12, 2024
Generalize the arithmetic operator overloads of TComplex to general
arithmetic types with templates.

I'm using some `enable_if` for arithmetic types only, so we can be sure
that nothing unexpected will happen for non-arithmetic types that
implement arithmetic operators with `double` themselves.

Closes root-project#13730.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants