[英]Same numerical code returns different output whether it is in C++ or C
给我一个示例C ++数值代码,它以三种不同的方式计算导数。 我必须将其转换为C。我认为由于原来使用cmath不会给我带来任何问题,但是我错了。 这是原始代码:
#include <iostream>
#include<string>
#include <cmath>
#include <fstream>
using namespace std;
double metoda_pochodna_1(int x, double h)
{
return (sin(x+h) - sin(x)) / h;
}
double metoda_pochodna_2(int x, double h)
{
return (sin(x+(0.5*h)) - sin(x-(0.5*h))) / h;
}
double metoda_pochodna_3(int x, double h)
{
return ((sin(x-2*h) - 8*sin(x-h) + 8*sin(x+h) - sin(x+2*h)) / (12*h));
}
int main()
{
double h, w1, w2, w3, kos = cos(1.0);
int x=1;
ofstream wyniki;
wyniki.open("wyniki.dat");
for (h = pow(10.0, -15.0); h < 1; h *= 1.01)
{
w1 = log10(abs(metoda_pochodna_1(x,h) - kos));
w2 = log10(abs(metoda_pochodna_2(x,h) - kos));
w3 = log10(abs(metoda_pochodna_3(x,h) - kos));
wyniki << log10(h)<<" "<< w1 <<" "<< w2 <<" "<< w3 << "\n";
cout << log10(h)<<" "<< w1 <<" "<< w2 <<" "<< w3 << "\n";
}
wyniki.close();
cout << endl;
/*system("pause");*/ //uruchamiane z windowsa
return 0;
}
这是我的C版本; 我只是更改了文件处理。 注意:我在故障排除期间更改为long double
,但是结果输出与使用常规double
的版本完全相同。
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
long double metoda_pochodna_1(int x,long double h)
{
return (sin(x+h) - sin(x)) / h;
}
long double metoda_pochodna_2(int x,long double h)
{
return (sin(x+(0.5*h)) - sin(x-(0.5*h))) / h;
}
long double metoda_pochodna_3(int x, long double h)
{
return ((sin(x-2*h) - 8*sin(x-h) + 8*sin(x+h) - sin(x+2*h)) / (12*h));
}
int main()
{
long double h, w1, w2, w3, kos = cos(1.0);
int x=1;
FILE * file;
file = fopen("wyniki.dat","w+");
for (h = pow(10.0, -15.0); h < 1; h *= 1.01)
{
w1 = log10(abs(metoda_pochodna_1(x,h) - kos));
w2 = log10(abs(metoda_pochodna_2(x,h) - kos));
w3 = log10(abs(metoda_pochodna_3(x,h) - kos));
fprintf(file,"%f %Lf %Lf %Lf\n",log10(h), w1,w2,w3);
printf("%f %Lf %Lf %Lf\n",log10(h), w1,w2,w3);
//wyniki << log10(h)<<" "<< w1 <<" "<< w2 <<" "<< w3 << "\n";
//cout << log10(h)<<" "<< w1 <<" "<< w2 <<" "<< w3 << "\n";
}
fclose(file);
printf("\n");
return 0;
}
这是C ++代码的输出,我假设它是正确的:
-15 -1.82947 -1.82947 -1.28553 -14.9957 -2.03091 -2.03091 -1.33768 14.9914 -2.41214 -2.41214 -1.39632 -14.987 -2.81915 -2.81915 -1.46341
[...]
这是C版本的输出,显然是不同的(前一行的附加精度来自long double
,但其他三列均为-inf
,无论使用double
还是long double
):
-15.000000 -inf -inf -inf -14.995679 -inf -inf -inf -14.991357 -inf -inf -inf -14.987036 -inf -inf -inf
[...]
转换后的代码有什么问题?
您正在使用abs
,它是一个整数函数。 C浮点函数是fabs
。 将三行更改为
w1 = log10(fabs(metoda_pochodna_1(x,h) - kos));
w2 = log10(fabs(metoda_pochodna_2(x,h) - kos));
w3 = log10(fabs(metoda_pochodna_3(x,h) - kos));
程序输出更合理的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.