By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,996 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,996 IT Pros & Developers. It's quick & easy.

Delphi Power arithmetic function in C#

P: n/a
I'm migrating an applicationf from Delphi5 to C# .net Framework 2.0,
but have some problems convertingt the Power function, because it is
not implemented in the framework Math library.
Does anyone have some code lines how to create an own Power function?

regards
Magnus

Mar 28 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I take it that Math.Pow wasn't able to do what you wanted?
Mar 28 '06 #2

P: n/a
Nope, that is not the same function. I guess that the delphi Power
function does some logarithmic calculation...

Mar 28 '06 #3

P: n/a
The Delphi source is in the Math.pas unit

Cheers
Doug Forster

"Magnus" <su*****@hotmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Nope, that is not the same function. I guess that the delphi Power
function does some logarithmic calculation...

Mar 29 '06 #4

P: n/a
Magnus <su*****@hotmail.com> wrote:
I'm migrating an applicationf from Delphi5 to C# .net Framework 2.0,
but have some problems convertingt the Power function, because it is
not implemented in the framework Math library.
Does anyone have some code lines how to create an own Power function?


For those of us who don't know Delphi, could you tell us what the Power
function actually does?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #5

P: n/a
I think it should be the same as the Math.Pow() method.
"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Magnus <su*****@hotmail.com> wrote:
I'm migrating an applicationf from Delphi5 to C# .net Framework 2.0,
but have some problems convertingt the Power function, because it is
not implemented in the framework Math library.
Does anyone have some code lines how to create an own Power function?


For those of us who don't know Delphi, could you tell us what the Power
function actually does?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 29 '06 #6

P: n/a
cody <de********@gmx.de> wrote:
I think it should be the same as the Math.Pow() method.


That's what I'd have thought, but another post from Magnus suggested
that it wasn't what was required.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #7

P: n/a
Hello,

Being an old delphi programmer myself i can shed some light on this.

The power function, does exactly what is says on the box, it raises a number
to a power specified

P int;
P := Power(3, 2); // 9 - 3 * 3
P := Power(2, 3); // 8 - 2 * 2 * 2

And so on.

So using the Math.Pow() function as suggested does exactly what the delphi
function does.

Regards
Scott Blood
C# Developer

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
cody <de********@gmx.de> wrote:
I think it should be the same as the Math.Pow() method.


That's what I'd have thought, but another post from Magnus suggested
that it wasn't what was required.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 29 '06 #8

P: n/a
Magnus wrote:
Nope, that is not the same function. I guess that the delphi Power
function does some logarithmic calculation...

The delphi power function does the same thing using natural log and e^x
functionality. I would expect that C# does the same logic behing the
scenes. The result is the same.
Mar 29 '06 #9

P: n/a
Thank you all, here is my c# contribution:

public static double MathPower(double basenum, double exponent)
{
if(exponent == 0)
return 1;
else if(basenum == 0 && exponent > 0)
return 0;
else if((basenum - (int)basenum) == 0 && Math.Abs(exponent)
<= int.MaxValue)
return Math.Pow(basenum, (int)Math.Truncate(exponent));
else
return Math.Exp(exponent * Math.Log(basenum, Math.E));
}

The Math.Pow is the same method as the Delphi-method IntPow

/Magnus - Thanks to guys in a Delphi newsgroup, i found the source code
in math.pas

Mar 30 '06 #10

P: n/a
Magnus wrote:
Thank you all, here is my c# contribution:

public static double MathPower(double basenum, double exponent)
{
if(exponent == 0)
return 1;
else if(basenum == 0 && exponent > 0)
return 0;
else if((basenum - (int)basenum) == 0 && Math.Abs(exponent)
<= int.MaxValue)
return Math.Pow(basenum, (int)Math.Truncate(exponent));
else
return Math.Exp(exponent * Math.Log(basenum, Math.E));
}
It doesn't look to me like that does anything that Math.Pow doesn't do.
The Math.Pow is the same method as the Delphi-method IntPow


No, it's not. Math.Pow copes with floating point exponents just fine.
Could you give an example where just using Math.Pow doesn't do what you
want it to? Here's an example showing Math.Pow giving the same answer
as MathPower for non-integral values:

using System;

class Test
{
static void Main()
{
Console.WriteLine (MathPower (15.2, 2.3));
Console.WriteLine (Math.Pow (15.2, 2.3));
}

public static double MathPower(double basenum, double exponent)
{
if(exponent == 0)
return 1;
else if(basenum == 0 && exponent > 0)
return 0;
else if((basenum - (int)basenum) == 0 && Math.Abs(exponent)
<= int.MaxValue)
return Math.Pow(basenum, (int)Math.Truncate(exponent));
else
return Math.Exp(exponent * Math.Log(basenum, Math.E));
}
}

Jon

Mar 30 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.