473,813 Members | 3,361 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
10 5106
Magnus wrote:
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));
}
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.WriteLi ne (MathPower (15.2, 2.3));
Console.WriteLi ne (Math.Pow (15.2, 2.3));
}

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));
}
}

Jon

Mar 30 '06 #11

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
3068
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
7007
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
1712
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
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10669
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
10407
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...
0
10140
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
9224
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
7684
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
5706
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3885
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
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.