hi,
I'm just starting out scripting in C, and while I know the theory, practice seems to be harder then I had hoped.
I wrote this simple script as an exercise but for some reason I don't get the expected result:
Code:
#include <stdio.h>
float sqroot(unsigned int, float);
float absff(float);
int main()
{
int first, second, nil=1;
while(nil){
printf("\ngetal 0: ");
scanf("%d",&nil);
printf("\nsqroot(%d)=%f\n",nil,sqroot(nil,0.1F));
}
}
float sqroot(unsigned int aa, float eps){
float res1,res2;
printf(" aa=%f\n",(float)aa);
printf(" eps=%f\n",eps);
res1=absff((float)aa);
res2=absff(-1.0F*aa );
printf(" absff( (float)aa )=%f\n",res1);
printf(" absff( -1.0F*aa )=%f\n",res2);
return absff((float)aa);
}
float absff(float ff){
printf(" ff=%f\n",ff);
return (ff>=0.0F?ff:-1.0F*ff);
}
I run this script inside the console and get this:
Code:
getal 0: 25
aa=25.000000
eps=0.100000
ff=0.000000
ff=0.000000
absff( (float)aa )=16384.000000
absff( -1.0F*aa )=30720.000000
ff=0.000000
sqroot(25)=28672.000000
getal 0:
I would expect to see this though:
Code:
getal 0: 25
aa=25.000000
eps=0.100000
ff=25.000000
ff=25.000000
absff( (float)aa )=25.000000
absff( -1.0F*aa )=25.000000
ff=25.000000
sqroot(25)=25.000000
getal 0:
Notes:
--------
- I know there's a math lib available, but I'm unfamiliar with it('s use).
- I really don't understand why the float values (ie. (float)aa) aren't passed on to the absff() function, nor why I get this result:
Code:
ff=0.000000
ff=0.000000
absff( (float)aa )=16384.000000
absff( -1.0F*aa )=30720.000000
If ff==0.000000 then why does the console tell me the result is 16384.000000??