显示C中的所有十进制数

时间:2017-09-02 06:14:30

标签: c

所以我试图从已经提供给我的文件中打印我的数据。在我们的数据中,大多数数字都有9位小数。我的老师给了我一个入门代码,当我尝试打印数据时,它只打印最多6位小数。因此最后3位数字不会显示在输出中。此外,我试图写出像%.9f这样的小数位数,但令人惊讶的是最后三位数字是不同的。例如,我的数据中的数字为1.900195512,而打印的数字(设置为小数点后9位)为1.900195479

我的代码是:

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

3 个答案:

答案 0 :(得分:2)

wg.Done()类型对9个精确小数位的精度不够。您应该使用float类型,您可以使用double扫描。

另外,请不要将%lf的返回值与fscanf()进行比较以检测失败,无效输入可能会导致EOF返回。只需验证0返回的成功转化次数。

以下是修改后的版本:

fscanf()

答案 1 :(得分: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

查看答案和评论,如果您有其他问题,请告诉我。

答案 2 :(得分:1)

7个地方后,

float不准确。请改用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