473,396 Members | 1,995 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,396 software developers and data experts.

Using power function with big numbers

5
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 4763
willakawill
1,646 1GB
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 Expert Mod 8TB
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 1GB
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 Expert Mod 8TB
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
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
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...
4
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...
9
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...
7
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...
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...
14
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...
7
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...
20
by: ravi | last post by:
Give a one-line C expression to test whether a number is a power of 2.
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...

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.