0
#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

Edited by WaltP: Added CODE Tags -- Learn to use them

3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by invisal
0

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

0

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.

0

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.

-1

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.

Votes + Comments
Thank you for repeating what I posted.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.