🔒 Locked
C++ question
Advertisement
Original Post
Hi guys, what does the following expression ? result = (float) nX + ( ( nTri == 1 || nTri == 2 || nTri == 5 ) ? 1 : 0.0f ); Thanks !
Adds one to nX if there are 1, 2 or 5 triangles.
e1 ? e2 : e3 means this:
If e1 is true, then e2, else e3. So look at the following:
bool flag = false;
int x = flag ? 42 : 3;
This means if flag is true, then 42, else 3. So in that example, x = 3 since flag is false, so 3 is returned from the boolean expression.
e1 ? e2 : e3 is the ternary operator in C++. MSDN documentation.
If e1 is true, then e2, else e3. So look at the following:
bool flag = false;
int x = flag ? 42 : 3;
This means if flag is true, then 42, else 3. So in that example, x = 3 since flag is false, so 3 is returned from the boolean expression.
e1 ? e2 : e3 is the ternary operator in C++. MSDN documentation.
Quote:
Original post by DevFred
If nTri is guaranteed to always be less than 32, you can make the code even shorter:result = nX + ((38 >> nTri) & 1);
If you wonder where the magic number 38 comes from, it's 32 + 4 + 2 = 2^5 + 2^2 + 2^1.
Even if you know where the magic number comes from, do not use this. There's nothing to gain in making this code "shorter", we just make it harder to understand.
Why are 1, 2 and 5 triangles special cases? I would try to find out, give it a name and make a function.
result = nX + is_number_of_triangles_special(nTri);
The implementation of is_number_of_triangles_special can use whatever trick you want, but make sure to include a good comment explaining what it does, and why 1, 2 and 5 are special.
result = nX + is_number_of_triangles_special(nTri);
The implementation of is_number_of_triangles_special can use whatever trick you want, but make sure to include a good comment explaining what it does, and why 1, 2 and 5 are special.
Quote:
Original post by summaky Quote:
Original post by DevFred
If nTri is guaranteed to always be less than 32, you can make the code even shorter:result = nX + ((38 >> nTri) & 1);
If you wonder where the magic number 38 comes from, it's 32 + 4 + 2 = 2^5 + 2^2 + 2^1.
Even if you know where the magic number comes from, do not use this. There's nothing to gain in making this code "shorter", we just make it harder to understand.
I would go with the shorter one if performance was an issue, but also supply a comment with the original expression to clarify its functionality.
Quote:And Zahlman pulls into an early lead for the 2009 edition of 'most evil perversion of C++ syntax' [wink]
Original post by Zahlman
I'm bored.
*** Source Snippet Removed ***
Quote:
Original post by rip-off Quote:
Original post by someboddy Quote:
Original post by Zahlman
I'm bored.
*** Source Snippet Removed ***
Shouldn't "in" be a class instead of a struct?
Why? You are aware of the differences between classes and structs in C++, yes?
That classes can have functions that automatically get a reference to the object, and structs can't?
Quote:
That classes can have functions that automatically get a reference to the object, and structs can't?
Sure they can. The only difference between a struct and a class in C++ is default visibility. A struct's members are public by default, while a classes are private. When using inheritance, a struct defaults to public inheritance and a class to private. A struct can have member functions, constructors and a destructor etc.
Quote:
Original post by SiCrane Quote:
Original post by swiftcoder
And Zahlman pulls into an early lead for the 2009 edition of 'most evil perversion of C++ syntax' [wink]
At least he didn't overload operator , this time.
Be careful what you wish for? :s
// variadic macro support required. Removed the 'is' to match Python syntax.#define in(x) + (IN(), __VA_ARGS__)struct IN { int bitset; IN(int initial): bitset(1 << initial) {} // Ack, I forgot to 'return *this;' in the previous version and noone - // oh, nm, well spotted, iMalc :) IN& operator,(int next) { bitset |= 1 << next; return *this; }};bool operator+(int lhs, const IN& rhs) { return (rhs.bitset >> lhs) & 1;}result = nX + float(nTri in(1, 2, 5));Quote:Gaaa my eyes! Do you really ever write code like that in your programs?
Original post by Zahlman
Be careful what you wish for? :s
*** Source Snippet Removed ***
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement