2017-09-02 3 views
2

私は既に私に与えられたファイルから自分のデータを印刷しようとしています。私たちのデータでは、ほとんどの数字は小数点以下9桁です。私の先生が私にスターターコードを与えました。データを印刷しようとすると、小数点以下6桁までしか印刷されません。したがって、最後の3桁は出力に表示されません。また、私は%.9fのような小数点以下の桁数を書こうとしましたが、意外にも最後の3桁が異なります。たとえば、私のデータの数字は1.900195512ですが、印刷された数字(小数点以下9桁に設定)は1.900195479です。Cの小数点をすべて表示

私のコードは次のとおりです。

#define _CRT_SECURE_NO_DEPRECATE 
#include <stdio.h> 
#include <stdlib.h> 
// Declare constants 

// Name of file that stores our raw data 
#define FILE_NAME "data_1.csv" 

// Data size 
#define MAX_ROWS 20 
#define MAX_COLUMNS 20 

// Main entry point for the program 
int main(void) { 
    // Decalred variables 
    int rowIndex = 0; 
    int columnIndex = 0; 
    float rawData[MAX_ROWS][MAX_COLUMNS]; // 2-dimensional array to store our 
    raw data 
    // Misc variables used for reading the data from the file 
    float tempfloat = 0.0F; 
    float tmp = 0.0F; 
    char newline = ' '; 

    // ---------------------------------------------------------------------- 
    // Open the file for reading 
    FILE *infp; 
    infp = fopen(FILE_NAME, "r"); 

    // Check for errors and exit if found 
    if (infp == NULL) { 
     printf("Error: failed to open %s for reading\n", FILE_NAME); 
     return(1); 
    } 

    // Read the file into the data structure 
    for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++) { 
     // Read up until the last value 
     for (columnIndex = 0; columnIndex < MAX_COLUMNS - 1; columnIndex++) { 
      if (fscanf_s(infp, "%f,", &tempfloat) != EOF) { 
       rawData[rowIndex][columnIndex] = tempfloat; 
      } else { 
       printf("Error: incorrect file format at row %d, col %d.\n", rowIndex + 1, columnIndex + 1); 
       return(1); 
      } 
     } 

     // Read the last value and the newline char 
     if (fscanf_s(infp, "%f%c", &tempfloat, &newline) != EOF) { 
      // Check if the last character in the line was a \n otherwise an error occured. 
      //Xiao: I have added newline != '\n' 
      if (newline != '\0' && newline != '\r' && newline != '\n') { 
       printf("Error: incorrect file format at line %d. did not find a newline.\n", rowIndex + 1); 
       return(1); 
      } else { 
       rawData[rowIndex][columnIndex] = tempfloat; 
      } 

      // Reset the character before the next read. 
      newline = ' '; 
     } 
    } 
    // ---------------------------------------------------------------------- 
    // Print out the rawdata array 
    printf(" --- RAW DATA ---\n"); 
    for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++) { 
     // Read up until the last value 
     for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++) { 
      printf("%.9f ", rawData[rowIndex][columnIndex]); 
     } 
     printf("\n"); 
    } 

    // Exit 
    return (0); 
} 
+2

読むhttp://floating-point-gui.de/ –

+1

浮動小数点精度を使用します。 – Amit

+1

データファイルなしで実行可能な最小限の例にコードを縮小すると、これはより良い質問になります。おそらく、 'scanf'と' printf'の呼び出し以上にはなりません。 –

答えて

2

floatタイプは9正確な小数点以下の桁のための十分な精度を持っていません。 でスキャンできるdoubleタイプを使用する必要があります。

fscanf()EOFの戻り値を比較して失敗を検出しないと、無効な入力によって0が返されることがあります。成功したコンバージョン数を確認するには、fscanf()をご利用ください。ここで

が変更されたバージョンである:

#define _CRT_SECURE_NO_DEPRECATE 
#include <stdio.h> 
#include <stdlib.h> 
// Declare constants 

// Name of file that stores our raw data 
#define FILE_NAME "data_1.csv" 

// Data size 
#define MAX_ROWS 20 
#define MAX_COLUMNS 20 

// Main entry point for the program 
int main(void) { 
    // Decalred variables 
    int rowIndex = 0; 
    int columnIndex = 0; 
    double rawData[MAX_ROWS][MAX_COLUMNS]; // 2-dimensional array to store our 
    raw data 
    // Misc variables used for reading the data from the file 
    double tempfloat = 0.0; 
    double tmp = 0.0; 
    char newline; 

    // ---------------------------------------------------------------------- 
    // Open the file for reading 
    FILE *infp; 
    infp = fopen(FILE_NAME, "r"); 

    // Check for errors and exit if found 
    if (infp == NULL) { 
     printf("Error: failed to open %s for reading\n", FILE_NAME); 
     return(1); 
    } 

    // Read the file into the data structure 
    for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++) { 
     // Read up until the last value 
     for (columnIndex = 0; columnIndex < MAX_COLUMNS - 1; columnIndex++) { 
      if (fscanf_s(infp, "%lf,", &tempfloat) == 1) { 
       rawData[rowIndex][columnIndex] = tempfloat; 
      } else { 
       printf("Error: incorrect file format at row %d, col %d.\n", rowIndex + 1, columnIndex + 1); 
       return 1; 
      } 
     } 

     // Read the last value and the newline char 
     if (fscanf_s(infp, "%lf%c", &tempfloat, &newline) == 2) { 
      // Check if the last character in the line was a \n otherwise an error occured. 
      //Xiao: I have added newline != '\n' 
      if (newline != '\r' && newline != '\n') { 
       printf("Error: incorrect file format at line %d. did not find a newline.\n", rowIndex + 1); 
       return 1; 
      } else { 
       rawData[rowIndex][columnIndex] = tempfloat; 
      } 
     } 
    } 
    // ---------------------------------------------------------------------- 
    // Print out the rawdata array 
    printf(" --- RAW DATA ---\n"); 
    for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++) { 
     // Read up until the last value 
     for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++) { 
      printf("%.9f ", rawData[rowIndex][columnIndex]); 
     } 
     printf("\n"); 
    } 

    // Exit 
    return 0; 
} 
1

あなたは問題を特定するためにはるかに簡潔な例にあなたの問題を打破することができます。 floatから9桁の精度を取得しようとしていますが、これは6-7にしか適していません。正確さを向上させるには、タイプdoubleを代わりに使用する必要があります。

ユーザー入力を常に読むときは、常にが使用された関数の戻り値と値の制約を検証します(あなたには何もありません)。問題を修正

短い実装は次のようになります。

#include <stdio.h> 

int main (void) { 

    double d = 0.0; 

    printf ("Enter value: "); 
    if (scanf ("%lf", &d) == 1) 
     printf ("Your value : %.9f\n", d); 

    return 0; 
} 

使用例/出力

$ ./bin/scanf_double 
Enter value: 1.900195512 
Your value : 1.900195512 

回答やコメントに目を通すと、さらにご質問がある場合は私に知らせてください。

1

floatは7か所以降は正確ではありません。 double代わり

#include <stdio.h> 

int main() 
{ 
    float f; 
    double d; 
    scanf("%f",&f); 
    scanf("%lf",&d); 
    printf("%.9f\n",f); 
    printf("%.9lf\n",d); 
    return 0; 
} 


Input: 
0.123456789  // float variable i.e. f 
0.123456789  // double variable i.e. d 

Output: 
0.123456791  // float precision 
0.123456789  // double precision 
関連する問題