954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

why atof is giving different result.

#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

Rajdarji
Newbie Poster
3 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

Rajdarji
Newbie Poster
3 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Is there anything do with the gcc compiler?

Rajdarji
Newbie Poster
3 posts since Oct 2010
Reputation Points: 10
Solved Threads: 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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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, whileatof 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.

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You