Calculating greatest common divisor

Hi, I have written the following function to calculate GCD of floating point numbers, but when I run this for (111.6, 46.5), the calculation of fmod(a,b) in the funciton starts giving the wrong result after 2 recursive calls. I am unable to find the error here. Can anyone please help me with this?

float gcd(float a, float b){
if (a>b) {
if(b==0){
return a;
}
else {
return gcd(b, fmod(a,b));
}

}
else {
if (a==0) {
return b;
}
else {
return gcd(fmod(b,a), a);
}

}

}
while not a recursive approach, this seems to work...not well tested though.

1
2
3
4
5
6
7
8
9
10
11
float gcd(float a, float b) {
	while(a!=b) {
		if(b>a) {
			float c=b;
			b=a;
			a=c;
		}
		while(a>b) a-=b;
	}
	return(a);
}
Topic archived. No new replies allowed.