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

Translate (2>3)-1 to python #5

Open
BenShen98 opened this issue Feb 21, 2019 · 3 comments
Open

Translate (2>3)-1 to python #5

BenShen98 opened this issue Feb 21, 2019 · 3 comments

Comments

@BenShen98
Copy link

So in C we could write code such as below, which is syntactically valid, but somewhat meaningless.

printf("%x",(2>3)-1);

In spec for C, it does say

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >=
(greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.
The result has type int.

As result of this do we need translate such exprssion to python?
It can't be done by just print (2>3)-1, as 2>3 returns nontype

@ymherklotz
Copy link
Member

ymherklotz commented Feb 21, 2019

One possible solution to this could be to wrap all expressions, or just the expressions outputting a Bool in python, with int(...). I believe that would still work in if statements too:

if int(5 < 3):
   ... 

And would convert True to 1 and False to 0 in expressions.

@ymherklotz
Copy link
Member

Actually, I think that the problem is that you are using python 3 with python 2 syntax. If you try:

print((2>3)-1)

It should convert the Bool to int automatically and print the correct answer.

Your print statement works fine for me on python 2. In python 3 it is seeing the print(2>3) call and is trying to add the result None to 1.

@BenShen98
Copy link
Author

Actually, I think that the problem is that you are using python 3 with python 2 syntax. If you try:

print((2>3)-1)

It should convert the Bool to int automatically and print the correct answer.

Your print statement works fine for me on python 2. In python 3 it is seeing the print(2>3) call and is trying to add the result None to 1.

Yes, that's the problem, thanks

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