TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Chris Mcbrien
from: David Noon
date: 1994-10-10 19:29:00
subject: C/Set++

On Sunday, 1994-10-09  Chris Mcbrien wrote to All about "C/Set++" as
follows:

CM> Could someone with a similar setup please compile the following
CM> program:
CM> 
CM> main()
CM> {
CM>         float temp=26.4;
CM> }
CM> 
CM> Then debug it, and tell me what you see as the value of 
CM> temp, after the assignment?
CM> 
CM> My system tells me the value is 2.639996E+01.  Now that's 
CM> not a huge difference, I admit, but it's enough to really 
CM> screw up my program.

Hi Chris,

You have just discovered why responsible programmers do not use
floating point arithmetic for financial applications. It is also one
reason why C has not replaced COBOL in the financial applications
area.

In binary floating point, there is no exact repesentation of 26.4; the
nearest you can get using IEEE single precision is 26.39996. Your
decimal number has been rounded to the nearest binary floating point
number. You can try double or long double instead of float, but these
just reduce the rounding error rather than eliminate it.

In this regard, C suffers from the same linguistic limitations as
FORTRAN. You will need to change languages, at least up to C++, if you
want to avoid this problem. Some C++ compilers include a BCD class,
but these usually run excruciatingly slowly (Borland's does); you can
also write your own, if your knowledge of number theory is up to
keeping track of the decimal point.

Binary Coded Decimal, or BCD, eliminates the rounding errors, since
26.4 (or any other decimal number) can be repesented exactly in BCD,
with an implied decimal point. Both COBOL and PL/I support hardware
implementations of BCD as native features of the language. Indeed, PL/I
supports this (BCD representation) in both REAL and COMPLEX modes.

Regards

Dave

 * KWQ/2 1.2g * e^B01000000fed4  Not NOW, you stupid machine!
--- Maximus/2 2.01wb

* Origin: OS/2 Shareware BBS, Fairfax, VA: 703-385-4325 (1:109/347)
SEEN-BY: 12/2442 54/54 620/243 624/50 632/348 640/820 690/660 711/409 410 413
SEEN-BY: 711/430 807 808 809 934 942 949 712/353 623 713/888 800/1
@PATH: 109/347 2 7 3615/50 229/2 12/2442 711/409 54/54 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.