[comp.sys.mips] MAX_FLT isn't?

bin@primate.wisc.edu (Brain in Neutral) (10/17/89)

So what am I doing wrong here?

test.c:

# include	<stdio.h>
# include	<float.h>

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

Paul DuBois
stewed-monkey-heads@primate.wisc.edu

earl@wright (Earl Killian) (10/18/89)

In article <898@uakari.primate.wisc.edu>, bin@primate (Brain in Neutral) writes:
># include	<stdio.h>
># include	<float.h>
>
>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)".
-- 

bin@primate.wisc.edu (Brain in Neutral) (10/18/89)

From article <29655@wright.mips.COM>, by earl@wright (Earl Killian):
| In article <898@uakari.primate.wisc.edu>, bin@primate (Brain in Neutral) writes:
|>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.

| ...

| 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)".

Hm, well, it works under Ultrix on my VAX (with or w/o the (float)
stuff).  Thus my disappointment.  Oh, well.


Paul DuBois
dubois@primate.wisc.edu