为什么这个函数没有超过值 2^31?

发布于 2024-12-23 16:32:37 字数 1181 浏览 6 评论 0原文

幂函数(用 C++ 编写)...

long power (long a, long b){
    long result=1l;
    for (int i = 0;i<b;i++){
        result*=a;
    }
    return result;
}

现在我做了一些输出测试...

cout<<power(2l,2l)<<endl;
cout<<power(2l,4l)<<endl;
cout<<power(2l,31l)<<endl;
cout<<power(2l,32l)<<endl;
cout<<power(2l,61l)<<endl;

输出:

4
16
-2147483648
0
0

嗯,长时间回落到 32 位大小(而不是保持 64 位大小)似乎存在一些问题。我想知道为什么这不起作用,但如果我使用 long long 类型,一切正常。

一些额外信息:

我正在使用 C++ 和编译器 MinGW
我正在运行 64 位操作系统 (Windows 7)

更新:

你们太棒了!没想到竟然会发生这样的事情。

我刚刚使用 sizeof 检查了一些任意 PDT,这就是我发现的...

cout<<sizeof(long)<<" "<<sizeof(int)<<" "<<sizeof(char)<<" "<<sizeof(long long)<<" "<<sizeof(uint64_t)<<endl;

输出:

4 4 1 8 8

所以,看起来我的 longint 是大小均为 32 位。进一步研究表明 intmax_t 类型也是 64 位。实际上每个 PDT 的上限都是 64 位,因此如果我需要表示 128 位整数,C++ 是否有一个内置类(类似于 Java 中的 BigInteger )?

The power function (written in c++)...

long power (long a, long b){
    long result=1l;
    for (int i = 0;i<b;i++){
        result*=a;
    }
    return result;
}

Now I do some output testing...

cout<<power(2l,2l)<<endl;
cout<<power(2l,4l)<<endl;
cout<<power(2l,31l)<<endl;
cout<<power(2l,32l)<<endl;
cout<<power(2l,61l)<<endl;

Output:

4
16
-2147483648
0
0

Well there seems to be some problem with the long falling back to a 32 bit size (instead of staying as a 64 bit). I'm wondering why this doesn't work, yet if I use the long long type, everything works fine.

Some extra info:

I'm using C++ and the compiler MinGW
I am running a 64-bit OS (Windows 7)

UPDATE:

You guys are awesome! Never thought that this type of thing would be going on.

I just checked some arbitrary PDTs using sizeof and this is what I found...

cout<<sizeof(long)<<" "<<sizeof(int)<<" "<<sizeof(char)<<" "<<sizeof(long long)<<" "<<sizeof(uint64_t)<<endl;

Output:

4 4 1 8 8

So, it looks like my long and int are both 32 bit in size. Some more playing around shows that the intmax_t type is also 64 bit. Practically every single PDT is capped at 64 bits, so if I ever needed to represent a 128-bit integer, does c++ have a built in class for that (something similar to BigInteger in Java)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若沐 2024-12-30 16:32:37

显然,在您的环境中,long 类型是 32 位。

为了解决类似的问题,我建议您使用像 uint64_t 这样的类型,而不是依赖于本机类型具有特定大小的假设。

编辑

回答你的第二个问题(c++有一个用于128位整数的内置类吗?):不,没有。或者更确切地说,它没有强制要求这样做。但是,如果实现可以提供这样的功能,您就可以使用诸如 uint128_t 之类的东西。但就我个人而言,我还没有看到任何系统可以做到这一点。不过,有像 GMP 这样的第三方库提供了该功能。

Apparently, the type long is 32 bits in your environment.

To get around similar problems, I would suggest that you use types like uint64_t instead of relying in the assumption that a native type has a specific size.

EDIT

To answer your second question (does c++ have a built in class for 128-bit integers?): No, it does not. Or rather, it does not mandate one. However, if an implementation would provide one you would be able to use something like uint128_t. Personally, I haven't seen any system that does this, though. There are third party libraries like GMP that provide that functionality, though.

公布 2024-12-30 16:32:37

A.尝试运行:

cout<<sizeof(long);

看看它是32位的。

B.我猜是你的项目定义有问题。即使您在 64 位机器上工作,它也可以编译 32 位程序,因此您可以在 32 位和 64 位机器上使用它。
long 始终是指针的大小...

C.uint64_t 是最佳实践。

A. trying running:

cout<<sizeof(long);

To see that it's a 32 bit.

B. I guess it is a problem with the definition of your project. even if you work on a 64 bit machine it can compile a 32 bit program so you can use it both on 32 and 64 but machines.
and long is always the size of a pointer...

C. uint64_t is the best practice.

栩栩如生 2024-12-30 16:32:37

那么 long 类型需要 32 位,因此最大正值是 4294967296-1。但是你的函数计算值5842587018385982521381124421=21^21。

Well long type takes 32 bits, so max positive value is 4294967296-1. But your function calculates value 5842587018385982521381124421=21^21.

静若繁花 2024-12-30 16:32:37

在 C++ 和 C 中,int 的大小取决于体系结构,这都是事实,但问题是 32 位有符号 int 的大小介于-2^31 和 (2^31-1),不是 2^31。您确实溢出了 32 位数字。您应该使用 unsigned int 来代替。介于 0 和 (2^32-1) 之间。

In C++, as well as in C, size of int is architecture-dependent, and that's all true, but that catch is that 32-bit signed int's go between -2^31 and (2^31-1), not 2^31. You are indeed overflowing a 32-bit number. You should use an unsigned int instead. That goes between 0 and (2^32-1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文