473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

printf: displaying sum of very large numbers

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 ;
}
<---
Nov 14 '05 #1
5 3081
In article <ed**************************@posting.google.com >,
"du***@dustu.com" <du*****@yahoo.co.in> 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.
Nov 14 '05 #2
du*****@yahoo.co.in (du***@dustu.com) 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
Nov 14 '05 #3
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 ;
}
Nov 14 '05 #4
Mac
On Wed, 05 May 2004 09:15:33 +0000, Mark A. Odell wrote:
In article <ed**************************@posting.google.com >,
"du***@dustu.com" <du*****@yahoo.co.in> 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

Nov 14 '05 #5
"jacob navia" <ja***@jacob.remcomp.fr> wrote in message news:<c7**********@news-reader3.wanadoo.fr>...
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.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Vinokur | last post by:
Dann Corbit has implemented function int ilog2 (unsigned long) at http://groups.google.com/groups?selm=lkPa4.2165%24I41.1498%40client Is exist a similar C++-function for very large numbers,...
5
by: Todd Steury | last post by:
Greetings Python'ers: I'm just an amature who occasionally uses Python for complex mathematical models. The current model I'm working with occasionally generates really large numbers that are...
7
by: Susan Bricker | last post by:
Sorry..I keep hitting the wrong key and sending the post too soon... The problem: COMBOBOX not displaying all possible rows. The RecordSource (generated by SQL view of the Query Builder) is:...
1
by: Joshco | last post by:
Does anybody know how to force visual to use a number this is around 1000 digits long and 64 bit floating point. I am a beginner in programing.
6
by: pasukaru | last post by:
I need to work with big integers ( around 2.^64), and I know it is not possible to have integer that big. So I use "double" instead, and it is ok with calculations. But when I want to print them...
15
by: bryanvick | last post by:
I just started learning C, and wrote this small program to play around with the printf function in stdio.h. At the console, I am able to type input at the first 2 getchar() calls, but when I call...
13
by: pozz | last post by:
Is there some modifier that skips a parameter of a printf? For example, I pass three parameters: printf( <format string>, '-', value/10, value%10 ); In certain cases I want to print the minus...
11
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab,...
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.