I dont suppose there's any way to do logical and/or in C without coercing the result to 0 or 1?
I.e. `0 || 5` would result in 5, not 1
I dont suppose there's any way to do logical and/or in C without coercing the result to 0 or 1?
I.e. `0 || 5` would result in 5, not 1
It's so irritating that something as simple as logical and/or isn't the same in different languages :/
In some languages 0 or 5 is 1, in others 0 or 5 is 5
@eniko Sorry "I am TOO OLD SKOOL" ( and mostly C/C++/ASM only ) .. WHAT LANGUAGE on Earth has a 0 || 5 = 5 ??! 😱
Suppose I could make an inline function like
int or(int x, int y) {
if (x) return x;
return y;
}
ok i think this will work even if nested, without statement expressions (so will compile on MSVC)
ORV(x, y) (tmp = (x) ? tmp : (y))
ANDV(x, y) (tmp = (x) ? (y) : tmp)
the AND version felt dicey but i realized that if you get to the (y) expression the tmp value is never evaluated so it's fine if it gets clobbered
@eniko If you do you own function, why don't use if/then/else instead of 'risking' with the ? thing ? .. At this point I won't change too much I suppose.
@eniko so this is neat, but I'm mostly curious if you're able to provide a bit more insight into what you're using this for
I've gotten super Rust-brained lately so I'm having trouble understanding the benefits of mixing up integers and booleans like this
@clarfonthey i'm developing an IR for a virtual machine and when i was making and/or yesterday i was just treating zero as false and anything else as true
so `0 or 5` is 5, because 5 is true
then i realized that's inconvenient if you wanna do like `(a && b) == (c or d)` because im not coercing truthy values to 1 so this wouldn't work
i also kinda wanna make this transpilable to human-readable C and realized C's logical operators always coerce the result to 0 or 1 (likely for exactly this reason)
then i realized other languages like lua and javascript actually do it kinda the way i was doing it and i realized i kinda wanna support being able to do it both ways
but if i want value selecting logical operators, and C only has boolean logical operators, and i wanna transpile to C, then i have to find a way to make those work in C
@gilesgoat because you can't do if/then/else inline in an expression
@gilesgoat @eniko JavaScript 😬
@gilesgoat @eniko many languages say that "a or b" is the first "truthy" of the two expressions. What "falsy/truthy" is, is a different can of worms.
@Xarizzar @gilesgoat and lua
@Xarizzar @gilesgoat @eniko That is probably the most annoying feature of JavaScript I ever experienced. I am old-school and learned that "0 || 5 == 1 (true)", as defined by Boolean Algebra, but then I saw how it was used in some JavaScript-code and my brain exploded.
Using it instead of "0? 0:5" is really annoying, especially since JS has the "?"-op.
@gilesgoat To be fair, at that point, just use parenthesis, and/or maybe actual if statements, no? Sometimes programming is a work of art but damn if we all don't need to be a little bit more clear in our code at times, hahaha
@Xarizzar in other words KISS ( Keep It Simple and Stupid ) and don't use "exclusive features" that only some languages have ? 🙂
@eniko @Xarizzar @gilesgoat and python, and perl, and pretty much any of the "lisp but with syntax" languages.
@deshipu @eniko @Xarizzar At this point better those STRONG IMPERATIVE languages a la' VHDL/PASCAL where if you do a thing like NUMERIC OR NUMERIC the result is NUMERIC ( i.e. bitwise OR ) BOOLEAN OR BOOLEAN and result can be only TRUE or FALSE (0/1) and you CAN NOT do a thing like NUMERIC OR BOOLEAN ( or else ) unless you cast types so they are the same so "(BOOLEAN) a OR b" or instead "a OR (NUMERIC) b" and if b is BOOLEAN its cast can only be numerically 0 or 1 ( or something like that ).
@gilesgoat don't shout