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

Dividing two integers should be round towards zero #65

Open
r888800009 opened this issue Apr 9, 2023 · 0 comments
Open

Dividing two integers should be round towards zero #65

r888800009 opened this issue Apr 9, 2023 · 0 comments

Comments

@r888800009
Copy link

r888800009 commented Apr 9, 2023

Hello, I am trying to bind some existing c programs and libraries to python, and I have a problem when creating an array object. Then I found that the parser did not round towards zero in the integer division, and the array was multiplied by float cause the struct to fail

The following is the current parser result

// Test int div 9 / 2 should be 4
float x3 = 9.0 / 2.0;
int x4 = 9 / 2;
float x5 = 9 / 2;
(4.5, Type('float'))
(4.5, Type('int'))
(4.5, Type('float'))

If you actually test the c program, it should be the following result

#include <stdio.h>
int main()
{
    float x3 = 9.0 / 2.0;
    int x4 = 9 / 2;
    float x5 = 9 / 2;
    printf("%f\n", x3);
    printf("%d\n", x4);
    printf("%f\n", x5);
    return 0;
}
4.500000
4
4.000000

Then I also made some testcases, if necessary r888800009@d02ee92

Also note that python integer division will take floor, but c is rounded towards zero

python3 -9 // 2 = -5
c -9 / 2 = -4

Update1

I've researched the issue, recombine(tok) causes eval to lose type, and currently I think there are two possible ways to do it.
The first one is to avoid recombine and directly processing on AST,
The second is to wrap a class outside the int type to check when division operation.

Both of these methods will inevitably make a lot of modifications to the paser or testcase

@r888800009 r888800009 changed the title dividing two integers should be round towards zero Dividing two integers should be round towards zero Apr 10, 2023
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

1 participant