在 Visual Studio 2005 的 math.h 中找不到错误函数 erf(x) [英] Error function, erf(x), not found in math.h for visual studio 2005

查看:38
本文介绍了在 Visual Studio 2005 的 math.h 中找不到错误函数 erf(x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 cmathVisual Studio 2005 没有 erf(x).我正在将 NIST 统计测试套件用于随机和伪随机数生成器.在 cephes.c 的方法 double cephes_normal(double x) 中,它使用了一个 C99 数学函数 erf(x),我认为 Visual Studio 2005 不支持该函数.

It seems that cmath for visual studio 2005 does not have erf(x). I am using NIST Statistical Test Suite for Random and Pseudorandom Number Generators. In cephes.c's method, double cephes_normal(double x), it uses a C99 math function erf(x) which I don't believe is supported by visual studio 2005.

我怎样才能克服这个问题?我在这里看到了一个 C++ 解决方案:http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/9f5f4bf4-c0ae-4620-8039-4dc36e98d718/

How can I overcome this problem? I saw a C++ solution here: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/9f5f4bf4-c0ae-4620-8039-4dc36e98d718/

有人使用了 boost C++ 数学库.但我认为我不能在 C 源文件中包含 C++ 头文件.

Someone used the boost C++ math library. But I don't think I can include a c++ header into a C source file.

推荐答案

一些谷歌搜索发现了这个 C++ 实现(转贴在这里):

Some Googling found this C++ implementation (reposted here):

#include <cmath>

double erf(double x)
{
    // constants
    double a1 =  0.254829592;
    double a2 = -0.284496736;
    double a3 =  1.421413741;
    double a4 = -1.453152027;
    double a5 =  1.061405429;
    double p  =  0.3275911;

    // Save the sign of x
    int sign = 1;
    if (x < 0)
        sign = -1;
    x = fabs(x);

    // A&S formula 7.1.26
    double t = 1.0/(1.0 + p*x);
    double y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);

    return sign*y;
}

测试功能

void testErf()
{
    // Select a few input values
    double x[] = 
    {
        -3, 
        -1, 
        0.0, 
        0.5, 
        2.1 
    };

    // Output computed by Mathematica
    // y = Erf[x]
    double y[] = 
    { 
        -0.999977909503, 
        -0.842700792950, 
        0.0, 
        0.520499877813, 
        0.997020533344 
    };

    int numTests = sizeof(x)/sizeof(double);

    double maxError = 0.0;
    for (int i = 0; i < numTests; ++i)
    {
        double error = fabs(y[i] - erf(x[i]));
        if (error > maxError)
            maxError = error;
    }

    std::cout << "Maximum error: " << maxError << "\n";
}   

这篇关于在 Visual Studio 2005 的 math.h 中找不到错误函数 erf(x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