[comp.sys.ibm.pc] Rounding errors on a PC

huisjes@ark.cs.vu.nl (Michiel Huisjes) (04/21/87)

We are developing an administration package for IBM PC and compatibles,
using the Microsoft C compiler 4.00 and no 8087 co-processor. When we
entered the number "123456.01" and printing it afterwards, we got
"123456.09999999" back. Using this float in calculations leads (very quickly)
to a rounding error of one (or more) cents. Although this amount of money,
isn't that much to loose, it shouldn't happen in an administration package.

Is this a compiler error, or just the inaccuracy of an IBM PC? If the latter
is the case, what can be done about it. I know that the COBOL compiler
uses BCD (Binary Coded Decimals) for its calculations, but implementing
this in C, would mean writing all substraction, addition, multiplication, etc.
routines all over.
Do such packages already exist? Is there an undocumented obscure C compiler 
flag I don't know about? Has anybody out there encountered the same problem
and if so, how did they deal with it?

Answers to these questions or solutions are VERY welcome. If any useful
solutions are presented, or wise remarks are made, I will summarize
to the net.

Please send replies to:

			Michiel Huisjes.  (huisjes@cs.vu.nl)
			{seismo|decvax|philabs}!mcvax!vu44!huisjes

Thanx in advance.

timothym@tekigm2.TEK.COM (Timothy D Margeson) (04/22/87)

Go buy Turbo Pascal BCD version, then write the math functions you need as
externals to the worldly 'C' your using, then link it all together (of course
the external really have to be handled as fork()s, but 'C' does that well
enough.


-- 
Tim Margeson (206)253-5240
PO Box 3500  d/s C1-937                          @@   'Who said that?'  
Vancouver, WA. 98668
{amd..hplabs}cae780!tektronix!tekigm2!timothym (this changes daily)

jvc@mirror.UUCP (04/24/87)

/* Written 10:21 am  Apr 21, 1987 by huisjes@ark.UUCP in mirror:comp.sys.ibm.pc */
/* ---------- "Rounding errors on a PC" ---------- */

We are developing an administration package for IBM PC and compatibles,
using the Microsoft C compiler 4.00 and no 8087 co-processor. When we
entered the number "123456.01" and printing it afterwards, we got
"123456.09999999" back. Using this float in calculations leads (very quickly)
to a rounding error of one (or more) cents. Although this amount of money,
isn't that much to loose, it shouldn't happen in an administration package.

Is this a compiler error, or just the inaccuracy of an IBM PC? If the latter
is the case, what can be done about it. I know that the COBOL compiler
uses BCD (Binary Coded Decimals) for its calculations, but implementing
this in C, would mean writing all substraction, addition, multiplication, etc.
routines all over.
Do such packages already exist? Is there an undocumented obscure C compiler 
flag I don't know about? Has anybody out there encountered the same problem
and if so, how did they deal with it?

Answers to these questions or solutions are VERY welcome. If any useful
solutions are presented, or wise remarks are made, I will summarize
to the net.

Please send replies to:

			Michiel Huisjes.  (huisjes@cs.vu.nl)
			{seismo|decvax|philabs}!mcvax!vu44!huisjes

Thanx in advance.
/* End of text from mirror:comp.sys.ibm.pc */

jvc@mirror.UUCP (04/27/87)

Guess what, I screwed up.  I started to reply to this base note a few
days ago but decided to email directly instead.  However, when I
attempted to cancel my response, I aborted from emacs forgetting that
even though I didn't save the buffer the notes system would remember
that I copied the base note into my response.  The result, duplicate
postings of the base note but with my name in the header of the newer
one as well and my address info.

I'm now getting email in regard to that note.   Please STOP IT.
It's great to see so much helpful info from the net but I'm not the
one who needs the info.  I'll forward the mail I received so far but
please send all additional mail (on the rounding topic) to: 

	Michiel Huisjes (huisjes@cs.vu.nl)
	{seismo!decvax!philabs}!mcvax!vu44!huisjes

Thanks,
jvc (too avoid confusion and more mailings, I'm not giving my address)