c++ - 来自 math.h 的 pow 错误

标签 c++ c pow math.h

我被 C 语言中的这个错误困住了,无法弄清楚出了什么问题。以下查询构成了我的代码的一个组成部分,用于在连续迭代中以指数方式增加值。这是我编写的公式:

我已经注释了代码中的错误。

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

int main()
{
    double maxMeshS = 0;
    double minMeshS = 0;

    cout << "enter max mesh size (m): ";
    cin >>maxMeshS;

    cout <<"\nenter min mesh size (m): ";
    cin >> minMeshS;

    double raise = maxMeshS/minMeshS; //Values used for run maxMeshS = .5
    double factor = 1/9;              //                    minMeshS = .005    

    double b = pow(raise,factor);
    cout << "b " << b << endl;  // The error happens here when I use above values
                               // b comes out to be 1 which should be 1.668100 instead
    double a = minMeshS/b;
    cout << "a " << a << endl;

    int x;
    cout << "enter x " ;
    cin >> x;

    double queryCube = a*pow(b,x);
    cout << queryCube << endl;
    return(0);
}

然而,当我为除法 maxMeshS/minMeshS 使用计算值时,即 100 和 1/9,即 .11111,我获得了 b = pow(100, .1111) = 的正确值1.66800。为什么会这样?

最佳答案

你的因子是 (int)1/(int)9(int)0; 几乎所有的零次方都是一 你可以做这样的事情

double factor = 1/9.0; // make sure the 9 is a floating point

关于c++ - 来自 math.h 的 pow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18446959/

相关文章:

c++ * vs & 在函数声明中

c++ - 如何注册 Windows 服务但避免它被列在服务控制台中?

c 段错误 fgets

java - 为什么这段代码使用 Math.pow 打印 "HELLO WORLD"?

c - 高效 10 次幂

c++ - LSH 比 BruteForce 匹配慢

c++ - 如何找到点云中物体的质心?

c++ - 如何获取 GDI 句柄列表

c - 使 valgrind 在未初始化的值上快速失败

java - Math.pow 在重复调用时产生不同的结果