printf: displaying sum of very large numbers

Discussion in 'C Programming' started by dustu@dustu.com, May 5, 2004.

  1. Guest

    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 ;
    }
    <---
    , May 5, 2004
    #1
    1. Advertising

  2. In article <>,
    "" <> 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/book/c_guide/2.12.html#printf

    and scroll down to the "formatted I/O functions" section.
    Mark A. Odell, May 5, 2004
    #2
    1. Advertising

  3. Richard Bos Guest

    () 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
    Richard Bos, May 5, 2004
    #3
  4. jacob navia Guest

    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 ;
    }
    jacob navia, May 5, 2004
    #4
  5. Mac Guest

    On Wed, 05 May 2004 09:15:33 +0000, Mark A. Odell wrote:

    > In article <>,
    > "" <> 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/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
    Mac, May 6, 2004
    #5
  6. Guest

    "jacob navia" <> wrote in message news:<c7b7tp$tb2$>...
    > 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.
    , May 11, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    454
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,594
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    410
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    504
    Dave Thompson
    Feb 14, 2005
  5. bmm
    Replies:
    0
    Views:
    130
Loading...

Share This Page