• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Math.h and linux

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

David

Forums Super Moderator
Joined
Feb 20, 2001
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>
 
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 )?
 
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.
 
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));
 
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.
 
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...
 
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
 
Back