Skip to content

Integers do not get casted in operations with complex variables #1524

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

Open
faze-geek opened this issue Feb 15, 2023 · 11 comments
Open

Integers do not get casted in operations with complex variables #1524

faze-geek opened this issue Feb 15, 2023 · 11 comments

Comments

@faze-geek
Copy link
Contributor

faze-geek commented Feb 15, 2023

While playing with the complex module I saw certain expressions giving incorrect results .In Python, when an integer is operated with objects of complex() class the integer is implicitly casted to a complex variable with 0 imaginary value.

y = complex(5)+100
print(y)
(lp) C:\Users\kunni\lpython>python try.py
(105+0j)

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
(5.000000,0.000000)
y = complex(5)*10
print(y)
(lp) C:\Users\kunni\lpython>python try.py
(50+0j)

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
(0.000000,0.000000)
y = c32(5)-10
print(y)
(lp) C:\Users\kunni\lpython>python try.py
(-5+0j)

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
(5.000000,-0.000000)
@faze-geek
Copy link
Contributor Author

All the operations work correctly with float variables -

def main1():
    y:c32
    y = c32(5)*1.5
    print(y)
    y = c32(5)+1.5
    print(y)
    y = complex(5)-1.5
    print(y)
    y = c32(5)/1.5
    print(y)


main1()

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
(7.500000,0.000000)
(6.500000,0.000000)
(3.500000,-0.000000)
(3.333333,0.000000)

@Smit-create
Copy link
Collaborator

Thanks, I'll have a look.

@Smit-create Smit-create self-assigned this Feb 15, 2023
@Smit-create
Copy link
Collaborator

Are you trying this on the main branch?

It works fine for me:

from ltypes import c32

def f():
    y: c32
    y = complex(5)+c32(100)
    print(y)
    y = complex(5)*c32(10)
    print(y)

f()

LPython:

% lpython a.py
(105.000000,0.000000)
(50.000000,0.000000)

CPython:

% PYTHONPATH=$PWD/src/runtime/ltypes python a.py 
(105+0j)
(50+0j)

@faze-geek
Copy link
Contributor Author

Are you trying this on the main branch?
It works fine for me:

Yes, I cannot confirm right now but I think the example you are writing worked with the main branch for me as well, it's when you do complex(5) + 100 when the result is incorrect.

@faze-geek
Copy link
Contributor Author

Could you run the exact examples which I have given in the issue and recheck ? I had typed y: c32 above, rest everything is shown.

@Smit-create
Copy link
Collaborator

That throws some other error:

from ltypes import c32

def f():
    y: c32
    y = complex(5)+100
    print(y)

f()

Output:

case exprType::BinOp: { v.visit_BinOp((const BinOp_t &)x); return; }
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 2813
    }
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 2091
    ASRUtils::expr_value(right));
  File "/Users/thebigbool/repos/lpython/src/libasr/asr.h", line 40
    LCOMPILERS_ASSERT(is_a<T>(*f));
AssertFailed: is_a<T>(*f)

Can you provide the exact snippet you tried and also please make sure you are using main branch?

@faze-geek
Copy link
Contributor Author

Can you provide the exact snippet you tried and also please make sure you are using main branch?

Sure I'll do that soon . Thankfully the snippet leads to an error only, so raising this issue wasn't a waste !

@faze-geek
Copy link
Contributor Author

Yes those above results were run on the main branch. I checked now.
After pulling changes from #1525 I get the following -

def main1():
    y:c32
    y = c32(5)*1
    print(y)

main()

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
semantic error: Type mismatch in binary operator; the types must be compatible
 --> try.py:3:9
  |
3 |     y = c32(5)*1
  |         ^^^^^^ ^ type mismatch (c32 and i32)

And same incompatibility is shown by all binary operators. Can someone confirm this once ? I am using a windows machine
(maybe the cause of different output earlier ??)

@Smit-create
Copy link
Collaborator

That is the output we expect.

@faze-geek
Copy link
Contributor Author

faze-geek commented Feb 17, 2023

That is the output we expect.

Hey @Smit-create
I still have my doubts. Shouldn't the output be inline with that of Cpython ?

y = complex(5)+100
print(y)
(lp) C:\Users\kunni\lpython>python try.py
(105+0j)

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
(5.000000,0.000000)
y = complex(5)*10
print(y)
(lp) C:\Users\kunni\lpython>python try.py
(50+0j)

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
(0.000000,0.000000)

If it is still what is expected I'll close this issue soon. Thanks !

@Smit-create
Copy link
Collaborator

I still have my doubts. Shouldn't the output be inline with that of Cpython ?

Can you please provide the complete python snippet you are trying so that I can reproduce the same results? I am unable to get these results on the main branch.

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