Jump to content

  • Log In with Google      Sign In   
  • Create Account

Eight, Nine, Ten...

  • You cannot reply to this topic
32 replies to this topic

#1 Milcho   Crossbones+   -  Reputation: 1175

Like
14Likes
Like

Posted 18 March 2013 - 10:28 AM

Excerpt from code I recently saw:

 

#define EIGHT  (0x08) 
#define NINE   (0x09) 
#define TEN    (0x10) 

. . .

I hope I wasn't too subtle - the definition of TEN is what is ... interesting here.


Edited by Milcho, 18 March 2013 - 10:35 AM.


Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6985

Like
7Likes
Like

Posted 18 March 2013 - 10:29 AM

Good thing they put parentheses around those literals!

 

edit:

Oh gosh, I didn't even realize the TEN issue... That's horrible.


Edited by Cornstalks, 18 March 2013 - 10:37 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 Milcho   Crossbones+   -  Reputation: 1175

Like
0Likes
Like

Posted 18 March 2013 - 10:44 AM

edit:
Oh gosh, I didn't even realize the TEN issue... That's horrible.

That edit made my day :D - that is exactly why this is so scary.



#4 bollµ   Members   -  Reputation: 354

Like
1Likes
Like

Posted 18 March 2013 - 11:01 AM

an explanation for the more noobish of us? what's wrong with that definition? >_<;


a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)


#5 Rattrap   Members   -  Reputation: 1651

Like
3Likes
Like

Posted 18 March 2013 - 11:07 AM

an explanation for the more noobish of us? what's wrong with that definition? >_<;

 

0x10 in 16 in hex.



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5369

Like
3Likes
Like

Posted 18 March 2013 - 11:24 AM

Could be binary coded decimal... and it makes outputting a text value easier as well ;)

 

Manic Miner on the ZX Spectrum didn't even store the game score or high score in a variable... it just used ASCII text on the screen (the screen area where the score was displayed was never cleared).

 

The algorithm only added to your score either 1 or 10 or 100 at a time IIRC. It just increased the ASCII value at the screen position by 1 and if that makes it > 9 it made the current digit 0 and added 1 to the next digit along (and looped if that was bumped to > 9 as well).

 

It then just copied the score to the high score location if the score was larger than the current high score (check was done with another ASCII comparison).


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 Milcho   Crossbones+   -  Reputation: 1175

Like
0Likes
Like

Posted 18 March 2013 - 11:53 AM

Could be binary coded decimal... [snip]

 

That sounds so convoluted it requires its own thread. 

Unfortunately that wasn't the case here - this code, according to a colleague who originally fixed it - caused a fatal crash in the software. 



#8 Tournicoti   Prime Members   -  Reputation: 683

Like
2Likes
Like

Posted 18 March 2013 - 12:55 PM

Obvious, the coder has 8 fingers on each hand


Edited by Tournicoti, 18 March 2013 - 12:57 PM.


#9 BGB   Crossbones+   -  Reputation: 1554

Like
0Likes
Like

Posted 18 March 2013 - 05:39 PM

Obvious, the coder has 8 fingers on each hand

... maybe the use of named constants is what was at fault here ...

...
#define FOURTY_TWO (042)
#define NINETEEN_EIGHTY_FOUR (NINETEEN, EIGHTY, FOUR)
...

#10 ApochPiQ   Moderators   -  Reputation: 15692

Like
1Likes
Like

Posted 18 March 2013 - 06:16 PM

#define FOURTY_TWO (042)

Eurgh. I have many horror stories of Octal gone awry.

#11 mhagain   Crossbones+   -  Reputation: 7955

Like
0Likes
Like

Posted 18 March 2013 - 06:18 PM

#define FOURTY_TWO (042)

 

That's even worse!!!


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#12 Bacterius   Crossbones+   -  Reputation: 8821

Like
0Likes
Like

Posted 18 March 2013 - 07:21 PM

That's even worse!!!

This used to happen to me when I was trying to align constants properly. Nowadays I just put spaces instead, been burned too often by this damn octal notation "feature" which I'm guessing nobody actually uses, except the odd raw socket hacker (if even). Permission bits are another one, but constants for those are already defined anyway.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#13 BGB   Crossbones+   -  Reputation: 1554

Like
0Likes
Like

Posted 18 March 2013 - 09:36 PM


That's even worse!!!

