View Full Version : Math.h and linux
Having dipped a toe into C programming, i have a problem. I try to use the sqrt function with all the correct arguments but with no luck.
any advice?
yes I did include:
#include <math.h>
engjohn
08-27-01, 01:26 AM
show me the function call and maybe i can help...
here is an example
/*Example:
SQRT.C: This program calculates a square root. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
double question = 45.35, answer;
answer = sqrt( question );
if( question < 0 )
printf( "Error: sqrt returns %.2f\n, answer" );
else
printf( "The square root of %.2f is %.2f\n", question, answer );
}
it was actually a program to calculate the standard deviation. I'll post it here soon. It all worked apart from the sqrt function.
do you call it as sqrt(integer) or can you call it as sqrt(double )?
engjohn
08-27-01, 10:44 PM
double sqrt( double x );
If you look at the example I gave, both variables (question and answer) are double.
Give the sqrt() a double and it returns a double.
It will not work if you pass it an integer. Or if you place the return value into an integer variable, this results in data loss.
Both variables need to be of type Double.
if I pass an integer to the sqrt() function will it not cast it to type double?
I am passing the result to a type double I'm sure. I'll check tonight.
engjohn
08-28-01, 01:39 PM
No, I have had problems with passing it an integer, it will return some crazy number. I thouht that it should convert an integer to a double just fine (no data loss), but it didnt want to work...
Both the passed variable and the return variable need to be of type double.
S*&^%!!!
Lost the source code. D*mn!
I will try to type it up again tonite. I might try explicitly casting an integer to type double ie.
int var;
double answer;
answer = sqrt(double(var));
engjohn
08-29-01, 04:41 PM
Look at the doc I uploaded... It is in windows Word .doc format.
Sorry I had to Zip it to upload it...
I tried both explicitly type casting and with both variables as double.
engjohn
08-29-01, 04:46 PM
I just went through the type casting version in debug mode and everything works fine, I mean all the values are correct until it gets output?????
It is really weird....
I will try a few things...
engjohn
08-29-01, 07:12 PM
OOPS.... I found an ERROR on my part... my bad Sorry for confusing you.
This works passing it a int...
I had an error in my printf statement.
replaced the first %.2f with a %.2d in the second printf statement.
check your output to make sure it is formatted correctly...
/*Example:
SQRT.C: This program calculates a square root. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
double answer;
int question = 45;
answer = sqrt(question);
if( question < 0 )
printf( "Error: sqrt returns %.2f\n, answer" );
else
printf( "The square root of %.2d is %.2f\n", question, answer );
}
I thought it was weird that I could make it work in C++ but not C so I went back and found the error. Once again sorry for all the confusion...
heres the source...damn ! have to zip it first
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.