www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Function overloading (not working as I expected)

reply Peter Federighi <pfederighi yahoo.com> writes:
(I apologize if this ends up being a duplicate posting.  The first time I tried
posting didn't seem to work.)
Hello fellow D programmers and enthusiasts.

I am wondering if you might be able to help me with a problem.  I thought it
would be a good idea to write a bunch of complex math routines to complement D's
native complex types.

To begin, I wrote a quick function to compute complex z to the real power n (z^n
= r^n(cos(n*theta) + sin(n*theta)i)).  It's definition is:
creal pow(creal z, real n){...code...}.

The function works fine, I've even used it to draw some fractals.  The problem
arises when I want to call regular pow as defined in std.math which has two
definitions:
real pow(real, real) and
real pow(real, uint)

DMD compiles my code to use my pow function, even though I'm not passing any
complex variables or assigning the return value to a complex variable.  The only
way I've gotten DMD to call the correct function is to explicitly call
std.math.pow.

Is there any solution to this aside from just renaming my function to cpow?
Thank you for your help.

- Peter
Mar 31 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Peter Federighi" <pfederighi yahoo.com> wrote in message
news:d2h9tp$1ct5$1 digitaldaemon.com...
 To begin, I wrote a quick function to compute complex z to the real power

 = r^n(cos(n*theta) + sin(n*theta)i)).  It's definition is:
 creal pow(creal z, real n){...code...}.

 The function works fine, I've even used it to draw some fractals.  The

 arises when I want to call regular pow as defined in std.math which has

 definitions:
 real pow(real, real) and
 real pow(real, uint)

 DMD compiles my code to use my pow function, even though I'm not passing

 complex variables or assigning the return value to a complex variable.

 way I've gotten DMD to call the correct function is to explicitly call
 std.math.pow.

 Is there any solution to this aside from just renaming my function to

What are the exact types of the arguments you are passing to pow()?
Mar 31 2005
parent reply Peter Federighi <pfederighi yahoo.com> writes:
Walter:
Thanks for responding.  Here's the code I'm trying to work with.  I've noted
where I try to call pow() and dmd doesn't do it correctly.  I've also tried
explicitly casting the values I pass to pow, but dmd still gets it wrong.

Thanks again.

- Peter

//
// Begin code
//
import std.math;
import std.math2;

void main ()
{
cdouble	z = 4 + 4i;
double	d;

printf("z = %f + %fi\n", z.re, z.im);
z = sqrt(cast(creal)z);
printf("sqrt(z) = %f + %fi\n", z.re, z.im);

z = 4 + 4i;
d = abs(z);
printf("abs(z) = %f\n", d);

z = 4 + 4i;
d = sqrt(abs(z));
printf("sqrt(abs(z)) = %f\n", d);

z = 4 + 4i;
d = hypot(z.re, z.im);
printf("hypot(z) = %f\n", d);

// d^2 -- DOES NOT CALL THE CORRECT POW()
d = pow(d, 2);
printf("pow(hypot(z.re, z.im), 2) = %f\n", d);

// sqrt(z)
z = pow(z, .5);	
printf("pow(z, .5) = %f +%fi\n", z.re, z.im);
}

real abs (creal z)
{
return z.re * z.re + z.im * z.im;
}

// Computes complex z to the real power n
// z^n = r^n(cos(n*theta) + sin(n*theta)i)
creal pow (creal z, real n)
{
// hypot() is the more correct way.  It catches errors, etc.
// sqrt(abs()) is the faster way.  It does not catch errors.
//	real	rn = pow(hypot(z.re, z.im), abs(n));
// DOES NOT CALL THE CORRECT POW() -- in this case it recurses until it blows
// the stack and segfaults
real	rn = pow(sqrt(abs(z)), abs(n));
real	angle;
creal	rtn;

if (!z.re)
angle = PI_2 * sign(z.im) * abs(n);
else
angle = atan(z.im / z.re) * sign(z.re) * abs(n);

rtn = rn * (cos(angle) + sin(angle)*1i);
if (n < 0)
return 1 / rtn;
else
return rtn;
}

//
// End code
//

In article <d2hg0h$1kel$2 digitaldaemon.com>, Walter says...
"Peter Federighi" <pfederighi yahoo.com> wrote in message
news:d2h9tp$1ct5$1 digitaldaemon.com...
 To begin, I wrote a quick function to compute complex z to the real power

 = r^n(cos(n*theta) + sin(n*theta)i)).  It's definition is:
 creal pow(creal z, real n){...code...}.

 The function works fine, I've even used it to draw some fractals.  The

 arises when I want to call regular pow as defined in std.math which has

 definitions:
 real pow(real, real) and
 real pow(real, uint)

 DMD compiles my code to use my pow function, even though I'm not passing

 complex variables or assigning the return value to a complex variable.

 way I've gotten DMD to call the correct function is to explicitly call
 std.math.pow.

 Is there any solution to this aside from just renaming my function to

What are the exact types of the arguments you are passing to pow()?

Apr 03 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Peter Federighi" <pfederighi yahoo.com> wrote in message
news:d2ovu8$2l1t$1 digitaldaemon.com...
 Walter:
 Thanks for responding.  Here's the code I'm trying to work with.  I've

 where I try to call pow() and dmd doesn't do it correctly.  I've also

 explicitly casting the values I pass to pow, but dmd still gets it wrong.

Try explicitly casting the arguments to the exact types of the pow() you wish to call.
Apr 04 2005
parent reply Peter Federighi <pfederighi yahoo.com> writes:
In article <d2t0iu$ruj$1 digitaldaemon.com>, Walter says...
"Peter Federighi" <pfederighi yahoo.com> wrote in message
news:d2ovu8$2l1t$1 digitaldaemon.com...
 Walter:
 Thanks for responding.  Here's the code I'm trying to work with.  I've

 where I try to call pow() and dmd doesn't do it correctly.  I've also

 explicitly casting the values I pass to pow, but dmd still gets it wrong.

Try explicitly casting the arguments to the exact types of the pow() you wish to call.

I tried that. It still didn't work. If it works for you, what version of dmd are you using? I'm using the Linux release of version 0.113. - Peter
Apr 07 2005
parent "Walter" <newshound digitalmars.com> writes:
"Peter Federighi" <pfederighi yahoo.com> wrote in message
news:d32vvc$1md7$1 digitaldaemon.com...
 In article <d2t0iu$ruj$1 digitaldaemon.com>, Walter says...
"Peter Federighi" <pfederighi yahoo.com> wrote in message
news:d2ovu8$2l1t$1 digitaldaemon.com...
 Walter:
 Thanks for responding.  Here's the code I'm trying to work with.  I've

 where I try to call pow() and dmd doesn't do it correctly.  I've also

 explicitly casting the values I pass to pow, but dmd still gets it



Try explicitly casting the arguments to the exact types of the pow() you
wish to call.

I tried that. It still didn't work.

Please post the exact code of that that didn't work.
 If it works for you, what version of dmd
 are you using?  I'm using the Linux release of version 0.113.

Apr 15 2005