473,387 Members | 1,724 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.

Speaking of sum programs (let's keep Razi out of it) here's my

typedef struct KahanAdder_t {
double sum_; /* The current working sum. */
double carry_; /* The carry from the previous operation */
double temp_; /* A temp used to capture residual bits of precision
*/
double y_; /* A temp used to capture residual bits of precision */
} KahanAdder_t;

/* After each add operation, the carry will contain the additional */
/* bits that could be left out from the previous operation. */
void add(const double datum, KahanAdder_t * kp)
{
kp->y_ = datum - kp->carry_;
kp->temp_ = kp->sum_ + kp->y_;
kp->carry_ = (kp->temp_ - kp->sum_) - kp->y_;
kp->sum_ = kp->temp_;
}

void reset( KahanAdder_t * kp)
{
kp->y_ = 0;
kp->temp_ = 0;
kp->carry_ = 0;
kp->sum_ = 0;
}

#define MAXLINELEN 256

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <float.h>
int main(void)
{
KahanAdder_t k = {0};
double d;
double standard_sum = 0;
float standard_sum_f = 0;
clock_t start, end;
unsigned count = 0;
char line[MAXLINELEN];

setvbuf(stdin, NULL, _IOFBF, 1024 * 16);
start = clock();
while (fgets(line, sizeof line, stdin)) {
d = atof(line);
add(d, &k);
standard_sum += d;
standard_sum_f += (float) d;
count++;
}
end = clock();

printf("Standard sum (double) = %20.15f\n"
"Kahan sum (double) = %20.15f\n"
"Standard sum (float) = %20.15f\n"
"Number of items was %u\n"
"Time consumed was %f seconds\n",
standard_sum,
k.sum_,
standard_sum_f,
count,
(end - start) * 1.0 / CLOCKS_PER_SEC
);

return 0;
}
/* Possible Output from a file of 5,000,001 numbers:
C:\tmp\Releasefoo.exe < c:\tmp\n.txt
Standard sum (double) = 27104700.447471067000000
Kahan sum (double) = 27104700.447473072000000
Standard sum (float) = 26857582.000000000000000
Number of items was 5000001
Time consumed was 4.594000 seconds
*/
Jun 27 '08 #1
0 914

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

Similar topics

77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
14
by: John Salerno | last post by:
Bear with me, but I've been reading a lot about how the .NET languages are platform independent, and I assume this means a program written in C# can be run on a Unix or Mac machine. If this...
8
by: Terry Olsen | last post by:
Can console programs do events? Or are they strictly linear programs?
55
by: Steve | last post by:
I have to develop several large and complex C++ hardware test programs that should work under DOS, most likely with 32-bit DOS extender. Development workstation OS would be Microsoft XP. Quite some...
13
by: Snis Pilbor | last post by:
Hello, Here is an idea I've been toying with to speed up programs but still keep them portable. It's just a very handwavey rough description right now since I haven't worked out details. The...
27
by: HKSHK | last post by:
Hello, I have this problem: I wrote some DLLs with VB.Net 2003 which I use with my programs. But I want to avoid that I have to go down to "DLL hell" and to copy all used dlls into each program...
24
by: c language | last post by:
Hello all, I am using 'Valgrind' to fix the memory leaks of my programs. I am looking for other programs to see how they are detecting the problems. Does any of you have experience in working...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Sharing in Barters Inbox Reply WORKING IN FAITH to katrina's_proo. show details 12:38 pm (12 minutes ago) This one is for you Donald Trump... Reply from Craig Somerford...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Programs dealing with autoruns Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list programs that help me to view/modify the autoruns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.