An amusing bit of background from the tabletop RPG Paranoia.
RedFeather
@RedFeather
Best posts made by RedFeather
-
Keeping hope alive
-
RE: Self-WTF
Okay, yeah, that must have been a joke. There's no such thing as C13, for one thing. Too early in the morning for me to get humor, I guess.
-
RE: Self-WTF
@flop said:
Don't you know that C13 will require that unsigned can represent -1 as well, exactly to remove that source of errors?
Wait, what, seriously? That's going to cause way more bugs than it prevents. What I really should have done is test whether (i+1) > 0. On another variable type note: looking at the program, there was no real reason for the vector to hold unsigned long longs; there was no plausible way any entry in it could even get into the triple digits.
-
RE: Self-WTF
@alegr said:
Error right here. Must subtract one. That will fail for an empty vector, though.
No, that's intentional. Note that in the next line I add another element to the end.
-
Self-WTF
So I was looking through some old stuff I wrote when I was a student, and came across this gem (carbuncles are gems, right?):
//returns true if it made a step, false if it couldn't do so
bool oneStep(vector<unsigned long long>& baseVec)
{
const unsigned lastEl = baseVec.size();
baseVec.push_back(0);
for (int i = lastEl; i >= 0; i--)
if (baseVec[i] >= 2)
for (unsigned j = i + 1; j <= lastEl; j++)
if (baseVec[j] + 2 <= baseVec[i])
{
baseVec[j]++;
baseVec[i]--;
for (unsigned k = j, l = lastEl; k < l;)
if (baseVec[k] == baseVec[k - 1])
k++;
else if (baseVec[l])
{
baseVec[k]++;
baseVec[l]--;
}
else
l--;
while (!baseVec.back())
baseVec.pop_back();
return true;
}
return false;
}I'm not sure what the worst part is: my apparent allergy to braces, the variable names, the singularly unhelpful comment, the complete lack of any indication what the function actually does, the bizarre way I used repeated ++ and -- rather than adding and subtracting a lump all at once... geez. (I actually remember writing this, a little. In an earlier version, the variable i was unsigned. That is, I was testing an unsigned number to see if it was >= 0. Yeah. That caused some problems.)
Latest posts made by RedFeather
-
Keeping hope alive
An amusing bit of background from the tabletop RPG Paranoia. -
RE: We encouraged people to learn to program...
@blakeyrat said:
Stealing from JWZ?
Stealing from Gabriel Gironda, really. I guess I should have put a link to the source in the post.
-
RE: Fight the Powah
@boomzilla said:
Using a float loses too much precision to handle the full range of
Out of all the problems with that function, that's the one that bothers you?int
s. You really need to use a double. -
RE: Fight the Powah
@boomzilla said:
#include int isPowerOfTwo( int value ){ union ieee754_double d; d.d = (double) value; return !(d.ieee.mantissa0 | d.ieee.mantissa1); }
No, that's far too readable. I had in mind something like:
bool isPowerOfTwo(float value) { return !(*(int *) &value << sizeof(float)*8 - FLT_MANT_DIG + 1); }
Because really, why bother doing it in C++ if you're not going to have incomprehensible pointer idiocy? (This cheats a bit on the one-line rule, in that part of the functionality is included in the signature, which does the actual casting into a float).
ETA: Also, this only works if floats and ints are exactly the same size
-
RE: Fight the Powah
Cast it to a floating point, then extract the mantissa and see if it's zero. [/troll]
-
RE: Self-WTF
@alegr said:
Error right here. Must subtract one. That will fail for an empty vector, though.
No, that's intentional. Note that in the next line I add another element to the end.
-
RE: Self-WTF
Okay, yeah, that must have been a joke. There's no such thing as C13, for one thing. Too early in the morning for me to get humor, I guess.
-
RE: Self-WTF
@flop said:
Don't you know that C13 will require that unsigned can represent -1 as well, exactly to remove that source of errors?
Wait, what, seriously? That's going to cause way more bugs than it prevents. What I really should have done is test whether (i+1) > 0. On another variable type note: looking at the program, there was no real reason for the vector to hold unsigned long longs; there was no plausible way any entry in it could even get into the triple digits.
-
Self-WTF
So I was looking through some old stuff I wrote when I was a student, and came across this gem (carbuncles are gems, right?):
//returns true if it made a step, false if it couldn't do so
bool oneStep(vector<unsigned long long>& baseVec)
{
const unsigned lastEl = baseVec.size();
baseVec.push_back(0);
for (int i = lastEl; i >= 0; i--)
if (baseVec[i] >= 2)
for (unsigned j = i + 1; j <= lastEl; j++)
if (baseVec[j] + 2 <= baseVec[i])
{
baseVec[j]++;
baseVec[i]--;
for (unsigned k = j, l = lastEl; k < l;)
if (baseVec[k] == baseVec[k - 1])
k++;
else if (baseVec[l])
{
baseVec[k]++;
baseVec[l]--;
}
else
l--;
while (!baseVec.back())
baseVec.pop_back();
return true;
}
return false;
}I'm not sure what the worst part is: my apparent allergy to braces, the variable names, the singularly unhelpful comment, the complete lack of any indication what the function actually does, the bizarre way I used repeated ++ and -- rather than adding and subtracting a lump all at once... geez. (I actually remember writing this, a little. In an earlier version, the variable i was unsigned. That is, I was testing an unsigned number to see if it was >= 0. Yeah. That caused some problems.)