Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!gem.mps.ohio-state.edu!apple!mips!earl@wright From: earl@wright (Earl Killian) Newsgroups: comp.sys.mips Subject: Re: MAX_FLT isn't? Message-ID: <29655@wright.mips.COM> Date: 17 Oct 89 18:35:56 GMT References: <898@uakari.primate.wisc.edu> Sender: earl@mips.COM Reply-To: earl@wright (Earl Killian) Organization: MIPS Computer Systems Inc. Lines: 49 In-reply-to: bin@primate.wisc.edu (Brain in Neutral) In article <898@uakari.primate.wisc.edu>, bin@primate (Brain in Neutral) writes: ># include ># include > >main () >{ >float f; > > f = FLT_MAX; > printf ("%g %g\n", f, (float) FLT_MAX); > printf ("%g\n", f - FLT_MAX); > printf ("%f %f\n", f, (float) FLT_MAX); > printf ("%f\n", f - FLT_MAX); >} >% cc test.c >% a.out >3.40282e+38 3.40282e+38 >-3.61471e+29 >340282346638528860000000000000000000000.000000 340282346638528860000000000000000000000.000000 >-361471124579617760000000000000.000000 >So what am I doing wrong here? You're assuming that all computations are done in single precision, which is not the case in most C implementations. K&R C said that "f - FLT_MAX" would be done by converting f to double, FLT_MAX to double, and then subtracting. Convert f to double and you get 3.4028234663852886e+38. That's the closest decimal number of 17 digits to the single-precision value. Convert the string "3.40282347e+38" (which is the definition of FLT_MAX in float.h) to double and you get 3.4028234699999998e+38. That's the closest dp number to the decimal. Subtract, and you get the result above. You would have better seen the difference if you'd left the (float) off the front of FLT_MAX in the printf and printed more digits. You would have gotten the zero that you expect if you put the same "(float)" in front of the FLT_MAX in the subtract. Does this clarify what's going on? You could argue that float.h should have put "(float)" into the definition of FLT_MAX. Then you wouldn't have seen the pecularities of floating point arithmetic in this case. I think it was AT&T that omitted the "(float)". --