[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scanf of floats and then prints is not same
From: |
Mudit Wahal |
Subject: |
scanf of floats and then prints is not same |
Date: |
Mon, 14 May 2001 15:58:57 -0700 (PDT) |
Hi,
I'm running Linux 2.2.19.
>gcc --version
egcs-2.91.66
>ld --version
GNU ld 2.9.5
Copyright 1997 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License. This program has absolutely no warranty.
Supported emulations:
elf_i386
i386linux
My problem is that when i use scanf() to scan a float and then print it, I get
incorrect values.
When I scan the following line using "%f" for all the fields except 1st one
-------------- input --------------------
20010511 45.92 45.92 45.81 1 45.81 9.52
-------------------------------------------
then i printf using "%f" all the values, I get the following output
-------------------- output ------------------------------------------
Rec1 20010511 45.919998 45.919998 45.810001 1.000000 45.810001 9.520000
-----------------------------------------------------------------------
As you can see, instead of 45.92, I get 45.919998. When I do a comparsion in my
c program
between two floats, and say one float is 45.92, the test fails, even though the
input
file has 45.92. Is it a bug in scanf ? Or am I missing something ?
Thanks
Mudit
Here is the C source code
-------------- c source code ------------------
#include <stdio.h>
int p_date;
float p_close, p_high, p_low, p_vol, p_open, p_tm;
int main(int argc, char ** argv)
{
int num = 0;
int nf;
while (1)
{
nf = scanf("%d %f %f %f %f %f %f",
&p_date,
&p_close,
&p_high,
&p_low,
&p_vol,
&p_open, &p_tm);
if (nf < 0)
exit(0);
num++;
printf("Rec%d %d %f %f %f %f %f %f\n",
num,
p_date,
p_close,
p_high,
p_low,
p_vol,
p_open, p_tm);
}
return 1;
}
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/
- scanf of floats and then prints is not same,
Mudit Wahal <=