相关疑难解决方法(0)

pow()是否适用于C中的int数据类型?

我只是编写一个程序来计算整数的幂.但产量不如预期.除了5之外,它适用于所有整数.

我的代码是:

#include <stdio.h>
#include <math.h>

int main(void)
{
  int a,b;
  printf("Enter the number.");
  scanf("\n%d",&a);
  b=pow(a,2);
  printf("\n%d",b);
}
Run Code Online (Sandbox Code Playgroud)

输出是这样的:

"Enter the number. 2
 4
"Enter the number. 5
 24
"Enter the number. 4
 16
"Enter the number. 10
 99
Run Code Online (Sandbox Code Playgroud)

我们不能使用pow()int数据类型的函数??

c exponentiation pow

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

为什么在C++中pow(10,5)= 9,999

最近我写了一段代码:

const int sections = 10;

for(int t= 0; t < 5; t++){
   int i = pow(sections, 5- t -1);  
   cout << i << endl;
}
Run Code Online (Sandbox Code Playgroud)

结果是错误的:

9999
1000
99
10
1
Run Code Online (Sandbox Code Playgroud)

如果我只使用这个代码:

for(int t = 0; t < 5; t++){
    cout << pow(sections,5-t-1) << endl; 
}
Run Code Online (Sandbox Code Playgroud)

问题不再发生:

10000
1000
100
10
1
Run Code Online (Sandbox Code Playgroud)

有没有人给我解释?非常感谢你!

c++ mingw pow

11
推荐指数
2
解决办法
8344
查看次数

pow功能在这里发生了什么?

我在这里看到了各种各样的答案,描述了powC 中函数的奇怪行为.
但我在这里有一些不同的问题.

在下面的代码我初始化int x = pow(10,2)int y = pow(10,n) (int n = 2).

在第一种情况下,当我打印它显示的结果时100 ,在另一种情况下它出来了99.

我知道pow返回double它会在存储时被截断int,但我想问为什么输出会有所不同.

CODE1

#include<stdio.h>
#include<math.h>
int main()
{
     int n = 2;
     int x;
     int y;
     x = pow(10,2);   //Printing Gives Output 100   
     y = pow(10,n);   //Printing Gives Output 99


     printf("%d %d" , x , y);

}
Run Code Online (Sandbox Code Playgroud)

Output : 100 99

为什么输出结果不同.?

我的gcc版本是4.9.2

更新:

代码2

