Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raising to powers in C++ vs. Python

Coming from Python, I've noticed things in C++ tend to be a little more complicated. A good example would be raising numbers to a power. In Python's math library, all that is needed is:

a = a**b

However, in C++ I've found explanations in online documentation such as.....

//float
pow( float base, float exp );
//double      
pow( double base, double exp );
//long double 
pow( long double base, long double exp );
//float
pow( float base, int iexp );
//double      
pow( double base, int iexp );
//long double 
pow( long double base, int iexp );
//promoted    
pow( Arithmetic1 base, Arithmetic2 exp );

Obviously the creators of C++ must have had great reasons for making it this way, but as a new programmer, those reasons elude me. Does this give greater flexibility over the answer? What benefits have I been given here in C++ in terms of powers?

like image 462
Jessiah Burgess Avatar asked Dec 29 '15 20:12

Jessiah Burgess


People also ask

Can you do powers in C?

Power Function in C/C++ Given two numbers base and exponent, pow() function finds x raised to the power of y i.e. xy. Basically in C exponent value is calculated using the pow() function. pow() is function to get the power of a number, but we have to use #include<math.

What is raised to the power in Python?

The ** operator in Python is used to raise the number on the left to the power of the exponent of the right. That is, in the expression 5 ** 3 , 5 is being raised to the 3rd power. In mathematics, we often see this expression rendered as 5³, and what is really going on is 5 is being multiplied by itself 3 times.

Does Python have a power function?

Python pow() Function The pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z.

How do you write to the power of 2 in Python?

The power operator ( ** ) raises the left value to the power of the second value. For example: 2 ** 3 . The built-in pow() function does the same thing: it raises its first argument to the power of its second argument. Like this: pow(2, 3) .


2 Answers

The multiple declarations you found are a result of C++ having static types vs. Python's dynamic types. In Python when you call a function on a value it can determine at runtime if you are calling it on an integer, float, or some more complex object and take the appropriate actions. In C++ you can't call the same function with different types so you must declare a different version of the function for all the types you wish to support. However unlike in C, you can have functions with the same name that differ only in their parameter signature, (as Untitled123 points out, this is called function overloading). This actually makes your life easier since you can just call pow(a,b) without having to worry about which special function to call for the types you want (e.g. pow_float_int(a,b) or pow_float_float(a,b)), the compiler will call the correct version for you based upon the types of the arguments.

like image 63
Turn Avatar answered Oct 22 '22 03:10

Turn


Numbers in python have no limit. Some consequences that arise out of this is that programs tend to be slower since python is not statically typed, and has more overhead since it does not know if a number is within a certain range. In short, the main reason for using overloading functions in that way is for speed purposes, and it's easier to reason about what's happening under the hood. Python provides a lot of conveniences, but sometimes become very difficult to reason about.

Secondly, most languages have a math.pow function (Java also for example), and while it's a bit verbose, I think it's still very easy to understand. Note that when you call pow() in C++, the compiler will figure out which of the pow(some data type, some data type) should be called and there is no need to worry over the multiple definitions.

like image 41
Untitled123 Avatar answered Oct 22 '22 02:10

Untitled123