为什么C ++没有幂运算符? [英] Why doesn't C++ have a power operator?

查看:177
本文介绍了为什么C ++没有幂运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多语言都有幂运算符;为什么C ++没有?例如,Fortran和Python使用**,并且通常使用^编写(例如,在LaTeX中).

Many languages have a power operator; why doesn't C++? For example, Fortran and Python use ** and is commonly written (in LaTeX, for example) using ^.

推荐答案

C ++确实具有幂运算符—编写为pow(x, y).

C++ does have a power operator—it's written pow(x, y).

最初,C在设计时就考虑了系统软件,并且 不需要动力操作员. (但它有 &|之类的按位运算符 其他语言.)有人在讨论添加一种 在C ++标准化期间,但最终共识是更多 或更少:

Originally, C was designed with system software in mind, and there wasn't much need for a power operator. (But it has bitwise operators, like & and |, which are absent in a lot of other languages.) There was some discussion of adding one during standardization of C++, but the final consensus was more or less:

  • 它不能是^,因为优先级是错误的(以及 当然,具有2. ^ 8 == 256.,但是2 ^ 8 == 10不是很 要么愉快).

  • It couldn't be ^, because the priority was wrong (and of course, having 2. ^ 8 == 256., but 2 ^ 8 == 10 isn't very pleasant either).

不能为**,因为那样会破坏现有的 程序(可能具有x**p之类的内容,其中xintpint*).

It couldn't be **, because that would break existing programs (which might have something like x**p, with x an int, and p an int*).

可能是*^,因为此序列当前不合法 在C或C ++中.但这仍然需要引入 其他优先级.

It could be *^, because this sequence isn't currently legal in C or C++. But this would still require introducing an additional level of precedence.

C和C ++已经具有足够的特殊标记和级别的 优先事项,以及与数字社区的讨论之后, 得出的结论是,确实没有任何问题 pow(x, y).

C and C++ already had enough special tokens and levels of precedence, and after discussions with the numerics community, it was concluded that there really wasn't anything wrong with pow(x, y).

因此C ++保持原样,这似乎没有 造成了任何问题.

So C++ left things as they were, and this doesn't seem to have caused any problems.

这篇关于为什么C ++没有幂运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