Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2006, 3:30 PM   #1
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
Math functions in programming

I'm looking for the best way to represent a math function like:
f(x) = x^(9/2) in programming. I've tried to look this up by google, but I don't know what to search for because the term "function" it way to broad when dealing with programming. Is there some kind of class for this? Thanks!
Kazy is offline   Reply With Quote
Old Apr 13th, 2006, 4:09 PM   #2
a3orange
Newbie
 
Join Date: Apr 2006
Posts: 14
Rep Power: 0 a3orange is on a distinguished road
What math function are you using for f(x)?
a3orange is offline   Reply With Quote
Old Apr 13th, 2006, 4:10 PM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
arent you looking for something as simple as this:
#include<iostream.h>
#include<cmath>


double function(double x)
{
	 
    return pow(x,4.5);
     
         
 }

int main()
{
	cout<<function(3)<<endl;
	return 0;
}
hbe02 is offline   Reply With Quote
Old Apr 13th, 2006, 4:11 PM   #4
a3orange
Newbie
 
Join Date: Apr 2006
Posts: 14
Rep Power: 0 a3orange is on a distinguished road
Ooh, that's what you wanted. (I wasn't sure if you wanted multiplication for the first one)
a3orange is offline   Reply With Quote
Old Apr 13th, 2006, 7:16 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
In C++, heb02's example should actually be;
#include<iostream>
#include<cmath>

double function(double x)
{
   return std::pow(x,4.5);
}

int main()
{
	std::cout<<function(3.0)<<endl;
	return 0;
}
Of course, that assumes by ^ Kazy means "to the power of" and (s)he think of his terms are real numbers.

If we just gave the compiler x^(9/2), the compiler would turn that into x^4 (as 9 and 2 are integers, so 9/2 is 4), and that would cause a compile error unless x is an integral type. If x is an integral type, some bits of x would be fiddled.

If you want to find information on the C++ standard, google for "C++ standard library" or "C standard library" (as C++ also allows use of the C standard library, but deprecates it). Both include functions for doing floating point mathematics.
grumpy is offline   Reply With Quote
Old Apr 14th, 2006, 3:03 AM   #6
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
Grumpy, shouldnt we use namespace std?
biohazard is offline   Reply With Quote
Old Apr 14th, 2006, 3:33 AM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by biohazard
Grumpy, shouldnt we use namespace std?
Look that the code I gave again. It is explicitly using functions in namespace std. There is no requirement for a "using namespace std;" directive.
grumpy is offline   Reply With Quote
Old Apr 14th, 2006, 3:19 AM   #8
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
biohazard, I think you mean shouldn't we simply put
using namespace std;
at the top after including the files.

Yes, we can do it. If we do, we don't need to specify the std:: prefix everytime we use something from the std namespace. But it's not recommended to do this in a header file. Also I'd bring the whole of the std namespace into scope. If you just want a few of the elements, you can specify them individually like:
using std::cout;
using std::cin;
using std::endl;
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 14th, 2006, 3:38 AM   #9
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Completely unrelated, I just noticed Grumpy was user #2600.
__________________

tempest is offline   Reply With Quote
Old Apr 14th, 2006, 5:07 AM   #10
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
I'm slightly curious as to whether the OP may have been looking for something like syntax trees instead of a hard-coded function...
Jimbo 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 2:20 AM.

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