Empowerment in C ++ and Python

Based on Python, I noticed that things in C ++ are usually a bit more complicated. A good example would be a rise in power. In the Python math library, all you need is:

a = a**b 

However, in C ++, I found explanations in the online documentation, for example .....

 //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 this, but as a new programmer, these reasons elude me. Does this give more flexibility in response? What benefits did I get here in C ++ in terms of authority?

+5
source share
4 answers

Numbers in python have no limits. Some of the consequences that arise from this are that programs are generally slower because python is not statically typed and has more overhead because it does not know if the number is in a certain range. In short, the main reason for using overload functions in this way is for speed purposes, and it’s easier to talk about what happens under the hood. Python provides many amenities, but sometimes it becomes very difficult to reason.

Secondly, most languages ​​have a math.pow function (like Java), and although 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 determine which of pow (some data type, some data type) should be called, and there is no need to worry about several definitions.

+4
source

Several declarations found are the result of C ++ having static types and Python dynamic types . In Python, when you call a function by a value that it can determine at runtime, if you call it on an integer, float, or some more complex object and take the appropriate action. In C ++, you cannot call the same function with different types, so you must declare a different version of the function for all types that you want to support. However, unlike C, you can have functions with the same name that differ only in their parameter signature (as Untitled123 notes, this is called function overloading ) It actually makes your life easier, since you can just call pow(a,b) without worrying about which special function to call for the types you need (e.g. pow_float_int(a,b) or pow_float_float(a,b) ), the compiler will call the correct version for you by argument type.

+7
source

The benefits of having multiple function signatures are optimized compilation times and runtime decisions.

In Python, operation ** looks at its arguments at runtime and decides which algorithm to apply. An integer exponent compared to a rational exponent versus arbitrary indefinite accuracy with unlimited accuracy versus a complex exponent compared to any other. This decision takes time.

In C ++, when the compiler sees pow(x, y) , it knows the x and y types at compile time, so it can choose the best pow() match. This, in turn, allows the compiler to avoid any decision at runtime. Depending on how the headers are written and / or the overall optimization of the program is written, the compiler can even inline the call and avoid any runtime costs other than the actual power calculation.

This usually does not really matter. However, if your pow() call is in a tight loop with millions of iterations, this can be significant.

+3
source

as you know in C ++, we use a function for Power instead of operators (e.g. python)

you can use pow just like the examples above. There are several modes of using this function, and each time you send it different variables, the system decides which mode to choose. pow (float base, float exp); and pow (double base, double exp); do not match, and each of them is a separate function

Now that you are switching to Python, there is exactly the same state. ** The operator is similar to C ++ functions, which have different modes. Just like defining operators in C ++

I hope you understand my advice

+1
source

Source: https://habr.com/ru/post/1239448/


All Articles