473,800 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5104
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*****@hotmai l.com> wrote in message
news:11******** **************@ t31g2000cwb.goo glegroups.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*****@hotmai l.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.co m>
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.co m> schrieb im Newsbeitrag
news:MP******** *************** *@msnews.micros oft.com...
Magnus <su*****@hotmai l.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.co m>
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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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(doubl e basenum, double exponent)
{
if(exponent == 0)
return 1;
else if(basenum == 0 && exponent > 0)
return 0;
else if((basenum - (int)basenum) == 0 && Math.Abs(expone nt)
<= int.MaxValue)
return Math.Pow(basenu m, (int)Math.Trunc ate(exponent));
else
return Math.Exp(expone nt * Math.Log(basenu m, 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
5644
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 as u_int64_t and long int etc. My power function is as follows: for(i=0; i<5000; i++) result *= 2;
32
3350
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 I'm turning to micro-optimizations. One function that gets run a bazillion times is the pow() function from math.h. However I've realized probably 90% of the time, the exponent will be 0. 99.9% of the time, the exponent will lie between -3 and...
26
3065
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
1
8084
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 print layout for slip printers. Pass a few parameters to this method and it returns a Delphi string which contains the entire slip, ready for sending to the slip printer. Now, we're slowly migrating to .NET and am in need of printing this slip...
9
7006
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 pow() function is too expensive... Is there another way to do that?
22
1709
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 "counting" part part works only with this: numericip=atoi(textip)*256*256*256+atoi(textip)*256*256+atoi(textip)*256+atoi(textip); When I wrote *(256^3), I got fake results. I'd like to use something more sophisticated, instead of 256*256 *256, which...
1
10706
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 MyFunc1(lpszInput : PChar; var lpszOutput : PChar) : DWORD; stdcall;
7
2070
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 codes as follows,it suddenly works well. why?? DWORD returnAdd; __asm { mov ecx,
5
3868
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 extern "C" char *Func1(int isDsb, char *firstString, char
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.