将整数提升到C中另一个整数的幂的最有效方法是什么?
// 2^3
pow(2,3) == 8
// 5^5
pow(5,5) == 3125
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n,i,ele;
n=5;
ele=pow(n,2);
printf("%d",ele);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是24.
我在Code :: Blocks中使用GNU/GCC.
怎么了?
我知道pow函数返回一个double,但是25适合一个int类型所以为什么这个代码打印24而不是25?如果n=4; n=6; n=3; n=2;代码有效,但有五个代码没有.
运行以下代码行时:
int i,a;
for(i=0;i<=4;i++)
{
a=pow(10,i);
printf("%d\t",a);
}
Run Code Online (Sandbox Code Playgroud)
我很惊讶地看到输出,它出来了 1 10 99 1000 9999而不是1 10 100 1000 10000.
可能的原因是什么?
注意
如果您认为它是浮点不准确,在上面的for循环中i = 2,存储在变量中的值a是99.
但如果你写的话
a=pow(10,2);
Run Code Online (Sandbox Code Playgroud)
现在a的价值出现了100.怎么可能?
我是C编程的新手,我编写了这段代码,因为我被要求主要使用printf()和scanf()来做一些事情.我知道可以有更好的方法来解决这个问题,我很快就需要学习,但无论如何,现在这就是我所拥有的:
int add1, add2, exponent, exponent_result, multiplier, parenthese, product, sub, total;
printf("Let's try a slightly more complex calculation, in which we'll use an exponent.\n\n");
printf("Type 5 whole numbers or integers.\n\n");
scanf("%i %i %i %i %i", &add1, &add2, &exponent, &multiplier, &sub);
printf("Out of the numbers you typed, we're going to make this operation: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
exponent_result = pow(add2, exponent);
parenthese = add1 + exponent_result;
product = parenthese * multiplier;
total = …Run Code Online (Sandbox Code Playgroud) 假定n和m是正整数,且n 米是一个整数的范围内,将(int)pow(n,m)永远给一个错误的答案?
我已经尝试了许多n的m=2,并没有得到任何错误的答案为止.
在做我的作业时,我注意到一些非常奇怪的东西,我无法弄清楚为什么.
int x = 5;
cout << pow(x, 2);
Run Code Online (Sandbox Code Playgroud)
结果是25.那没关系.但如果我写这样的程序:
int x = 5;
int y = pow(x, 2);
cout << y;
Run Code Online (Sandbox Code Playgroud)
结果是24!
当x为2,3,4,6,7,8没有问题,但是5,10,11,13等结果比它应该低1.
if()也是一样的.
for (int x = 1; x <= 20 ; x++) {
if (x * x == pow(x, 2))
cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
它打印出数字1,2,3,4,6,8,12,16.
以下C代码
int main(){
int n=10;
int t1=pow(10,2);
int t2=pow(n,2);
int t3=2*pow(n,2);
printf("%d\n",t1);
printf("%d\n",t2);
printf("%d\n",t3);
return (0);
}
Run Code Online (Sandbox Code Playgroud)
给出以下输出
100
99
199
Run Code Online (Sandbox Code Playgroud)
我正在使用devcpp编译器.它没有任何意义,对吧?有任何想法吗?(那个pow(10,2)可能就像99.9999那样不能解释第一个输出.而且,即使我包含math.h,我也得到了相同的输出.)
pow() 函数给出非常奇怪的输出。
我尝试了各种组合:
#include<stdio.h>
#include<math.h>
int main()
{
int d=1;
long long n1,n2;
while(d<10)
{
n1=pow(10,d);
n2=pow(10,d);
d++;
printf("%lld %lld\n",n1,n2);
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误的输出,即 99 而不是 100 之类的。
现在,如果我删除其中一个变量,ans。是正确的。如果我使用常量而不是 d,则 ans 是正确的。如果我将 n1 和 n2 作为双精度,则 ans 是正确的。
因此,拥有两个 pow() 函数,并且将变量作为幂并将类型转换为整数会产生错误的输出。为什么有这种奇怪的行为?
我有以下c程序,有些时候我运行它,输出不同,基于编译器和平台.我知道double to int转换可能会导致问题.
这是代码:
//Compiler version gcc 6.3.0
#include <stdio.h>
#include <math.h>
int main(void){
double d = 2;
printf("%.20lf\n", pow(10, d));
printf("%d\n", (int)pow(10, d));
printf("%d\n", (int)pow(10, 2));
}
Run Code Online (Sandbox Code Playgroud)
100是预期值,但声明
printf("%d\n", (int)pow(10, d));
Run Code Online (Sandbox Code Playgroud)
当我同时使用gcc 6.3.0和Windows 10 x64时,输出为99,但在其他情况下则不然.
以下是一些结果:
//gcc 6.3.0 (Sublime Text 3) in Windows 10 x64
100.00000000000000000000
99 ->this is the problem
100
//gcc 6.3.0 in Android (using Dcoder app)
100.00000000000000000000
100
100
//MSVC(VS 2017 x86) in Windows 10 x64
100.00000000000000000000
100
100
Run Code Online (Sandbox Code Playgroud)
我还测试了一些在线gcc(6.3.0)编译器,但所有输出都是100.
谢谢您的帮助.