Help understanding ternary statement in py/objcomplex.c

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
BrendanSimon
Posts: 33
Joined: Wed Sep 07, 2016 10:46 am

Help understanding ternary statement in py/objcomplex.c

Postby BrendanSimon » Fri Feb 03, 2017 10:39 am

I just saw this change in `py/objcomplex.c` (line 225). I have no idea what the ternary statement is supposed to do. Is it a bug or some unusual use of the conditional operator?

Code: Select all

-                if (rhs_imag == 0) {
-                    lhs_real = 1;
+                if (rhs_imag == 0 && rhs_real >= 0) {
+                    lhs_real = 1 ? rhs_real == 0 : 0;


Doesn' the first expression `1` always evaluate to true, then the second expression is always executed?

What is the purpose of the third expression `0` ?

Is this equivalent to `lhs_real = true` or `lhs = false`, depending on the value of rhs_real ?

If so, why wouldn't it be written as: `lhs_real = (rhs == 0) ;

Is this some fancy way of assigning a boolean or an integer (by some magic) ?

And I thought I knew C ?


User avatar
deshipu
Posts: 1036
Joined: Thu May 28, 2015 5:54 pm

Re: Help understanding ternary statement in py/objcomplex.c

Postby deshipu » Fri Feb 03, 2017 10:58 am

Looking closer, it looks like a bug. Someone probably got confused with Python's "foo = bar if baz else boo" and got this wrong.

BrendanSimon
Posts: 33
Joined: Wed Sep 07, 2016 10:46 am

Re: Help understanding ternary statement in py/objcomplex.c

Postby BrendanSimon » Fri Feb 03, 2017 11:20 am

That's what I thought too -- that it was a Python ternary statement, but then double checked that it was a C file.

But the code I've seen in MicroPython looks very well written, so I assumed it was correct and purposeful (but even the really good programmers are fallible)

I also found this on stackoverflow (trying to find an answer) and it has a similar syntax, but it is about the type returned if both 2nd and 3rd expressions are pointers, which doesn't seem to apply in this case.

http://stackoverflow.com/questions/1409 ... ypecasting

Hopefully it's a bug, as it certainly isn't very clear (at least to me)

stijn
Posts: 135
Joined: Thu Apr 24, 2014 9:13 am

Re: Help understanding ternary statement in py/objcomplex.c

Postby stijn » Fri Feb 03, 2017 1:05 pm

*edit* not sure if what I wrote was even correct and I can't be bother to lookup the standard rules atm
Last edited by stijn on Fri Feb 03, 2017 1:51 pm, edited 1 time in total.

User avatar
pythoncoder
Posts: 1564
Joined: Fri Jul 18, 2014 8:01 am

Re: Help understanding ternary statement in py/objcomplex.c

Postby pythoncoder » Fri Feb 03, 2017 1:22 pm

Agreed. It looks like an issue that should be raised on GitHub.

[EDIT]I see it was, a few minutes ago.
Last edited by pythoncoder on Fri Feb 03, 2017 1:25 pm, edited 1 time in total.
Peter Hinch

User avatar
deshipu
Posts: 1036
Joined: Thu May 28, 2015 5:54 pm

Re: Help understanding ternary statement in py/objcomplex.c

Postby deshipu » Fri Feb 03, 2017 1:24 pm

I think it was supposed to be:

Code: Select all

if (rhs_real == 0) {
    lhs_real = 1;
} else {
    lhs_real = 0;
}


Which could of course be written much shorter:

Code: Select all

lhs_real = !rhs_real;



Return to “Programs, Libraries and Tools”

Who is online

Users browsing this forum: No registered users and 2 guests