|
Influenced by Pascal...
-
04-19-2008 10:39 AM
|
|
-
Steven V


- Joined on 04-13-2008
- Boston, MA
- Posts 3
|
I never had a problem asking for help with code I didn't understand.
I really wish I hadn't this time...
When looking at some very old code to set the location of a graphical annotation, I ended up talking to one of our senior developers.
The conversation:
"Oh, yeah. I remember the guy who wrote that. See--here's the thing...He has a very...different style of coding. I like it, granted, but it's definitely different. I think he was influenced by Pascal. He likes to flip things around back and forth until he gets them the way he wants them."
//The code in question int senseCount = nNintersections + nEdges + nVertexParams;
bool flip = false;
for (int ii=0; ii!= senseCount; ++ii) { flip = !flip; }
|
|
-
-
Volmarias


- Joined on 07-14-2005
- Princeton, NJ
- Posts 402
|
Re: Influenced by Pascal...
Flip.... Flip, Flip, Flip the patties! Flip, Flip, Flip!
Oh Parappa the Rappa, the memories you bring.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
Doesn't seem so weird to me.. I make use of this paradigm all the time. int is_neg(long n)
{
// tells if a number is negative or positive
for (;;) {
n++;
if (n == 0) {
return 1;
} else if (n == 2147483647) {
return 0;
}
}
}long multi_add(int x, int y)
{
// takes the first number and finds out how much it is
// added to itself the number of times specified by the second
long result = 0;
for(int i = 0; i < y; i++) {
result += x;
}
return result;
}
|
|
-
-
Weng


- Joined on 03-15-2008
- Posts 508
|
Re: Influenced by Pascal...
morbiuswilters:Satanic code
*HEAD ASPLODES*
|
|
-
-
ZippoLag


- Joined on 03-19-2008
- Argentina
- Posts 109
|
Re: Influenced by Pascal...
morbiuswilters:I have come here to chew memory.. and processor time..
"up and down, back and forth, faster, faster.."
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
ZippoLag: morbiuswilters:I have come here to chew memory.. and processor time..
Are you joking? is_neg can determine that 5 is positive in less than 10 seconds on my computer. I'd like to see you now-it-alls do better.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
morbiuswilters: ZippoLag: morbiuswilters:I have come here to chew memory.. and processor time..
Are you joking? is_neg can determine that 5 is positive in less than 10 seconds on my computer. I'd like to see you now-it-alls do better.
Fine, here you go: int is_neg(long n) { // tells if a number is negative or positive
for (;;) { n /= 2;
if (n == 1) { return 1; } else if (n == -1) { return 0; } } } Stick around, maybe I'll teach you a thing or two about good programming.
|
|
-
-
AbbydonKrafts


- Joined on 11-21-2006
- Carrollton, GA, USA
- Posts 1,022
|
Re: Influenced by Pascal...
bstorer:Fine, here you go
You're both sick. LOL
Join us at #TDWTF on irc.slashnet.org !
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
bstorer: Stick around, maybe I'll teach you a thing or two about good programming.
Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop. Better luck next time, rookie.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
morbiuswilters: bstorer: Stick around, maybe I'll teach you a thing or two about good programming.
Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop. Better luck next time, rookie.
You can't figure out if 0 is negative without using a computer? Pathetic.
|
|
-
-
ZippoLag


