john@newave.UUCP (John A. Weeks III) (05/25/90)
In article <411@yonder.UUCP> michael@yonder.UUCP (Michael E. Haws) writes: >Is it reasonable to expect that the following code should >work the same on all platforms, and produce "good" results? > > if ((45.0 / 100.0) == .45) > printf("good\n"); > else > printf("bad\n"); E-gads. Don't ever try "exact" comparisions of floating point numbers. After all, floating point numbers in a computer are a binary representation of an "approximation" to the real number value that you immagined when you typed in the base 10 symbols. You can compare integers representations, within the limits of the machine representation, but floating point numbers should be range checked. For example, try the following: double ratio; ratio = 45.0 / 100.0; if ( ratio > 0.44999 && ratio < 0.45001 ) printf( "good\n" ); else printf( "bad\n" ); Some people like to use a subtract and a "fabs" of some sort (macro or a procedure): if ( fabs( (45.0 / 100.0) ) < 0.001 ) printf( "good\n" ); else printf( "bad\n" ); In this manner, you can substiture a define or a variable for the 0.001 incase you decide to globally change your tolerance level. There are a few legit places that you can compare real numbers, but this is best left for experts that know _when_ and _why_ it is legit. -john- -- =============================================================================== John A. Weeks III (612) 942-6969 john@newave.mn.org NeWave Communications ...uunet!rosevax!bungia!wd0gol!newave!john ===============================================================================