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

Home Posts Topics Members FAQ

Using power function with big numbers

5 New Member
Hi all,

Need the community great support once more. :)

I need to know how to calculate the power of some numbers in C, the problem is that the number is too big , and the compiler gives a error when doing the arithmetics.

See the power function I am using:

unsigned long int power(int base, int n)
{
unsigned long long int p;
int i;

p = 1;

for (i = 1; i <= n; ++i)
p = p * base;
return p;
}



Passing the parameter 2 as base, I can't execute this function with a n greater than 32... the problem is that I need to calculate from 2**0 to 2**64.

Hope I made myself clear about what I need
Any suggestions?

Thanks!
Gus
Jan 7 '07 #1
5 4800
willakawill
1,646 Top Contributor
Hi all,

Need the community great support once more. :)

I need to know how to calculate the power of some numbers in C, the problem is that the number is too big , and the compiler gives a error when doing the arithmetics.

See the power function I am using:

unsigned long int power(int base, int n)
{
unsigned long long int p;
int i;

p = 1;

for (i = 1; i <= n; ++i)
p = p * base;
return p;
}



Passing the parameter 2 as base, I can't execute this function with a n greater than 32... the problem is that I need to calculate from 2**0 to 2**64.

Hope I made myself clear about what I need
Any suggestions?

Thanks!
Gus
Hi Gus, the answer is in the question. The built in data type that can handle 2^64 (64 bits) is a double
Jan 8 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
Hi Gus, the answer is in the question. The built in data type that can handle 2^64 (64 bits) is a double
Erm many systems today also support a 64 bit integer type as well, for instance

long long - *nix
__int64 - Windows
Jan 8 '07 #3
willakawill
1,646 Top Contributor
Erm many systems today also support a 64 bit integer type as well, for instance

long long - *nix
__int64 - Windows
Interesting. Are these built in data types for every system. i.e. are they portable?
Jan 8 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
Interesting. Are these built in data types for every system. i.e. are they portable?
Not really, but then strictly speaking neither is any type.

That is you have no guarentee of the number of bits in a long in any system, however you are guarenteed that a type long does exist.


I suspect that it is possible that as more 64 bit systems emerge the standards will be updated.

Of course what should have happened is that for 64 bit systems

char = 8 bits (actually this is an assumption)
short = 16 or 32 bits
int = 32 or 64 bits
long = 64 bits

This would all be within the constraints of the C standard, unfortuneately the compiler writers were so used to the convention

char = 8 bits (actually this is an assumption)
short = 16
int = 16 or 32 bits
long = 32 bits

that they didn't feel they could alter the number of bits in a long or short even though they are not set by the standard which resulted in them all having to introduce a 64 bit integer extension type.

It's a bit of a mess that needs resolving.
Jan 8 '07 #5
Gus007
5 New Member
Thanks for the answers!

As you can see I was using 'long long" in my implementation so this was supposed to work if I was a Unix user,right? Since I am a windows user this is probably the problem.

I didn't know that "long long" was system specific...
Gus
Jan 10 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1534
by: Roie Kerstein | last post by:
Hello! I want to compute a**b%c for very long numbers. Computing a**b and then applying modulu is not practical, since it takes ages. There is a built-in optional parameter to the long.__pow__(a,b), and it works well. My question is: How can I write it is a form of an expression, like a**b? I prefer not to fill my script with calls of the form long.__pow__(a,b,c).
4
10767
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group consisting of 6 numbers - with a total of 50 groups of numbers. A well-known girl that some publishing companies use to provide introductory level textbooks to various Junior Colleges in the U.S., not surprisngly, asks for this same exact...
9
2214
by: Aaron Gallimore | last post by:
Hi, Pretty simple one I think...Is there a "power of" or squared function in C++. This is what i'm trying to achieve. (array-array)*2 this doesnt work with minus numbers so i need a square or power function. also.. Is there a "sum of" function. Ultimately i'm trying to do a euclidean
7
3251
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI library, which is based on GTK. Depending on which libraries I link in, I can have either a GTK1 or GTK2 user interface. I'm finding that the simulation behaves differently depending on whether I link with GTK1 or GTK2.
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;
14
3137
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are very unhappy with the idea and the compiler refuses to play ball with any of the attempts I have made to build such a structure. There is no problem when using a single string but a two dimensional array of strings appears to be a very different...
7
13952
by: yodadbl07 | last post by:
hey im trying to write a class of polynomials using a linked list. But I am getting an error at run time with my Polynomial* getNext() function. The error says access violation reading location. Im not sure what im doing wrong. #ifndef POLYNOMIAL_H #define POLYNOMIAL_H class Polynomial { public: Polynomial( ){} Polynomial(int theCoefficient);
20
10743
by: ravi | last post by:
Give a one-line C expression to test whether a number is a power of 2.
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?
0
9690
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
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...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.