- Joined on 03-19-2008
- Argentina
- Posts 109
|
Re: Influenced by Pascal...
"up and down, back and forth, faster, faster.."
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
bstorer:You can't figure out if 0 is negative without using a computer? Pathetic.
Listen, that kind of attitude may pass the mustard in your academic ivory tower. But here in the real world we have to deal with user data data data. And part of that means sometimes there are zeroes and you can't use your "elegant" solutions and you just have to Jam it!
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
morbiuswilters: bstorer:You can't figure out if 0 is negative without using a computer? Pathetic.
Listen, that kind of attitude may pass the mustard in your academic ivory tower. But here in the real world we have to deal with user data data data. And part of that means sometimes there are zeroes and you can't use your "elegant" solutions and you just have to Jam it!
Fine, fine. The workaround is fairly obvious, of course. I'm surprised you couldn't figure it out for yourself. Just further proof that my work isn't done here, I guess: int is_neg(long n)
{
// tells if a number is negative or positive
int i = 0;
for (;;) {
n /= 2;
if (n == 1) {
return 1;
} else if (n == -1) {
return 0;
}
++i;
if (i == 32)
return 1;
}
}
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
bstorer: int i = 0;
What a memory hog. Hey, I hear there's an opening on the Firefox team -- you might consider joining.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
morbiuswilters: bstorer: int i = 0;
What a memory hog. Hey, I hear there's an opening on the Firefox team -- you might consider joining.
Too true, should've been a char.
|
|
-
-
mxsscott


- Joined on 03-28-2008
- UK
- Posts 50
|
Re: Influenced by Pascal...
bstorer:int is_neg(long n)
{
// tells if a number is negative or positive
int i = 0;
for (;;) {
n /= 2;
if (n == 1) {
return 1;
} else if (n == -1) {
return 0;
}
++i;
if (i == 32)
return 1;
}
}
Is there a WTF competition on that I missed? The code in this thread is sick. Brilliant, but sick.
Fixing bugs in a VB program is like playing whack-a-mole.
|
|
-
-
Nandurius


- Joined on 05-15-2006
- Posts 328
|
Re: Influenced by Pascal...
bstorer: if (i == 32)
return 1; Thanks for the code, I've been using it with great success. I've noticed that large negative numbers are detected as positive numbers though. It seems that the function isn't quite ready for production yet.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
Nandurius:It seems that the function isn't quite ready for production yet.
Nonsense. All code has been tested and confirmed to work on my production 286.
|
|
-
-
Nandurius


