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]
How's this for cryptic, uncommented code
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?
It''s not bad really, pretty easy to follow. I guess it depends on how much nasty code you have had to wade through .
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
________________________
Grab your sword, and get prepared for the SolidSteel RPG engine, coming soon...(i.e. when it''s done)
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)
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
To Cold_steel: Good God
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.
Don''t listen to me. I''ve had too much coffee.
Here's an obfuscated Eulerian GCD function for ya :-D
Don't listen to me. I've had too much coffee.
[edited by - sneftel on October 31, 2002 12:52:20 AM]
#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]
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]
Numerical double integrator
Implementation of properties in standard c++.
And my gcd can beat up your gcd:
Implementation of properties in standard c++.
And my gcd can beat up your gcd:
int gcd_of_evil(int a, int b){ while (true) { if (a > (b << 2)) a %= b; else while (a >= b) a -= b; if (!a) return b; if (b > (a << 2)) b %= a; else while (b >= a) b -= a; if (!b) return a; }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement