Re:1/0 = 0
by cyphar on Friday August 10, 2018 @09:48PM
(#17737855)
It is, but it will always give you a divisor of 1 (because "true" is 1 -- I also thought it would work until I tested it):
In Python this is not the case:
[ Parent
| on HN ]
Some trivia: GNU extensions to C/C++ include a "?:" operator that does what you'd want in this case, e.g.
x / (divisor ?: 1)
Re:1/0 = 0
by user5994461 on Saturday August 11, 2018 @06:09AM
(#17739103)
It shouldn't be valid in C++. booleans cannot participate in arithmetic operations. You will get a warning from the compiler if you are lucky.
C doesn't have booleans and treat them as integers 0 or 1, it can do the math and will always return 1.
That's not true. C++ does define an implicit conversion from bool to int:
https://en.cppreference.com/w/cpp/language/implicit_conversi... (under "Integral promotion")
And C has a boolean type as of C99.
What do you expect for that C code?