- Joined on 05-15-2006
- Posts 328
|
Re: Influenced by Pascal...
bstorer: Nonsense. All code has been tested and confirmed to work on my production 286. Unfortunately there's no money for hardware upgrades left in the budget this year. I managed to fix the issue with large numbers, though, by making multiple passes through the division loop. Since my doubles are twice as long as the standard 32 bit ones, I need at least two passes.. I added another one just to be on the safe side of things.
static boolean is_neg(long n) { // tells if a number is negative or positive int i = 0; int passes = 0;
for (;;) { n /= 2;
if (n == 1) { return false; } else if (n == -1) { return true; } ++i; if (i == 32) if( passes == 2) return true; // return false; else { passes++; i = 0; } } } Unfortunately there still seems to be an issue with -1, which was returning false. If we redefine 0 to be negative though, and change the last return to true then it works as expected.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
Nandurius:I managed to fix the issue with large numbers, though, by making multiple passes through the division loop. No, no, no. Just copy and paste the loop twice, obviously, and reset i to 0. Nandurius:Unfortunately there still seems to be an issue with -1, which was returning false.
Simple solution to that, too: just subtract 1 from n before testing it.
|
|
-
-
Mo6eB


- Joined on 05-15-2007
- Posts 7
|
Re: Influenced by Pascal...
Nandurius:...almost 30 lines of code...
I swear, today's programmers can't code, even if I hit them on the head with a mallet. Observe, students: // tells if a number is negative or positive
static boolean is_neg(long n) { for (; n != 0; n /= 2) if (n == -1) return true; return false; } There, just one line, doesn't waste memory for extra variables and correctly handles "0" and "-1".
|
|
-
-
Faxmachinen


- Joined on 03-19-2007
- Posts 199
|
Re: Influenced by Pascal...
use threads;
sub universe
{
my ($number, $direction) = @_;
while ($number) { $number += $direction; }
threads->exit();
}
sub is_neg
{
my $number = shift;
my $positive_universe = threads->create('universe', $number, 1);
my $negative_universe = threads->create('universe', $number, -1);
while (1)
{
if (not $positive_universe->is_running())
{
$negative_universe->kill('DESTROY');
return 0;
}
if (not $negative_universe->is_running())
{
$positive_universe->kill('DESTROY');
return 1;
}
}
}
rpar PROTON all
|
|
-
-
Iago


- Joined on 01-12-2006
- Posts 123
|
Re: Influenced by Pascal...
for (; n != 0; n /= 2)
This is far too complicated for maintainance programmers to understand. What's wrong with do ... while (n /= 2)?
And using an int returning values of 1 and 0 would save a whole load of characters. Really, I don't know how your company can afford to retain people like you who insist on filling up the punched cards with all this verbosity.
|
|
-
-
DrJokepu


- Joined on 03-20-2008
- London, UK
- Posts 137
|
Re: Influenced by Pascal...
Iago:for (; n != 0; n /= 2)
This is far too complicated for maintainance programmers to understand. What's wrong with do ... while (n /= 2)? How about...
for(; n; n >>= 1) Regarding the original post: I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong. Any opinions? Edit: Oh yes, sorry, that was a signed integer. Never mind the bit shift then.
The good doctor is here to help. I promise this time I will not screw up the operation.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
DrJokepu:I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong.
Technically, it is wrong. x++ creates a temporary variable to store the return value. Now, most compilers are smart enough to convert it to ++x on the fly, but that doesn't make it right. Off the top of my head I cannot conceive of a single time when x++ is superior. A construct like n = ++x is no more expressive -- really, it's actually more muddled -- and less efficient than doing n = x; ++x;
|
|
-
-
DrJokepu


- Joined on 03-20-2008
- London, UK
- Posts 137
|
Re: Influenced by Pascal...
bstorer:A construct like n = ++x is no more expressive -- really, it's actually
more muddled -- and less efficient than doing n = x; ++x; Now I am far from being a C expert, but isn't n = ++x is more like x = x + 1; n = x; instead of n = x; ++x;? Note the difference between the final values of n.
The good doctor is here to help. I promise this time I will not screw up the operation.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
DrJokepu: bstorer:A construct like n = ++x is no more expressive -- really, it's actually
more muddled -- and less efficient than doing n = x; ++x; Now I am far from being a C expert, but isn't n = ++x is more like x = x + 1; n = x; instead of n = x; ++x;? Note the difference between the final values of n.
Quiet, you! I meant n = x++ is less expressive and efficient than n = x; ++x; Clearly I have an aversion to even typing x++.
|
|
-
-
mxsscott


- Joined on 03-28-2008
- UK
- Posts 50
|
Re: Influenced by Pascal...
bstorer: DrJokepu:I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong.
Technically, it is wrong. x++ creates a temporary variable to store the return value. And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++.
Fixing bugs in a VB program is like playing whack-a-mole.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
mxsscott:And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++.
Or, um, avoid operator overloading altogether.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
morbiuswilters: mxsscott:And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++.
Or, um, avoid operator overloading altogether.
Or, um, avoid C++ altogether.
|
|
-
-
mxsscott


- Joined on 03-28-2008
- UK
- Posts 50
|
Re: Influenced by Pascal...
bstorer: morbiuswilters: mxsscott:And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++.
Or, um, avoid operator overloading altogether.
Or, um, avoid C++ altogether. But then there would be no WTFs!
Fixing bugs in a VB program is like playing whack-a-mole.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
mxsscott:But then there would be no WTFs!
So long as there are languages to program in, there will WTFs. Have no fear.
|
|
-
-
ender


- Joined on 04-27-2006
- Posts 491
|
Re: Influenced by Pascal...
mxsscott:But then there would be no WTFs!Â
Why would C++ be necessary for WTFs?
Because 10 billion years' time is so fragile, so ephemeral... it arouses such a bittersweet, almost heartbreaking fondness.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
bstorer:Technically, it is wrong. x++ creates a temporary variable to store the return value. Now, most compilers are smart enough to convert it to ++x on the fly, but that doesn't make it right.
You may be a smart programmer, but you have a lot to learn about programming. Do all of your fancy books teach you a damn thing about relating to your variables as people? You see creating a temporary variable as wasteful, I see a variable who is going through a scary change and who might appreciate a friend. Remember the story of Variable Jesus who allowed his own destructor to be called so that all other variables might have everlasting scope.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
ender:Why would C++ be necessary for WTFs?
C++ doesn't create WTFs, it only attracts them. The hope is that one day the C++ spec becomes so massive that all WTFs are contained therein and it can then be nuked from orbit.
|
|
-
-
piptheGeek


- Joined on 12-16-2006
- Posts 36
|
Re: Influenced by Pascal...
bstorer:Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop. Better luck next time, rookie
here's a fix
int is_neg(long n) { // tells if a number is negative or positive if (n == 0) return FILE_NOT_FOUND; for (;;) { n /= 2;
if (n == 1) { return 1; } else if (n == -1) { return 0; } } }
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
piptheGeek: bstorer:Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop. Better luck next time, rookie
learn2quote That said, your program fails because I always #define FILE_NOT_FOUND to 1 in all of my C headers. Obviously, is_neg should not return 1 for 0.
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
morbiuswilters:You see creating a temporary variable as wasteful, I see a variable who is going through a scary change and who might appreciate a friend. Remember the story of Variable Jesus who allowed his own destructor to be called so that all other variables might have everlasting scope.
You drag your religion in as a justification of your wasteful practices? Next you'll be claiming that C++ is the result of intelligent design, despite the overwhelming evidence showing its evolution from C.
|
|
-
-
Helix


- Joined on 08-07-2007
- Posts 211
|
Re: Influenced by Pascal...
DrJokepu:Regarding the original post: I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong. Any opinions? whats wring with using inline asm instruction - inc?
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: Influenced by Pascal...
Helix: whats wring with using inline asm instruction - inc?
Other than a lack of portability, doing something in assembly that has a direct C counterpart, and limiting the compiler's ability to optimize? Nothing, I guess.
|
|
-
-
Helix


- Joined on 08-07-2007
- Posts 211
|
Re: Influenced by Pascal...
Mo6eB: Nandurius:...almost 30 lines of code...
I swear, today's programmers can't code, even if I hit them on the head with a mallet. Observe, students: // tells if a number is negative or positive
static boolean is_neg(long n) { for (; n != 0; n /= 2) if (n == -1) return true; return false; } There, just one line, doesn't waste memory for extra variables and correctly handles "0" and "-1".
This is actually two lines of code so time to learn a little known programming fact "the more lines of code the more expensive it is to maintain". Additionaly this example shows javas waste of processor cycles, after your byte code has been interpreted, mine is executed without any loops or maths:
int is_neg(long n) {return((n&0x80000000)>>31);} Just an AND and SHR on intel
|
|
-
-
Helix


- Joined on 08-07-2007
- Posts 211
|
Re: Influenced by Pascal...
//using 72 as test case //Arm7TDMI core bstores solution cycle count = 234 helix solution cycle count = 2 ~100 times faster code without changing tools
|
|
-
-
upsidedowncreature


- Joined on 11-21-2007
- Posts 216
|
Re: Influenced by Pascal...
Helix: ~100 times faster code without changing tools I'm not sure you're getting what's going on here...
What if the hokey cokey really IS what it's all about?
|
|
-
-
mxsscott


- Joined on 03-28-2008
- UK
- Posts 50
|
Re: Influenced by Pascal...
upsidedowncreature: Helix: ~100 times faster code without changing tools I'm not sure you're getting what's going on here... And my worry is, if you're not getting that everyone is producing inefficient solutions on purpose, that you didn't write: bool is_neg(long x) { return (x < 0); } which works for any size of long.
Fixing bugs in a VB program is like playing whack-a-mole.
|
|
-
-
Eternal Density


- Joined on 03-25-2007
- Posts 358
|
Re: Influenced by Pascal...
upsidedowncreature: Helix: ~100 times faster code without changing tools I'm not sure you're getting what's going on here...
SSDS is the only tool you need! I think the original writer of the flipping code deserves a job flipping burgers.
Mass Effect 2 FTW!
lolwtf: Instead of comfy chair, package contained bobcat. Would not buy again. curtmack: It's like Godwin's Law, but instead of Hitler it's xkcd references. morbiuswilters: Right, but the Holocaust wasn't nearly as bad as xkcd.
|
|
-
-
Helix


- Joined on 08-07-2007
- Posts 211
|
Re: Influenced by Pascal...
this cirtainly does not compile any flatter - worse depending on tool. Also bool is not suggested for use due to vendors c standard implimentation - i prefer to stick with ISO/IEC 9899:1990 Programming Languages C
standard, changed by Amendment 1:1995, Technical corrigendum 1:1994,
and Technical corrigendum 2:1996. That way there is no problems with code migration.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
Helix:whats wring with using inline asm instruction - inc?
Helix:This is actually two lines of code so time to learn a little known
programming fact "the more lines of code the more expensive it is to
maintain". Additionaly this example shows javas waste of
processor cycles, after your byte code has been interpreted, mine is
executed without any loops or maths:
int is_neg(long n) {return((n&0x80000000)>>31);} Just an AND and SHR on intel
Helix:That way there is no problems with code migration.
*cough*
|
|
-
-
Helix


- Joined on 08-07-2007
- Posts 211
|
Re: Influenced by Pascal...
I suggest you try bool is_neg(long x) { return (x < 0); } and int
is_neg(long n) {return((n&0x80000000)>>31);} in multiple c compilers
and archs and let me know the results of processor time and code portability.
The trouble is these days too many posters on TDWTF forums do not run real
'down to the metal code' and just run queries on databases - a
bit like the receptionist at the doctors.
|
|
-
-
mxsscott


- Joined on 03-28-2008
- UK
- Posts 50
|
Re: Influenced by Pascal...
Helix:this cirtainly does not compile any flatter - worse depending on tool. Also bool is not suggested for use due to vendors c standard implimentation - i prefer to stick with ISO/IEC 9899:1990 Programming Languages C
standard, changed by Amendment 1:1995, Technical corrigendum 1:1994,
and Technical corrigendum 2:1996. That way there is no problems with code migration. I was using C++, so bool is fine to use. Agreed that int is better for x-compiler compatibilty with C.
However, it's time for compilation wars: return (n < 0) compiles as:
0x00001df9 <+0015> shr $0x1f,%eax return (0x8000000 & n) >> 31 compiles as: 0x00001e0d <+0015> and $0x8000000,%eax 0x00001e12 <+0020> sar $0x1f,%eax This, with gcc 4.0.1 (Xcode 3 on an Intel Mac). Cunningly, if I compiled in release mode, the functions didn't exist (so saved all that parameter passing and stack manipulation) and it just shoved 0 onto the stack for the call to printf (I was asking if 10000 was negative).
Fixing bugs in a VB program is like playing whack-a-mole.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: Influenced by Pascal...
Helix:I suggest you try bool is_neg(long x) { return (x < 0); } and int
is_neg(long n) {return((n&0x80000000)>>31);} in multiple c compilers
and archs and let me know the results of processor time and code portability.
Are you a complete dumbass or what? Not only did you somehow miss that this whole thread was a joke, but you took the assignment seriously and actually wrote a function that checks if a number is negative. I'll let your contradictory, babbling quotes on portability speak for themselves. Helix:The trouble is these days too many posters on TDWTF forums do not run real
'down to the metal code' and just run queries on databases - a
bit like the receptionist at the doctors.
Apparently that's the only way to keep your sanity.
|
|
|
|
|