c - c程序中的隐藏错误(数值计算)

标签 c gcc numerical-methods

这里有两个函数。我认为它们之间没有重要区别,但结果不同。为什么?

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

long double GiveCx(long double x)
{  
double a = 5.3226927610784935E-01;
double b = 6.5410208763684241E-01;
double c = -1.4312869957125389E+00;
double d = 8.4710834303177074E-01;
return (c*atanl(expl(x-a)/b) + d);
}

double BurkardtCollectionBased_sech_cdf_Offset_model(double x_in)
{
 double temp;
 temp = 0.0;
 // coefficients
 double a = 5.3226927610784935E-01;
 double b = 6.5410208763684241E-01;
 double c = -1.4312869957125389E+00;
 double Offset = 8.4710834303177074E-01;
 temp = c * atan(exp((x_in-a)/b));
 temp += Offset;
 return temp;
}

int main()
{
 int ix;
 for (ix=0; ix<5; ix++)
  printf(" ix = %d ; c = %.20f  ;  %.20Lf \n", ix,      BurkardtCollectionBased_sech_cdf_Offset_model( (long double)ix), GiveCx((long double )ix));
return 0;
}

结果是:

ix = 0 ; c = 0.25000064588764425721 ; -0.20004050665796930359

ix = 1 ; c = -0.75000921765452766010 ; -0.84455584419096496618

ix = 2 ; c = -1.24993551212417064455 ; -1.18701705113792041978

ix = 3 ; c = -1.36825264215735509232 ; -1.32186430467910977205

ix = 4 ; c = -1.39401846938445195256 ; -1.37195787094497580628

最佳答案

你有一个 expl(x-a)/b 和另一个 exp((x_in-a)/b) 。检查 b 是否需要像后一种情况那样除以指数,或者像前一种情况那样的结果。

您可能需要在 GiveCx() 中使用 expl((x-a)/b)

关于c - c程序中的隐藏错误(数值计算),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21711419/

相关文章:

fortran - Fortran 中的二维边界条件

c - 访问pcap文件头

c - 带有 -Ofast 的 -DNDEBUG 比只有 -Ofast 慢

c - 使用完整缓冲区的生产者-消费者算法

gcc - Cygwin 中 .seh_savexmm 的无效寄存器

c - 局部变量未在内联函数中对齐

algorithm - 雅可比方法先收敛再发散

c - 使用 Lapack&co 求解病态线性方程组

c - 在 OpenSUSE 13.2 上使用 memcpy 解密时出现乱码

c - 受类型转换影响的变量