Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2006, 1:28 PM   #1
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
Math functions

Hi everyone,

I have a problem regarding certain math functions in C/C++.

I am trying to use the sqrt() function in my program, but I get compilation errors, stating that the library has not been included. I have include them in the program header.

Also is there a way to raise a variable to a power such as x^2 where the (^) notation is used often in Maple or Mathematica does not seem to work in C/C++.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main() {
double x, func = 0.0, func2 = 0.0, resultF, errorF, resultErr;
int try, ntry;
printf("Input the number of MC trials\n");
scanf("%d",&ntry);
srand((unsigned)time((long *)0));
for (try=0; try<ntry; try++) {
x = rand()/(float)RAND_MAX;
func = func + x*x; /* function to be evaluated is parabola of x^2 */
func2 = func2 + x*x*x*x;
errorF = sqrt(func - func2);
}
resultF = func/ntry;
resultErr = errorF/ntry;
printf("MC estimate for function = %f\n", resultF);
printf("MC error for function = %f\n", resultErr);
}

I am using Debian Linux system with GNU C/C++ compiler.

Thank you for your help.

Really appreciate it.
can342man is offline   Reply With Quote
Old Jan 26th, 2006, 1:33 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You should post your exact error message. The presence of headers and libraries is NOT the same thing. Investigate the POW function; the '^' is a bitwise exclusive-or operator in C.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 26th, 2006, 1:38 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Be careful in your variable names... in some languages "try" is a keyword used in exception handling. Yes, Pow is the exponent function and we will need to see the exact error msg.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jan 26th, 2006, 2:32 PM   #4
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 555
Rep Power: 5 Benoit is on a distinguished road
Did you remember to link the Math library with -lm ?

gcc foo.c -o foo -lm
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Jan 26th, 2006, 4:14 PM   #5
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
Quote:
Originally Posted by Benoit
Did you remember to link the Math library with -lm ?

gcc foo.c -o foo -lm
Thank you.

It worked like a charm.
can342man is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC