Path: utzoo!attcan!uunet!nih-csl!lhc!adm!cmcl2!phri!roy From: roy@phri.nyu.edu (Roy Smith) Newsgroups: comp.sys.mac.programmer Subject: Re: Why can't the Mac add? Message-ID: <1990Sep27.222809.9210@phri.nyu.edu> Date: 27 Sep 90 22:28:09 GMT References: <45060@apple.Apple.COM> <4485@sage.cc.purdue.edu> <26fc2801.6f3@petunia.CalPoly.EDU> Sender: news@phri.nyu.edu (News System) Organization: Public Health Research Institute, New York City Lines: 43 rcfische@polyslo.CalPoly.EDU (Ray Fischer) writes: > Although many floating point numbers ARE exact (0.125 for example), some > cannot be, which is why testing for equality using floating point numbers > is always dicey at best. It just occurred to me, that although you *should* be able to represent 0.125 as an exact binary fraction (namely 0b001, where b represents the binary point), there is no guarantee at all that if you write the constant 0.125 in a program, the compiler will convert that into the exact binary floating point constant 0.00100000... The process of converting the ascii string "0.125" into a binary floating constant probably involves evaluating something like: 1.0/10.0 + 2.0/100.0 + 5.0/1000.0 or maybe: (((5.0/10.0 + 2.0)/10.0 + 1.0)/10.0 both of which involve inexact intermediate results. Somewhat to my surprise, when I compiled and ran the following on a Sun-3/50 with both -fsoft and -f68881, and on a 3/160 with -ffpa: main () { double x, y; x = 0.125; y = (x * 1000.0) - 125.0; if (y == 0.0) printf ("Yowza!\n"); else printf ("Cowabunga!\n"); } all three printed "Yowza!", but I'm sure you could find machines where that wasn't true. -- Roy Smith, Public Health Research Institute 455 First Avenue, New York, NY 10016 roy@alanine.phri.nyu.edu -OR- {att,cmcl2,rutgers,hombre}!phri!roy "Arcane? Did you say arcane? It wouldn't be Unix if it wasn't arcane!"