![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2006
Posts: 14
Rep Power: 0
![]() |
What math function are you using for f(x)?
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
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;
} |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2006
Posts: 14
Rep Power: 0
![]() |
Ooh, that's what you wanted. (I wasn't sure if you wanted multiplication for the first one)
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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;
}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. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2006
Posts: 18
Rep Power: 0
![]() |
Grumpy, shouldnt we use namespace std?
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
biohazard, I think you mean shouldn't we simply put
using namespace std; 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. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
Completely unrelated, I just noticed Grumpy was user #2600.
__________________
|
|
|
|
|
|
#10 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
I'm slightly curious as to whether the OP may have been looking for something like syntax trees instead of a hard-coded function...
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|