Efficient Power Calculation in C: Exponentiation with 2

  • Thread starter Thread starter martix
  • Start date Start date
  • Tags Tags
    Power
Click For Summary
SUMMARY

The discussion focuses on efficient power calculation in C, specifically raising 2 to a specified power using a unique method that involves checking digits for overflow. The proposed method concatenates results into a string, but participants highlight its inefficiency compared to traditional mathematical methods. The bit shift operator, such as "1ULL << 60", is identified as a significantly faster alternative for calculating powers of 2. For large numbers, using an array of integers is recommended over string manipulation for better performance.

PREREQUISITES
  • Understanding of C programming language
  • Knowledge of bit manipulation techniques
  • Familiarity with string handling in C
  • Concepts of overflow in numerical calculations
NEXT STEPS
  • Research the use of the bit shift operator in C for power calculations
  • Explore efficient string handling techniques in C
  • Learn about mathematical methods for exponentiation, such as exponentiation by squaring
  • Investigate the performance implications of using arrays versus strings in C
USEFUL FOR

Software developers, particularly those working with C programming, computer scientists interested in numerical methods, and anyone optimizing performance in power calculations.

martix
Messages
167
Reaction score
5
I'm trying to write a program that raises 2 to the specified power... and I need help.
And I don't mean an ordinary 2*2*2, n times and such.

The method used is slightly strange(kinda backwards).
For ex. take 256, what the program should do is
1. Take 2, check if the next digit is greater than 4. As it is there is an overflow which gets added to the product ot the first digit*2
2. Put the result in a string that is built piece by piece with concatenation.
3. Repeat for the next digit until the end. Then do the same for the next power, up to the desired one.

Issues I'm not sure of. Say you reach 512 - there is no zero digit to add the overflow of the first one. My guess is - artificially add one, right?

Then I have some questions - which will be faster?
1. Arrays or concatenation and substrings(a function which btw I did not find in the standart libraries).
2. This method or the usual mathematical ones.
 
Technology news on Phys.org
martix said:
Issues I'm not sure of. Say you reach 512 - there is no zero digit to add the overflow of the first one. My guess is - artificially add one, right?

Yes, unless you're trying to calculate mod some power of 10.

martix said:
Then I have some questions - which will be faster?
1. Arrays or concatenation and substrings(a function which btw I did not find in the standart libraries).
2. This method or the usual mathematical ones.

I'm not sure what you mean by each of these:
* Most if not all methods for working with large numbers involve arrays on some level.
* Concatenation methods will be very slow in C, since you'll generally be forced to copy the string many times.
* Your method is slow, since it doesn't take advantage of the form of the problem or the fast machine instructions.
* There are many mathematical methods for calculating this, some of which are faster than others.

Since you're working on a binary computer, I'd expect the bit shift operator to be far faster than your method. "1ULL << 60" calculates 2^60 pretty quickly, around a nanosecond. Your method would take around a microsecond unless the cache slowed it down, in which case it might take 5-10 times as long.

Of course if you want to do this for really large numbers (ones that won't fit into a double word) you'll have to do some programming, but even then an array of integers would surely be much faster than a series of string arrays, right?
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
Replies
235
Views
15K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
12
Views
3K
Replies
4
Views
2K
Replies
29
Views
6K