pow function

Hi,
What makes the pow function?I could not understand.
Can you briefly explain?


1
2
3
4
double pow( 
   double x, 
   double y 
);
=> xy
Last edited on
an exponentiation function.I got it thanks.

I am now

mypow(double, index); I am writing

Find a way to have 2 ** I call mypow(2 ,I);

Define a class I n d e x to hold the index for an exponentiation function

How do I do this operation.?

Last edited on
I guess you could overload the dereference operator for your Index class so that it returns something of type, let's say, Rdy2Pow. Then, you can overload double operator*(double x, Rdy2Pow y) to return mypow(x,y).

Though, I know people who would rather eat dead mice than write something like this...
@m4ster r0shi:
you would rather eat live mice?
No.
The question of the creator of C + +. (Bjarne Stroustrup)
still could not figure out this problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>
using namespace std;

struct Rdy2Pow
{
    double val;

    Rdy2Pow(double v):val(v){}
};

double operator*(double b, Rdy2Pow e)
{
    return pow(b,e.val);
}

struct Exponent
{
    double val;

    Exponent(double v):val(v){}

    Rdy2Pow operator*() const
    {
        return val;
    }
};

int main()
{
    Exponent e(0.5);

    cout << 2**e << endl;
    cout << 4**e << endl;
    cout << 9**e << endl;
    cout << 25**e << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
This is what?
How does this work?
What is it that you don't understand?

This -> cout << 2**e << endl; calls *e (i.e. e.operator*()) first.

That returns a temporary Rdy2Pow object, I'll call it r2p.

Now, we have something that looks like cout << 2*r2p << endl;.

Next, 2*r2p (i.e. operator*(2.0,r2p)) is called and returns pow(2.0,0.5), which is the square root of 2.

Finally, we have 1.4142... printed in the console window.
I understand very well.thanks
Topic archived. No new replies allowed.