Exponential Operator in C++

后端 未结 5 768
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 09:46

I am taking a class in C++ and I noticed there are only a few math operators to use. I also noticed that C++ does not come with an exponential operator within its math libr

相关标签:
5条回答
  • 2020-12-06 09:57

    You don't write a function for this (unless you're insane, of course). There's a perfectly good pow function defined in the <cmath> header.

    Aside: if you try to use ^ as a power operator, as some people are wont to do, you'll be in for a nasty surprise. It's the exclusive-or (XOR) operator (see here).

    0 讨论(0)
  • 2020-12-06 09:59

    Most C operations readily intended to mapped to a single processor instruction when C was invented. At the time, exponentiation was not a machine instruction, thus the library routine.

    0 讨论(0)
  • 2020-12-06 10:02

    What platform and which compiler are you using? My guess is TurboC. Mostly cmath file has most of the mathematical functions covered in other compilers.

    0 讨论(0)
  • 2020-12-06 10:03

    Python has the ** operator for exponentiation. In C++ you can actually define an operator like that with some trickery. By combining the unary * operator with the binary * operator, like this:

    #include <cmath>
    #include <iostream>
    
    struct Num {
        double value;
    
        Num(double value) : value(value) { }
    
        typedef Num* HalfStar;
    
        HalfStar operator*() const { return HalfStar(this); }
        Num operator*(const HalfStar& rhs) const
        {
            return Num(std::pow(value, rhs->value));
        }
        Num operator*(const Num& rhs) const
        {
            return Num(value * rhs.value);
        }
    
        friend std::ostream& operator<<(std::ostream& os, const Num& n)
        {
            return os << n.value;
        }
    };
    
    int main(int argc, char**argv)
    {
        Num a = 10;
        Num b = 9;
    
        std::cout << "a*b = " << (a*b) << "\n";
        std::cout << "a**b = " << (a**b) << "\n";
    
        return 0;
    }
    

    This will not work with non-class types though, so you can not do: x**2.

    See here for a live example.

    0 讨论(0)
  • 2020-12-06 10:09

    According to Bjarne Stroustrup in his book The design and evolution of C++. They decided to avoid exponential operator because :

    • An operator provides notational convenience, but does not provide any new functionality. Members of the working group, representing heavy users of scientific/engineering computation, indicated that the operator syntax provides minor syntactic convenience.
    • Every user of C++ must learn this new feature
    • Users have stressed the importance of susbtituting their own specialized exponentiation functions for the system default, which would not be possible with an intrinsic operator
    • The proposal is not sufficiently well motivated. In particular, by looking at one 30000 line Fortran program one cannot conclude that the operator would be widely used in C++
    • The proposal requires adding a new operator and adding another precedence level thus increasing the complexity of the language
    0 讨论(0)
提交回复
热议问题