printf: displaying sum of very large numbers

D

dustu

Hi,

I'm using lcc-win32 on Windows 2000 as compiler/IDE. The details are:

Input file (sample - the no. of digits per line may change):
--->
1111111111111111.00
1111111111111111.01
1111111111111111.02
<---

Objective : Reading each line and displaying the sum at the end
Desired output : 3333333333333333.03
Current output : 3333333333333333.000000

Code :
--->
#include<stdio.h>
#include<stdlib.h>

int read_lines(char *filename)
{
long double sum ;
char buf[256] ;
int new_line_ascii = 10 ;
char i ;
FILE *ffile ;

if ((ffile = fopen(filename,"r"))==NULL) { return 2 ; }

sum=0 ;
while (fgets(buf,sizeof(buf), ffile)) { sum+=atof(buf) ; }
printf ("%LF\n",sum) ;
return fclose(ffile) ;
}

int main(int argc, char *argv[])
{
char *fname ;
int err_code, j = 0 ;

if (argc < 2) {
err_code = 2 ;
printf("Usage : %s <filename> \n",argv[0]) ;
}
else {
for (j=1;j<argc;j++) {
err_code = read_lines((fname = argv[j])) ;
if (err_code != 0 ) {
printf("\nError: Could not open file [%s]\n",fname) ;
}
}
}
return err_code ;
}
<---
 
M

Mark A. Odell

I'm using lcc-win32 on Windows 2000 as compiler/IDE.

Shouldn't matter if this is ISO C.

The details are: Input
file (sample - the no. of digits per line may change): --->
1111111111111111.00
1111111111111111.01
1111111111111111.02
<---
Objective : Reading each line and displaying the sum at the end
Desired output : 3333333333333333.03
Current output : 3333333333333333.000000

Why don't you tell printf() how many digits you want to the right of the
decimal point? E.g. printf("%19.2f\n", someFloat);

Why do I sense you have not read and re-read all the formatting flags for
printf? See:

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#printf

and scroll down to the "formatted I/O functions" section.
 
R

Richard Bos

Input file (sample - the no. of digits per line may change):
--->
1111111111111111.00
1111111111111111.01
1111111111111111.02
<---

Objective : Reading each line and displaying the sum at the end
Desired output : 3333333333333333.03
Current output : 3333333333333333.000000

And what makes you think your floats, or even your doubles or long
doubles have seventeen digits of precision? If you want that kind of
accuracy, you may need a specialised math library.

BTW, atof() is risky, because it doesn't have good error checking. Use
strtod() instead.

Richard
 
J

jacob navia

You need more precision than double probably. Using your input data
1111111111111111.00
1111111111111111.01
1111111111111111.02
I obtain:
D:\lcc\mc52\test>tatod gg
3333333333333333.030000

The program is exactly like youtrs but using strtold instead of atod.
#include<stdio.h>
#include<stdlib.h>

int read_lines(char *filename)
{
long double sum ;
char *p,buf[256] ;
int new_line_ascii = 10 ;
char i ;
FILE *ffile ;

if ((ffile = fopen(filename,"r"))==NULL) {
return 2 ;
}

sum=0 ;
while (fgets(buf,sizeof(buf), ffile)) {
sum+=strtold(buf,&p) ;
}
printf ("%LF\n",sum) ;
return fclose(ffile) ;
}

int main(int argc, char *argv[])
{
char *fname ;
int err_code, j = 0 ;

if (argc < 2) {
err_code = 2 ;
printf("Usage : %s <filename> \n",argv[0]) ;
}
else {
for (j=1;j<argc;j++) {
err_code = read_lines((fname = argv[j])) ;
if (err_code != 0 ) {
printf("\nError: Could not open file [%s]\n",fname) ;
}
}
}
return err_code ;
}
 
M

Mac

Shouldn't matter if this is ISO C.



Why don't you tell printf() how many digits you want to the right of the
decimal point? E.g. printf("%19.2f\n", someFloat);

Why do I sense you have not read and re-read all the formatting flags for
printf? See:

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#printf

and scroll down to the "formatted I/O functions" section.

I don't think that's the point. The point is that there is not enough
precision. That is, the 0.03 is getting lost. There is really nothing that
can be done about this except use long doubles, as another poster
suggested.

--Mac
 
D

dustu

jacob navia said:
You need more precision than double probably. Using your input data
1111111111111111.00
1111111111111111.01
1111111111111111.02
I obtain:
D:\lcc\mc52\test>tatod gg
3333333333333333.030000

The program is exactly like youtrs but using strtold instead of atod.

Sorry for responding late.

Thanks jacob. strtold does it.

Richard, strtod returns only 3333333333333333.00 Btw, the kind of
numbers I'm adding up, are upto only two decimal places, so that part
is fixed.

Thanks for all the help. I'm a newbie & this was my first program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top