2017-01-07 9 views
0

3つの異なる方法で派生を計算するC++数値コードの例が与えられました。私はそれをC言語に変換しなければなりません。とにかくcmathを使ってオリジナルの問題がないと思っていましたが、間違っていました。元のコードは次のとおりです。同じ数値コードは、C++であろうとCであろうと異なる出力を返す

#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ですが、他の3つの列はすべて-infです)。doubleまたはlong double)が使用されます。

-15.000000 -inf -inf -inf 

-14.995679 -inf -inf -inf 

-14.991357 -inf -inf -inf 

-14.987036 -inf -inf -inf 

[...]私の変換されたコードが間違っている何

+2

'-inf'で表示された値が'%Lf'書式指定子の出力であることに気付きました。あなたは既知の値(計算されていない)の簡単なテスト出力を試みましたか? –

+1

だから、あなたはどうして**異なる言語のCとC++が同じ振る舞いをするべきだと思いますか?そして、[mcve]が何を意味するのか見てください! – Olaf

+4

Cで 'abs'は*整数*関数です。 'fabs'を使ってください。 –

答えて

3

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)); 

プログラムの出力より賢明な値にあなたの3行を変更した後。

+0

それはそれを解決しました!値は現在チェックされています。どうもありがとうございました。 –

関連する問題