int的取幂函数? [英] An exponentiation function for int?

查看:70
本文介绍了int的取幂函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一路上是否弄乱了什么,或者标准C ++中没有任何功能

将(无符号)int提升为(unsigned)int返回的功能

(无符号)int?直到今天,我再也没想过这个想法。我试着用b $ b做它,然后发现< cmath> std :: pow()只为第一个参数采用浮点类型

。我当然可以写一个。我可以用

写出至少3个根本不同的版本,在发现它的时间里没有这个功能。

-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论就构成了数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell

Did I mess something along the way, or is there no function in Standard C++
to raise an (unsigned) int to a power of (unsigned) int returning
(unsigned) int? I never gave this a second thought until today. I tried to
do it, and discovered <cmath> std::pow() only takes floating point types
for the first argument. Sure I could write one. I could have written at
least 3 fundamentally differnet versions in the time it took to discover
there isn''t such a function.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

推荐答案

* Steven T. Hatton:
* Steven T. Hatton:
我是否在整个过程中弄乱了一些东西,或者标准C ++中没有函数将(无符号)int提升为(unsigned)int的函数返回
(unsigned)int?直到今天,我再也没想过这个想法。我试着去做,然后发现< cmath> std :: pow()只为第一个参数采用浮点类型。我当然可以写一个。我可以写出至少3个根本不同的版本,在发现它的时间里没有这样的功能。
Did I mess something along the way, or is there no function in Standard C++
to raise an (unsigned) int to a power of (unsigned) int returning
(unsigned) int? I never gave this a second thought until today. I tried to
do it, and discovered <cmath> std::pow() only takes floating point types
for the first argument. Sure I could write one. I could have written at
least 3 fundamentally differnet versions in the time it took to discover
there isn''t such a function.




没有这样的功能。有C库pow,C ++库

valarray :: pow和C ++库complex :: pow。写一个无符号整数

版本应该如你所说的那样相当微不足道;为什么不包装C pow?


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



There isn''t such a function. There is the C library pow, the C++ library
valarray::pow and the C++ library complex::pow. Writing an unsigned integer
version should, as you state, be fairly trivial; why not just wrap the C pow?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Steven T. Hatton写道:
Steven T. Hatton wrote:
我是否在路上弄脏了东西,或者标准C ++中没有功能
将(unsigned)int提升为(unsigned)int的函数返回
(unsigned)int?直到今天,我再也没想过这个想法。我试着去做,然后发现< cmath> std :: pow()只为第一个参数采用浮点类型。我当然可以写一个。我可以写出至少3个根本不同的版本,在发现它的时间里没有这样的功能。
Did I mess something along the way, or is there no function in Standard C++
to raise an (unsigned) int to a power of (unsigned) int returning
(unsigned) int? I never gave this a second thought until today. I tried to
do it, and discovered <cmath> std::pow() only takes floating point types
for the first argument. Sure I could write one. I could have written at
least 3 fundamentally differnet versions in the time it took to discover
there isn''t such a function.




没错,没有一个。没有那么多的要求

我会怀疑。在开始溢出之前,你不必将数字提高到一个非常高的价值。


您要么编码速度快,要么需要学习如何更快地阅读文档。

加载标准的PDF只需要大约20秒钟,并且在26.5中找到
,其中它表示这些是重载for pow:

pow(float,float);

pow(float,int)

pow(double,double)

pow(double,int)

pow(长双,长双)

pow(long double,int)


我怀疑你的整数战力只是迭代进行取幂。

Pow通常使用一个日志(这是唯一真正的方法来做一个小数

指数无论如何),它给出了一个更恒定的时间。 br />



Correct, there isn''t one. There just isn''t that much call for it
I would suspect. You don''t have to raise a number to a very high
power before it starts overflowing.

You either code fast, or you need to learn how to read the docs faster.
It only took me about 20 seconds to load up the PDF of the Standard and
find in 26.5 where it says that these are the overloads for pow:
pow(float, float);
pow(float, int)
pow(double, double)
pow(double, int)
pow(long double, long double)
pow(long double, int)

I suspect your "integer pow" just iterates to do the exponentiation.
Pow typically uses a log (which is the only real way to do a fractional
exponent anyhow) which gives a more constant time.


