Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix _mod function to fix the % operator results #1567

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

seifkosbar
Copy link

Closing 1562

Task List:

  • Replaced the _lpython_floordiv function in all the _mod function with _floor function I created based on the math.floor function.

On Branch:

def f():
    print((8)%3)
    print((8)%(3))
    print(-(8)%3)       
    print((8)%-3)          
    print(-(8)%-3)
    print((-8)%3)          
    print(-8%3)            
    
f()

(base) seif@seif:~/Documents/GSoC/lpython$ ./src/bin/lpython examples/try.py
2
2
1
-1
-2
1
1

@@ -212,7 +212,7 @@ struct PythonIntrinsicProcedures {
int64_t a = ASR::down_cast<ASR::IntegerConstant_t>(arg1)->m_n;
int64_t b = ASR::down_cast<ASR::IntegerConstant_t>(arg2)->m_n;
return ASR::down_cast<ASR::expr_t>(
ASR::make_IntegerConstant_t(al, loc, a%b, type));
ASR::make_IntegerConstant_t(al, loc, ((a%b)+b)%b, type));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I have handled the compile time evaluations in my pr #1566 and got reviews on it. Could you please remove it from here to avoid clashes.
Also _mod function in lpython_builtin.py works on runtime evaluations, something like this -

def f():
    a:i32 = 8
    b:i32 = -3
    print(a%b)

f()

I hope you are aware of that, since all the test cases you refer in the OP are not in that format. Checkout if outputs of the print(a%b) format are actually getting corrected by the changes you made in lpython_builtin.py . On face value I can see that the below case is handled incorrectly -

def f():
    a:i32 = 8
    b:i32 = 0
    print(a%b)

f()
(lp) C:\Users\kunni\lpython>python try.py
Traceback (most recent call last):
  File "C:\Users\kunni\lpython\try.py", line 6, in <module>
    f()
  File "C:\Users\kunni\lpython\try.py", line 4, in f
    print(a%b)
ZeroDivisionError: integer division or modulo by zero

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

@seifkosbar
Copy link
Author

My bad I didn't see your PR, I removed it.

However, thank you for letting me know about the format of the test case. The test case in the OP is the one used in #1562 so I was just trying to test against it again.

I did some edits to handle the case mentioned in your comment:

def f():
    a:i32 = 8
    b:i32 = 0
    print(a%b)

f()

I made _mod function to raise ZeroDivisionError if the divisor is 0. Also, I got the _lpython_floordiv back as the _floor function made the tests to fail.

@overload
def _mod(a: i32, b: i32) -> i32:
    if b == i32(0):
        raise ZeroDivisionError("Integer division or modulo by zero not possible")
    return a - _lpython_floordiv(a, b)*b

I got this:

(lp) seif@seif:~/Documents/GSoC/lpython$ ./src/bin/lpython examples/try.py
ERROR STOP

I am not sure yet why it is behaving like this but I am looking into it.

@faze-geek
Copy link
Contributor

I got this:

(lp) seif@seif:~/Documents/GSoC/lpython$ ./src/bin/lpython examples/try.py
ERROR STOP

I am not sure yet why it is behaving like this but I am looking into it.

Yes, a bit strange. I got the same message on trying out your diff. @Thirumalai-Shaktivel I had pointed this out on zulip recently, Can you look into it !?

Copy link
Collaborator

@czgdp1807 czgdp1807 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test in integration_tests folder for this.

@czgdp1807 czgdp1807 marked this pull request as draft March 9, 2023 17:26
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 this pull request may close these issues.

3 participants