相关疑难解决方法(0)

实现基于整数的幂函数pow(int,int)的最有效方法

将整数提升到C中另一个整数的幂的最有效方法是什么?

// 2^3
pow(2,3) == 8

// 5^5
pow(5,5) == 3125
Run Code Online (Sandbox Code Playgroud)

c algorithm math exponentiation

239
推荐指数
8
解决办法
19万
查看次数

使用我的编译器和操作系统时,为什么当n = 5时pow(n,2)返回24?

#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;代码有效,但有五个代码没有.

c math.h c-standard-library

23
推荐指数
1
解决办法
4809
查看次数

pow功能的奇怪行为

运行以下代码行时:

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,存储在变量中的值a99.

但如果你写的话

a=pow(10,2);
Run Code Online (Sandbox Code Playgroud)

现在a的价值出现了100.怎么可能?

c pow

13
推荐指数
3
解决办法
4123
查看次数

为什么pow()从我的结果中减去1?

我是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)

c math codeblocks

7
推荐指数
1
解决办法
169
查看次数

对于某些正整数n,m,will(int)pow(n,m)是错误的吗?

假定nm是正整数,且n 是一个整数的范围内,将(int)pow(n,m)永远给一个错误的答案?

我已经尝试了许多nm=2,并没有得到任何错误的答案为止.

c floating-accuracy

5
推荐指数
1
解决办法
329
查看次数

奇怪的战俘(x,y); 行为

在做我的作业时,我注意到一些非常奇怪的东西,我无法弄清楚为什么.

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++ pow

4
推荐指数
3
解决办法
4652
查看次数

来自pow的异常输出

以下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,我也得到了相同的输出.)

c pow

4
推荐指数
1
解决办法
459
查看次数

幂函数输出错误 - C

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 c++ pow

4
推荐指数
1
解决办法
6965
查看次数

C:gcc 6.3.0中的double to int转换错误

我有以下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.

谢谢您的帮助.

c gcc output

3
推荐指数
1
解决办法
115
查看次数