C++中的指数运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3775274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:41:13  来源:igfitidea点击:

Exponential Operator in C++

c++exponential

提问by James Hayek

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

我正在上 C++ 课程,我注意到只有几个数学运算符可以使用。我还注意到 C++ 的数学库中没有指数运算符。

Why must one always write a function for this? Is there a reason for the makers of C++ to omit this operator?

为什么必须始终为此编写一个函数?C++ 的制造者是否有理由省略这个运算符?

回答by paxdiablo

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

不会为此编写函数(当然,除非您疯了)。标题中定义了一个非常好的pow函数<cmath>

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

旁白:如果您尝试^用作电力运营商,正如某些人习惯做的那样,您会遇到令人讨厌的惊喜。它是异或 (XOR) 运算符(请参阅此处)。

回答by msw

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

大多数C操作容易意图映射到单个处理器指令当C被发明。当时,取幂不是机器指令,因此是库例程。

回答by PixelsTech

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

根据 Bjarne Stroustrup 在他的书C++ 的设计和演变中的说法。他们决定避免使用指数运算符,因为:

  • 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
  • 运算符提供符号方便,但不提供任何新功能。代表科学/工程计算重度用户的工作组成员表示,运算符语法提供了次要的语法便利。
  • 每个 C++ 用户都必须学习这个新功能
  • 用户强调了将他们自己专门的求幂函数替换为系统默认值的重要性,这对于内部运算符是不可能的
  • 该提案的动机不够充分。特别是,通过查看一个 30000 行的 Fortran 程序,您无法得出该运算符将在 C++ 中广泛使用的结论
  • 该提案需要添加一个新的运算符并添加另一个优先级,从而增加了语言的复杂性

回答by Manoj R

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.

您使用的是什么平台和编译器?我的猜测是 TurboC。大多数 cmath 文件具有其他编译器中涵盖的大多数数学函数。