Delphi Power arithmetic function in C#

M

Magnus

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
 
M

Magnus

Nope, that is not the same function. I guess that the delphi Power
function does some logarithmic calculation...
 
J

Jon Skeet [C# MVP]

Magnus said:
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?
 
J

Jon Skeet [C# MVP]

cody said:
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.
 
S

scott blood

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
 
J

Jeff

Magnus said:
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.
 
M

Magnus

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
 
J

Jon Skeet [C# MVP]

Magnus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top