Exponential Operator in C++

独自空忆成欢 提交于 2019-11-28 00:40:29
paxdiablo

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).

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.

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!