本文介绍了C/C ++中两个INT_MAX numbes的乘积不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的情况下,两个INT_MAX数字的乘积为296447233,这是不正确的.

In my case, product of two INT_MAX numbers is 296447233, which is incorrect.

long long int product = 0;
product = 2137483647 * 2137483647;
printf("product: %lli\n", product);

我在做错什么,以及如何纠正它?谢谢!

What I am doing wrong, and how to correct it ??Thanks !

推荐答案

您的2137483647都属于int类型.因此,它们保持该类型并溢出.

Both of your 2137483647 are of type int. So they stay that type and overflow.

让他们long long s:

product = 2137483647LL * 2137483647LL;

或演员:

product = (long long)2137483647 * 2137483647;

这篇关于C/C ++中两个INT_MAX numbes的乘积不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:30