← 输入框|github.com|刷新
Skip to content

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

Open

Description

@faze-geek

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)

Activity

  1. faze-geek commented on Feb 15, 2023

    @faze-geek
    ContributorAuthor

    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)
    
  2. Smit-create commented on Feb 15, 2023

    @Smit-create
    Collaborator

    Thanks, I'll have a look.

  3. self-assigned this
    on Feb 15, 2023
  4. Smit-create commented on Feb 15, 2023

    @Smit-create
    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)
    
  5. faze-geek commented on Feb 15, 2023

    @faze-geek
    ContributorAuthor

    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.

  6. faze-geek commented on Feb 15, 2023

    @faze-geek
    ContributorAuthor

    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.

  7. Smit-create commented on Feb 15, 2023

    @Smit-create
    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?

  8. faze-geek commented on Feb 15, 2023

    @faze-geek
    ContributorAuthor

    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 !

  9. faze-geek commented on Feb 16, 2023

    @faze-geek
    ContributorAuthor

    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 ??)

  10. Smit-create commented on Feb 17, 2023

    @Smit-create
    Collaborator

    That is the output we expect.

  11. faze-geek commented on Feb 17, 2023

    @faze-geek
    ContributorAuthor

    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 !

  12. Smit-create commented on Feb 28, 2023

    @Smit-create
    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.

  13. removed their assignment
    on May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions