GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Dec-2006, 23:47
Petar Petar is offline
New Member
 
Join Date: Dec 2006
Posts: 2
Petar is on a distinguished road

Contents or source for pow() function


hiiiii,

How can I open library in c++?
And
How can I see functions in c++?
for example : I want see (pow(x,y) function).

I have visual c++.
I want open it by this program.
  #2  
Old 01-Jan-2007, 10:19
davis
 
Posts: n/a

Re: How?


Quote:
Originally Posted by Petar
hiiiii,

How can I open library in c++?
And
How can I see functions in c++?
for example : I want see (pow(x,y) function).

I have visual c++.
I want open it by this program.

First, RTFG....

Second: #include <cmath>


:davis:
  #3  
Old 01-Jan-2007, 19:20
Petar Petar is offline
New Member
 
Join Date: Dec 2006
Posts: 2
Petar is on a distinguished road
What dose mean ((RTFG....))?
  #4  
Old 01-Jan-2007, 19:28
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: How?


Quote:
Originally Posted by Petar
What dose mean ((RTFG....))?
Read The F?????? Guidelines.

I think :davis: might have a hangover.
ha!
  #5  
Old 02-Jan-2007, 12:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Post

Re: How?


Quote:
Originally Posted by Petar
How can I see functions in c++?
for example : I want see (pow(x,y) function).

I have visual c++.

If you want to use the function, then #include <cmath>, as Davis suggested.

If you want to look at the source code for pow(), you should know that this is not supplied by Microsoft for their library functions. (Same for Borland.) For GNU compiler installations, it is possible to look up the source code for everything, but there are some issues:



1. It is very likely that such things will include some very low level asm or other low-level bit manipulations and/or platform-specific floating point instructions to ensure the efficiency and robustness required for library functions, so if you are looking to learn "good programming practices", GNU source may not be the way to start. (Man, oh, man; look at all of those "goto" statements!!!)

2. It's kind of hard to find. Start at www.gnu.org and poke around; you might find library source code somehow, somewhere.

For example with the C++ in the GNU compiler suite, there are several pow() functions prototyped in <cmath>:

CPP / C++ / C Code:
float pow(float __x, float __y)
long double pow(long double __x, long double __y)
double pow(double __x, int __i)
float pow(float __x, int __n)
long double pow(long double __x, int __n)

The fun part: All are #defined to use built-in functions, whose actual code is buried (waaaaay deeply) in the source code --- somewhere.


If, for learning purposes, you want to make your own pow() function, then you can start with the mathematical definition of what "x to the power y" really means:

"x to the power y" in general is defined as "exp(y log x)". Where exp() is the exponential function, and log means natural log (log to the base e, where e = exp(1)).

Note that a special case for negative numbers raised to integer powers is needed, since the above definition would be meaningless (log of a negative number is undefined), but such expressions are commonly used in lots of places.

That is, "something" raised to the "n" power is (loosely) defined as "something" multiplied by itself with a total of "n" factors, so, for example pow(-2.0, 3) gives -8.0

Note that the "something" multiplied by itself "n" times definition only works for powers that are ints, since it couldn't be used for something like "3.4 raised to the 5.6 power". Also: how would you handle something like "zero raised to the -3 power"?

You can try different values of x and n in the following:
CPP / C++ / C Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = 3.4;
    double n = 5.6;
    cout << x << " raised to the " << n << " power is equal to " 
         << pow(x, n) << endl;
    return 0;
}

You should see something like:
Code:
3.4 raised to the 5.6 power is equal to 946.852

(Check it with a calculator, if you want to.)

Regards,

Dave
  #6  
Old 02-Jan-2007, 20:39
davis
 
Posts: n/a

Re: How?


Quote:
Originally Posted by davekw7x
If you want to look at the source code for pow(), you should know that this is not supplied by Microsoft for their library functions.

I think that you will find the sources to the Microsoft implementation of their standard C library--since MS VS6--in the header files implemented as inline functions. See the contents of math.h for more information.


:davis:
  #7  
Old 02-Jan-2007, 21:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How?


Quote:
Originally Posted by davis
I think that you will find the sources to the Microsoft implementation of their standard C library--since MS VS6--in the header files implemented as inline functions. See the contents of math.h for more information.


:davis:

I didn't realize that (and, of course, it's not the first time that I have been wrong --- not even the first time this year). For floats, doubles, long doubles raised to int powers there is a template function fully defined in <math.h> The other pow() functions (things raised to floating point powers) aren't as easy to find (at least I don't see them).

Thanks for the info.

Regards,

Dave
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:06.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.