This used to happen to me when I was trying to align constants properly. Nowadays I just put spaces instead, been burned too often by this damn octal notation "feature" which I'm guessing nobody actually uses, except the odd raw socket hacker (if even). Permission bits are another one, but constants for those are already defined anyway.


at one point I made things more orthogonal (for my script language) by adding several number notations:
0b... //binary
0c... //octal
0d... //decimal
in addition to:
0x... //hexadecimal

then got into a big mental debate as to whether or not to keep '0' by itself as an octal prefix, make it decimal, or maybe just deprecate it and issue a warning...

note, '_' is also a spacer, so:
0d999_999_999
is the same as:
999999999

likewise:
0x5CD13A42_3B9AC9FFL
or:
999_999_999_999_999_999L
or:
0x0DE0_B6B3__A763_FFFFL

#14 Sik_the_hedgehog   Crossbones+   -  Reputation: 1741

Like
0Likes
Like

Posted 18 March 2013 - 09:38 PM

Octal was big back in the day when C was being made, maybe even moreso than hexadecimal. These days nobody really uses it since it can't be aligned nicely to 8-bit (some computers back then had words with a bit count multiple of 3, so octal probably made a lot more of sense).


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#15 Cornstalks   Crossbones+   -  Reputation: 6985

Like
3Likes
Like

Posted 18 March 2013 - 09:40 PM

But they could have made it useful by adding pluses!

#define ONE     +1
#define TWENTY  +20
#define HUNDRED +100
 
// Here's a little example
#include <iostream>
 
int main()
{
    int x = ONE;
    std::cout << x << std::endl;
 
    x = ONE HUNDRED;
    std::cout << x << std::endl;
 
    x = ONE HUNDRED TWENTY;
    std::cout << x << std::endl;
 
    x = ONE HUNDRED TWENTY ONE;
    std::cout << x << std::endl;
}

 

Or even worse:

 
#define ONE     +0x001
#define TWENTY  +0x020
#define HUNDRED +0x100

Edited by Cornstalks, 18 March 2013 - 09:44 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#16 slicer4ever   Crossbones+   -  Reputation: 3883

Like
3Likes
Like

Posted 18 March 2013 - 10:02 PM

....snip...

just...just....o god my eyes....


Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#17 Trienco   Crossbones+   -  Reputation: 2169

Like
0Likes
Like

Posted 18 March 2013 - 10:23 PM

Still trying to figure out the reasoning behind that. Is that what happens when your code analysis tool complains about "magic numbers" and somebody just decides to "fix it"?

 

Because having one such tool complain about '0' being a "magic number" really made me question the worth of that tool and what the creators' code would look like.


f@dzhttp://festini.device-zero.de

#18 Bacterius   Crossbones+   -  Reputation: 8821

Like
1Likes
Like

Posted 18 March 2013 - 10:56 PM

But they could have made it useful by adding pluses!

#define ONE     +1
#define TWENTY  +20
#define HUNDRED +100
 
// Here's a little example
#include <iostream>
 
int main()
{
    int x = ONE;
    std::cout << x << std::endl;
 
    x = ONE HUNDRED;
    std::cout << x << std::endl;
 
    x = ONE HUNDRED TWENTY;
    std::cout << x << std::endl;
 
    x = ONE HUNDRED TWENTY ONE;
    std::cout << x << std::endl;
}

 

Or even worse:

 
#define ONE     +0x001
#define TWENTY  +0x020
#define HUNDRED +0x100

 

Sadly this doesn't fully work: 

 

 

TWO HUNDRED == 102

But don't worry - we can fix it! We just need to be "clever"...

 

 

#define AND +0
#define ONE +1
#define TWO +2
#define TWENTY +20
#define HUNDRED *100
 
// Here's a little example
#include <iostream>
 
int main()
{
    int x = TWO HUNDRED AND TWENTY ONE;
    std::cout << x << std::endl;
    // prints 221
}

 

This way you even get to write grammatically correct numbers. happy.png


Edited by Bacterius, 18 March 2013 - 11:00 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#19 Khatharr   Crossbones+   -  Reputation: 2996

Like
0Likes
Like

Posted 18 March 2013 - 11:07 PM

I think we should pool our efforts and make a programming language where this kind of thing is sane.


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#20 Hodgman   Moderators   -  Reputation: 30349

Like
4Likes
Like

Posted 18 March 2013 - 11:32 PM

I think we should pool our efforts and make a programming language where this kind of thing is sane.

http://thedailywtf.com/Articles/The_Secret_to_Better_C.aspx







PARTNERS