473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Delphi Power arithmetic function in C#

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
10 5065
I take it that Math.Pow wasn't able to do what you wanted?
Mar 28 '06 #2
Nope, that is not the same function. I guess that the delphi Power
function does some logarithmic calculation...

Mar 28 '06 #3
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Zero | last post by:
Hi, I am calculating an integer to the pwer of a large integer, e.g. 2^5000. It turns out that the result I always get is zero. I am sure that the result is too large to store in such type...
32
by: chris.fairles | last post by:
Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I've done as much algorithmic changes as I can to reduce the amount of code, so now...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
1
by: Thomas Due | last post by:
Hi, I manage an rather old application in which we have some fairly complex (ugly) Delphi code. This is Delphi 6 we're talking about. Among all this Delphi code there is method for formating a...
9
by: suppamax | last post by:
Hi everybody! I'm writing a C program for a PIC18F microcontroller. I need to calculate a power function, in which both base and exponent are fixed point numbers (ex: 3.15^1.13). Using...
22
by: Yanb | last post by:
Hi, I'm new to c, so please excuse, if this is silly ;-) I made a C function, which takes IP adress from string and converts to unsigned long int. Everything works, but I found that the...
1
by: Jure Bogataj | last post by:
Hello! Does anybody knows how to handle this issue: I have an Delphi DLL with following two function declaration: function DeallocateString(lpszString : PChar) : DWORD; stdcall; function...
7
by: Ryanivanka | last post by:
hi, I am using a delphi DLL in vc++,static linked with ".h"and "lib". the export functions in DLL are "__stdcall",but the function doesn't work well,it often returns some weird values. when I add...
5
by: kelvin.koogan | last post by:
How can I call a function in a Delphi DLL from C++/CLI? The Delphi function is declared as follows: function Func1(IsDsb: Boolean; FirstStr, SecondStr : String): String; I've tried ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.