int main()
    {
         int …
Run Code Online (Sandbox Code Playgroud)

c gcc compiler-optimization pow gcc4.9

10
推荐指数
2
解决办法
1397
查看次数

如果赋值为整数,则pow()的返回值会向下舍入

pow在C中使用函数并将返回值存储为整数类型.请参阅下面的代码段:

for (i = 0; i < 5; i++){
    val = (int)pow(5, i);
    printf("%d, ", val);
}
Run Code Online (Sandbox Code Playgroud)

这里i,val是整数,输出是1, 5, 24, 124, 624.我相信这是因为浮点数25被视为24.99999 ...在分配给整数时会向下舍入到24.

如果我仍然需要将返回值存储在int中,我怎么能通过这个?

c pow

6
推荐指数
2
解决办法
6896
查看次数

pow() 在特定条件下返回错误结果(以及意外修复) - 为什么会这样?

我一直在创建一个用于实施 Eratosthenes 筛分的简短程序。在他的程序中,我使用 int startingPointwhich 保存了当前素数的值,它的乘法被标记为非素数,从它的平方开始。我使用 pow() 函数计算当前循环中起点的平方。这样做的时候,我遇到了一个奇怪的现象。当startingPoint等于 时5,则后int i = pow(startingPoint,2)的值为i24,而不是 25。这种现象仅在startingPoint等于 5时发生。之后,我运行了一些测试,导致以下代码片段:

#include <iostream>
#include <math.h>

int main()
{
    int p=5;

    int num1 = pow(5,2);
    int num2 = pow(p,2);
    float num3 = pow(p,2);
    int num4 = floor(pow(p,2));

    std::cout.precision(10);

    std::cout<<num1<<std::endl; //25
    std::cout<<num2<<std::endl; //24
    std::cout<<std::fixed<<num3<<std::endl; //25.0000000000
    std::cout<<num4<<std::endl; //25            
}
Run Code Online (Sandbox Code Playgroud)

可以看出,如果我powint文字调用,结果将是数字的实际平方。但是,如果我使用 using 调用它int p=5,那么pow返回的结果实际上比预期的结果低一个。如果我将结果传递给一个float变量,它也会收到正确的结果。

现在,我知道pow计算幂的方法是通过近似计算,因此,在转换为整数时可能会出现诸如此类的错误。我可能就让它这样吧。但真正让我停下来思考的是num4. pow(p,2) …

c++ gcc pow

6
推荐指数
0
解决办法
1942
查看次数

对于某些正整数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
查看次数

这种计算大数的方式如何工作?

通常,要处理超出C++中long long范围的整数,您必须将它们表示为字符串并以这种方式对它们执行操作.但是,我在互联网上发现这个代码似乎像魔术一样.它计算两个幂的总和(没有2 ^ 0),即使它不能存储在很长的长度中.

#include <iostream>  
#include <cmath>  
#include <iomanip>  
#include <sstream>  
using namespace std;

int main() {
    int n;
    stringstream ss;
    cin >> n;

    ss << fixed << setprecision(0) << pow(2, n + 1) - 2;

    if (n >= 54) {
        string a = ss.str();

        a[a.size() - 1] = ((a[a.size() - 1] - 48) - 2) + 48;

        cout << a;
        return 0;
    }

    cout << ss.str();

}
Run Code Online (Sandbox Code Playgroud)

它是如何工作的?它适用于涉及大量的任何操作吗?如果n的值非常大(我试过1024)它只打印"inf".可以这种方式计算的数字范围的上限是多少?

以下部分到底做了什么以及它为什么这样做?

a[a.size() - 1] = ((a[a.size() - 1] - 48) …
Run Code Online (Sandbox Code Playgroud)

c++ largenumber stringstream

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

C++中2个64位数字的乘法

我实现了 karatsuba 乘法算法。我想以这种方式改进它,我可以将 2 个 64 位数字相乘,但我不知道该怎么做。我得到了一个提示,两个数字都包含一个 2 的幂的位数,但这对我没有任何暗示。你能给出任何其他提示吗?数学提示或算法改进提示。

#include <iostream>
#include <math.h>
using namespace std;

int getLength(long long value);
long long multiply(long long x, long long y);

int getLength(long long value)
{
    int counter = 0;
    while (value != 0)
    {
        counter++;
        value /= 10;
    }
    return counter;
}

long long multiply(long long x, long long y)
{
    int xLength = getLength(x);
    int yLength = getLength(y);

    // the bigger of the two lengths
    int N = (int)(fmax(xLength, yLength));

    // …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm math karatsuba

2
推荐指数
1
解决办法
5938
查看次数

pow功能出错

#include <math.h>
#include <stdio.h>

void main() {
    int decimal, count, binary, digit;
    printf("Enter the number : ");
    scanf("%d", &decimal);
    count = 0; binary = 0;
    while (decimal > 0) {
        digit = decimal % 2;
        binary = binary + digit * pow(10, count);
        decimal = decimal / 2;
        ++count;
    }
    printf("Binary form : %d", binary);
}
Run Code Online (Sandbox Code Playgroud)

我使用上面的代码将Decimal转换为二进制.但问题是输出.

Input           : 12
Expected Output : 1100
Actual Output   : 1099
Run Code Online (Sandbox Code Playgroud)

[img] https://i.imgur.com/1mZlMQN.png [/ img ]

其他输入也存在此问题.只有8给出正确的输出.

有人可以解释为什么会这样吗?当我将它移植到那里时,这个错误也出现在C++中.

PS:pow在检查数字是否是阿姆斯特朗以及是否为回文时,也会弹出此错误.

c

0
推荐指数
1
解决办法
344
查看次数

为什么pow(10,2)99而不是100的结果?

我正在用c制作一个新项目,功能"不起作用".我该怎么做才能解决?

它的功能实际为2到10的幂,但10的功率为1

#include <stdio.h>
#include <math.h>

int main(){
    int res=2;
    int base=10;
    int exp=2;
    res= pow(base,exp);
    printf("%d",res);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望输出为100,但输出为99

c

-2
推荐指数
1
解决办法
245
查看次数