简体   繁体   English

X等于y的幂,其中y为10或更高(我一直得到相同的结果)

[英]X to the power of y where y is 10 or higher (I keep getting the same result)

Alright, soo the task at hand is to compute 10^10 where the first 10 is an integer called "base" and the second 10 is an unsigned integer called "exponent". 好吧,当前的任务是计算10 ^ 10,其中前10个是称为“基数”的整数,而后10个是称为“指数”的无符号整数。

int base = 10; unsigned int exponent = 10; int i = 1; long long result = 1;
while(i<=exponent){
result = result*base;
i++;
}
printf("X^y = %ld\n\n", result);

Now I've tried this with a for loop, while loop, recursive functions etc. And my "result" value is still 1410065400 for some reason? 现在,我已经尝试过使用for循环,while循环,递归函数等。由于某种原因,我的“结果”值仍然是141006540​​0? It should be just a one with a bunch of zeros. 它应该只是带有一堆零的一个。 If anyone got a simple solution to this problem I would be very happy to hear it. 如果有人对这个问题有一个简单的解决方案,我将很高兴听到它。 And if this has been posted before (didn't find anything on here about it tho) then I would gladly accept the link to that solution. 如果以前已经发布过此内容(在此处找不到任何内容,那么),那么我很乐意接受该解决方案的链接。

As sad in comment, your format specifier is wrong in printf function. 遗憾的是,您的格式说明符在printf函数中是错误的。 It should be like: 应该是这样的:

printf("X^y = %lli\n", result);

lli is the correct format specifier. lli是正确的格式说明符。 Result: 结果:

X^y = 10000000000

Integer values in C programming language are 32 bits and it can hold values that contains 10 digits but 10^10 is 11 digit so it overflows. C编程语言中的整数值是32 bits ,可以容纳包含10位数字的值,但是10 ^ 10是11位数字,因此它会溢出。

You can learn about more from this question: 您可以从以下问题中了解更多信息:

What is the maximum value for an int32? int32的最大值是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM