Efficient Power Calculation in C: Exponentiation with 2

  • Thread starter Thread starter martix
  • Start date Start date
  • Tags Tags
    Power
Click For Summary
The discussion centers on creating a program to compute powers of 2 using a non-standard method that involves checking each digit for overflow and building a result string through concatenation. The user raises concerns about handling cases where overflow occurs without a subsequent digit to add it to, suggesting the need to artificially add a digit. Key points include the comparison of performance between using arrays versus string concatenation and substrings, with a consensus that concatenation is inefficient in C due to frequent copying. The method proposed is considered slow compared to traditional mathematical approaches, particularly given that binary computers can utilize bit shift operations for rapid calculations of powers of 2. For large numbers, the discussion emphasizes that using an array of integers would likely be more efficient than string manipulation. Overall, leveraging established mathematical methods and binary operations is recommended for optimal performance.
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?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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