Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf: displaying sum of very large numbers

Reply
Thread Tools

printf: displaying sum of very large numbers

 
 
dustu@dustu.com
Guest
Posts: n/a
 
      05-05-2004
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 ;
}
<---
 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      05-05-2004
In article <(E-Mail Removed) >,
"(E-Mail Removed)" <(E-Mail Removed)> wrote:


> 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/b...12.html#printf

and scroll down to the "formatted I/O functions" section.
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      05-05-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) ((E-Mail Removed)) wrote:

> 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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-05-2004
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 ;
}


 
Reply With Quote
 
Mac
Guest
Posts: n/a
 
      05-06-2004
On Wed, 05 May 2004 09:15:33 +0000, Mark A. Odell wrote:

> In article <(E-Mail Removed) >,
> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>
>
>> 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/b...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

 
Reply With Quote
 
dustu@dustu.com
Guest
Posts: n/a
 
      05-11-2004
"jacob navia" <(E-Mail Removed)> wrote in message news:<c7b7tp$tb2$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal display for very large/small numbers GGP Java 3 04-02-2007 05:13 PM
Generate a sequence of random numbers that sum up to 1? Anthony Liu Python 14 04-24-2006 06:35 AM
SOAP and very very large numbers bmm Ruby 0 04-18-2006 11:14 PM
ilog2() for very large numbers Alex Vinokur C++ 3 11-23-2004 09:00 AM
basic calculations with very large numbers mj C++ 2 09-12-2003 09:02 PM



Advertisments