digitalmars.D - Function overloading (not working as I expected)
- Peter Federighi (21/21) Mar 31 2005 (I apologize if this ends up being a duplicate posting. The first time ...
- Walter (9/22) Mar 31 2005 n (z^n
- Peter Federighi (64/89) Apr 03 2005 Walter:
- Walter (6/10) Apr 04 2005 noted
- Peter Federighi (4/14) Apr 07 2005 I tried that. It still didn't work. If it works for you, what version ...
- Walter (4/20) Apr 15 2005 wrong.
(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
"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 powern (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. Theproblemarises when I want to call regular pow as defined in std.math which hastwodefinitions: real pow(real, real) and real pow(real, uint) DMD compiles my code to use my pow function, even though I'm not passinganycomplex variables or assigning the return value to a complex variable.The onlyway 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 tocpow? What are the exact types of the arguments you are passing to pow()?
Mar 31 2005
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 powern (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. Theproblemarises when I want to call regular pow as defined in std.math which hastwodefinitions: real pow(real, real) and real pow(real, uint) DMD compiles my code to use my pow function, even though I'm not passinganycomplex variables or assigning the return value to a complex variable.The onlyway 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 tocpow? What are the exact types of the arguments you are passing to pow()?
Apr 03 2005
"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'venotedwhere I try to call pow() and dmd doesn't do it correctly. I've alsotriedexplicitly 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
In article <d2t0iu$ruj$1 digitaldaemon.com>, Walter says..."Peter Federighi" <pfederighi yahoo.com> wrote in message news:d2ovu8$2l1t$1 digitaldaemon.com...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. - PeterWalter: Thanks for responding. Here's the code I'm trying to work with. I'venotedwhere I try to call pow() and dmd doesn't do it correctly. I've alsotriedexplicitly 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 07 2005
"Peter Federighi" <pfederighi yahoo.com> wrote in message news:d32vvc$1md7$1 digitaldaemon.com...In article <d2t0iu$ruj$1 digitaldaemon.com>, Walter says...wrong."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'venotedwhere I try to call pow() and dmd doesn't do it correctly. I've alsotriedexplicitly casting the values I pass to pow, but dmd still gets itPlease post the exact code of that that didn't work.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.
Apr 15 2005