-
need some C noob help
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??
-
I copy-pasted your code and ran it. I got the result you were expecting and not the one you're getting. Which compiler are you using?
EDIT: There is a library which you can include <math.h>. The square root function works as sqrt(any_number). There is also a pow(x,y) which calculates xraised to the power y. The important point is these two function do not return int values.
math functions
-
I just created a new empty project, copy pasted the source and ran it. it ran like I wanted it to, but I still dont know what's wrong with the other build. I checked the compiler on my files and VS2008 tells me they all "Compile as C Code (/TC)".
I'll be trying out some more...
EDIT:
So I created a new project with my code, and it works fine.
Then I created my header file, moved some of the declarations etc into it, rebuild and it still works fine.
Then I created a new source file, moved my methods into it, rebuild and suddenly I get the same problem as before.
What am I doing wrong in this last step? this is my current source:
file: main.c
Code:
#include "head1.h"
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));
}
}
file: head1.h
Code:
#include <stdio.h>
#include <math.h>
float sqroot(unsigned int, float);
float absff(float);
file: f1.c
Code:
float sqroot(unsigned int aa, float eps){
float res1,res2;
printf(" aa=%f\n",(float)aa);
printf(" eps=%f\n",eps);
res1=fabs((float)aa);
res2=fabs(-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);
}
-
nvm, found the problem; I didn't include the right headers in the second source file...