何时使用std :: pow(x,n)而不是x次n次? [英] When to use std::pow(x,n) instead of times x for n times?

查看:78
本文介绍了何时使用std :: pow(x,n)而不是x次n次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道是否有任何关于何时使用

的一般准则

std :: pow(x,n)

而不是

x * x * x * ... * x(n x's)。


谢谢,


Hi,

I''m wondering if there is any general guideline on when to using
something like
std::pow(x, n)
rather than
x * x * x * ... * x (n x''s).

Thanks,
Peng

推荐答案

2008年9月9日星期二17:30:04 -0700(PDT),Peng Yu< Pe ******* @ gmail.com>

在comp.lang.c ++中写道:
On Tue, 9 Sep 2008 17:30:04 -0700 (PDT), Peng Yu <Pe*******@gmail.com>
wrote in comp.lang.c++:




我想知道是否有关于何时使用

类似
$ b的一般准则$ b std :: pow(x,n)

而不是

x * x * x * ... * x(n x's)。


谢谢,


Hi,

I''m wondering if there is any general guideline on when to using
something like
std::pow(x, n)
rather than
x * x * x * ... * x (n x''s).

Thanks,
Peng



除了Alf所说的,这里是我的建议:


总是使用std :: pow(x,n)而不是试图在n时多次将x乘以

不是不可或缺的。


-

杰克Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http:// www。 club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

In addition to what Alf said, here''s my advice:

ALWAYS use std::pow(x, n) instead of trying to multiply ''x'' by
multiple times when ''n'' is not integral.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


2008年9月9日星期二17:30:04 -0700 ,Peng Yu写道:
On Tue, 09 Sep 2008 17:30:04 -0700, Peng Yu wrote:




我想知道是否有任何关于何时使用的一般准则

类似

std :: pow(x,n)

而不是

x * x * x * ... * x(n x's)。


谢谢,


Hi,

I''m wondering if there is any general guideline on when to using
something like
std::pow(x, n)
rather than
x * x * x * ... * x (n x''s).

Thanks,
Peng



我t很难以x * x表示法表示分数幂。


sf

It can be hard to represent fractional powers in x*x notation.

sf


Peng Yu写道:
Peng Yu wrote:

我想知道是否有关于何时使用

类似

std :: pow的一般指导原则x,n)

而不是

x * x * x * ... * x(n x'')。
I''m wondering if there is any general guideline on when to using
something like
std::pow(x, n)
rather than
x * x * x * ... * x (n x''s).



如果你需要每秒数百万次计算这个函数,那么使用后者的形式可以大大加快到某一时间n

(之后std :: pow()变得更快)。

后者形式比std :: pow()快的最大n可能会非常大,

取决于代码(例如n = 8之类的东西)可能不是牵强附会。


当然这是系统性的,所以没有规则。

If you need to calculate that function millions of times per second,
then using the latter form can be considerably faster up to a certain n
(after which std::pow() becomes faster). The maximum n for which the
latter form is faster than std::pow() can be surprisingly large,
depending on the code (eg. something like n=8 might not be far-fetched).

Of course this is heavily system-dependent so there''s no rule.


这篇关于何时使用std :: pow(x,n)而不是x次n次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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