C ++ exponentiation operator

4

About a question recently posted and answered Is there any reason - historical or not - for C ++ (as well as many other programming languages) not to include an operator for exponentiation?

For example, when I want to calculate 5 to 7.2 I have to do:

#include <cmath>
[...]
resultado=pow(5,7.2);

And, if there were a op operator, it would simply do:

resultado=5 op 7.2;

The answer to this question should not only be based on personal opinions (these can be discussed in the footer in the comments) but rather try to include references that support any reasons.

    
asked by anonymous 11.12.2015 / 23:06

1 answer

4

The probable historical motive is because C does not. And it does not, because it was originally not meant to perform many mathematical operations.

Remember that the language was designed to write an operating system and not solve scientific problems. Remember that the language was meant to improve Assembly , which did not have this as an instruction.

In addition the machines of that time were simple and probably did not compensate to create an operator. Nor was it an ordinary operation either. It was very easy to burst the values that the processor worked naturally, complicating the algorithms in such a way that the function was the same.

We have already discussed the possibility of creating the operator for C ++. There are not enough advantages to introduce in language that is already complicated. It is not as trivial to implement it as it may seem, at least not in a language with this history.

If only the syntax is interesting, you can create it:

template<typename T>
T operator^(T x, T y) {
    return std::pow(x, y);
}

You need to know if you should. It's killing XOR. And keep an eye on the precedence table . It can get weird.

    
11.12.2015 / 23:19