Ron Natalie写道:
Ron Natalie wrote:
Steven T. Hatton写道:
Steven T. Hatton wrote:
我是不是弄乱了方式,或者标准的C ++中是否没有函数将(unsigned)int提升为(unsigned)int的函数返回
(unsigned)int?直到今天,我再也没想过这个想法。我试着去做,然后发现< cmath> std :: pow()只为第一个参数采用浮点类型
。我当然可以写一个。我可以写出至少3个根本不同的版本,在发现它的时候没有这样的功能。
正确,没有一个。没有那么多要求它
我怀疑。在开始溢出之前,你不必将数字提升到很高的功率。
Did I mess something along the way, or is there no function in Standard
C++ to raise an (unsigned) int to a power of (unsigned) int returning
(unsigned) int? I never gave this a second thought until today. I tried
to do it, and discovered <cmath> std::pow() only takes floating point
types
for the first argument. Sure I could write one. I could have written at
least 3 fundamentally differnet versions in the time it took to discover
there isn''t such a function.
Correct, there isn''t one. There just isn''t that much call for it
I would suspect. You don''t have to raise a number to a very high
power before it starts overflowing.




我正在做的是严格的整数运算。我严重怀疑我会进入

足够等级和顺序的张量,以溢出long int与索引

范围。

您要么快速编码,或者你需要学习如何更快地阅读文档。
我只花了大约20秒来加载标准的PDF并在26.5中找到它们说这些是pow的重载:
pow(float,float);
pow(float,int)
pow(double,double)
pow(double,int)
pow(long double) ,long double)
pow(long double,int)


我从错误输出中读取。我并不需要grep标准,但是

我也确实在那里查找它。它告诉我什么/是/那里,但不是什么

不是。有时标准是一个有用的参考,并且我在其中重新定义''定义''的部分运行

并继续使用

重新定义的含义不是一致的方式。

我怀疑你的整数战力只是迭代进行取幂。


不,我实际上使用了编译时间递归。

Pow通常使用一个日志(这是唯一真正做分数
指数的方法无论如何)这给了一个更恒定的时间。



What I''m doing is strictly integer math. I seriously doubt I will get into
tensors of sufficient rank and order to overflow long int with the index
range.
You either code fast, or you need to learn how to read the docs faster.
It only took me about 20 seconds to load up the PDF of the Standard and
find in 26.5 where it says that these are the overloads for pow:
pow(float, float);
pow(float, int)
pow(double, double)
pow(double, int)
pow(long double, long double)
pow(long double, int)
I read that from the error output. I didn''t need to grep the standard, but
I did look it up there as well. It tells me what /is/ there, but not what
isn''t. Sometimes the Standard is a useful reference, and othertimes I run
into the parts where is redefines ''definition'' and proceeds with the
redefined meaning in a less than consistent manner.
I suspect your "integer pow" just iterates to do the exponentiation.
No, I actually used compile time recursion.
Pow typically uses a log (which is the only real way to do a fractional
exponent anyhow) which gives a more constant time.




那是因为你可以在一次操作中添加指数而不是执行

n + m执行蛮力计算所需的单独操作。

浮点数用硬件表示为有效数和

(基数)和指数。加法和减法实际上比乘法和除法更昂贵




我不知道浮点数和2''之间的转换成本是多少s

补充是,但是如果我使用pow()来做我的整数数学,我怀疑它可以加/ b
加起来。在我做出决定之前,我真的要测试它。这个

可能比编译器或操作系统敏感的硬件更敏感。

-

如果我们的假设是关于任何事情而不是关于一些或多个特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell



That''s because you can add exponents in one operation rather than performing
n + m individual operations needed to do the brute force calculation.
Floating point numbers are represented in hardware in terms of significand
(base) and exponent. Addition and subtraction are actually more expensive
than multiplication and division.

I''m not sure what the cost of converting between floating point and 2''s
complement is, but if I use pow() to do my integer math, I suspect it can
add up. I really have to test it before I can make a determination. This
is probably hardware sensitive more so than compiler or OS sensitive.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell


这篇关于int的取幂函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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