#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <stdlib.h>

static char *
string_tok(
    char *str)
 {
    static char *next   = NULL;

    if(str == NULL) {
        str = next;
    }
    else {
        next = str;
    }

    if(str == NULL) {
        return NULL;
    }

    while(next && next[0] != '\n' && next[0] != '\r') {
        if(*next == ',') {
            *next = '\0';
            next++;
            return str;
        }
        next++;
    }

    if(*next == '\r' || *next == '\n') {
        *next = '\0';
    }

    next = NULL;

    return str;
}

void main()
{
    FILE *filep = NULL;

    char line[100] = {0};
    char *tmp_str = NULL;
    char *str_tok = NULL;
    char *fieldsp[10];

    int  nfield = 0;

    filep = fopen( (char *)"raj.in", "r" );
    if(NULL != filep){
        while(fgets(line, 1024, filep)){

            for ( tmp_str = line; ( str_tok = string_tok( tmp_str ) ) != NULL; tmp_str = NULL){
                fieldsp[nfield++] = str_tok;
            }
            double rec_amt = atof(fieldsp[4]);
            printf("%.21f\n", rec_amt);
        }
    }
    else {
        printf("error");
    }
    return;
}

Input file content
0001,000000000000000001-11227286431,9000000000000000001-11227286431000000044795,10/26/2010,0000024.18
0002,000000000000000001-16809572197,9000000000000000001-16809572197000000044793,10/26/2010,0000164.24

output:
24.179999999999999715783
164.240000000000009094947

My question here is in the file record amount shown is "0000024.18" but when I do atof it is returning 24.179999999999999715783. Does any one has an idea why this is happening? Do we have any alternative other then rounding?

Your response is highly appreciated.

Thanks,
--Raj

Recommended Answers

All 5 Replies

Because floating point numbers are approximations. Digital values cannot represent real numbers exactly. Search the web for explanations of how they work.

Thanks for the reply.

But once you converting string which has fixed value. Then how it can be treat as different value? I know it is not different value but i want to understand why exactly it is happening like this.

Is there anything do with the gcc compiler?

Thanks for the reply.

But once you converting string which has fixed value. Then how it can be treat as different value? I know it is not different value but i want to understand why exactly it is happening like this.

Did you try the search?

Is there anything do with the gcc compiler?

No.

But once you converting string which has fixed value. Then how it can be treat as different value?

The string you have is represented in decimal float-point, while atof function converting that string into binary float-point. Some fixed values in decimal float-point cannot be exactly converted into binary float-point. In this case, atof function will choose the closest approximate value to the decimal float-point. Dealing with float-point can be frustrated, you should better read how it works.

commented: Thank you for repeating what I posted. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.