2017-09-02 57 views
2

因此,我试图从已经提供给我的文件中打印我的数据。在我们的数据中,大多数数字都有9位小数。我的老师给了我一个初学者代码,当我尝试打印数据时,它只打印了6位小数。所以最后3位数字不会显示在输出中。此外,我试图写出像%.9f这样的小数位数,但令人惊讶的是最后三位数字不同。例如,我的数据中的数字是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类型,您可以使用%lf进行扫描。

也不要比较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 
相关问题