Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jediknight219

How's this for cryptic, uncommented code

This topic is 5886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I needed a gcd function, so I quickly hacked out this one. Then I looked back at it and was glad I knew how it worked, because it looked pretty arcane for something so simple. What does everybody else think?
int gcd(int x, int y) {
	if(x < 0) {
		x = -x;
	}
	if(y < 0) {
		y = -y;
	}
	if(x == 0) {
		return y;
	}
	if(y == 0) {
		return x;
	}
	while(true) {
		if(x > y) {
			x = x % y;
			if(x == 0) {
				return y;
			}
		} else {
			y = y % x;
			if(y == 0) {
				return x;
			}
		}
	}
}
  
[edit]Ok, fixed the divide by zero problem.[/edit] [edited by - jediknight219 on October 31, 2002 11:54:15 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
It''s not bad really, pretty easy to follow. I guess it depends on how much nasty code you have had to wade through .

Share this post


Link to post
Share on other sites
Yeah, that''s not too bad, here''s a few lines of my worst. I really should go back and comment this so I can remember what it does

rotX = animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].x + rotX;
rotY = animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].y + rotY;
rotZ = animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].z + rotZ;
deltaRotX = rotX - animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].x*(1-fraction);
deltaRotY = rotY - animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].y*(1-fraction) ;
deltaRotZ = rotZ - animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].z*(1-fraction) ;


________________________
Grab your sword, and get prepared for the SolidSteel RPG engine, coming soon...(i.e. when it''s done)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Too... much... indirection... *dies*

Share this post


Link to post
Share on other sites
OP: Thats not bad at all. Quite easy to follow. In fact, i would say that code is very readable.

To Cold_steel: Good God

Share this post


Link to post
Share on other sites
I imagine that the function would be hard to understand if you didn''t know the Eulerian method for computing the GCD. In terms of coding style, it seems fairly good.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
Here's an obfuscated Eulerian GCD function for ya :-D


#DEFINE a(x) (((x)>0)?x:-x)


int gcd(int x,
int/* O O */y){
x=a/* */(x)
;y=/* \____/ */abs
(y);while (x*y){if
(x<y) swap (x,y)
;x=x%y;} return x?x:y;}



Don't listen to me. I've had too much coffee.


[edited by - sneftel on October 31, 2002 12:52:20 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Amnesty
OP: Thats not bad at all. Quite easy to follow. In fact, i would say that code is very readable.

To Cold_steel: Good God



The scary thing is, I've seen much, much worse! I was playing some game, and an error occurred and some debug output popped up with the call that caused the error. Anyway, I have never seen so many ->'s in my life. Honestly there must have been at least 10, any maybe more.

Something->SomethingElse->MoreStuff->anotherThing->YetMore->AndAgain->andAgain->something();

Only worse
I really should just copy the rotation vector and get rid of those horendous 6 lines of code. It's got to be hurting performance Screw it, I'll optimize it later

________________________
Grab your sword, and get prepared for the SolidSteel RPG engine, coming soon...(i.e. when it's done)

[edited by - Cold_Steel on October 31, 2002 12:22:17 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!