views:

436

answers:

2

limits.h specifies limits for non-floating point math types, e.g. INT_MIN and INT_MAX. These values are the most negative and most positive values that you can represent using an int.

In float.h, there are definitions for FLT_MIN and FLT_MAX. If you do the following:

NSLog(@"%f %f", FLT_MIN, FLT_MAX);

You get the following output:

FLT_MIN = 0.000000, FLT_MAX = 340282346638528859811704183484516925440.000000

FLT_MAX is equal to a really large number, as you would expect, but why does FLT_MIN equal zero instead of a really large negative number?

+6  A: 

It's not actually zero, but it might look like zero if you inspect it using printf or NSLog by using %f. According to float.h (at least in Mac OS X 10.6.2), FLT_MIN is described as:

/* Minimum normalized positive floating-point number, b**(emin - 1). */

Note the positive in that sentence - FLT_MIN refers to the minimum number greater than zero.

If you want the minimum floating point number (including negative numbers), use -FLT_MAX.

Nick Forge
This doesn't even seem like an answer to me - I thought your question was why it's zero instead of something very small and positive.
Jefromi
It could be argued that 0 isn't strictly positive, and that the value should be 1.4e-45 or whatever the value right before underflow is.
Ignacio Vazquez-Abrams
@Ignacio: Yes, that's the point, isn't it?
Jefromi
I've updated the answer - you're absolutely correct about it not being zero. I'll update the question to reflect that it _looks like_ zero when you do a printf, not that it's actually zero.
Nick Forge
The text in the comment, "Minimum normalized positive floating-point number, b**(emin - 1)," is straight out of the C standard, and is valid for any C implementation (see ISO/IEC 9899:TC2 5.2.4.2.2/11).
James McNellis
@Nick: It doesn't look like zero if you use exponential notation.
Jefromi
@Jefromi: You're right, but I'm leaving the answer how it is, since I suspect more people will find the question helpful be saying it looks like zero.
Nick Forge
+2  A: 

The '%f' format prints 6 decimal places in fixed format. Since FLT_MIN is a lot smaller, it looks like zero in fixed point. If you use '%e' or '%g' format, you'd get a better formatted answer. Similarly with the FLT_MAX.

#include <float.h>
#include <stdio.h>
int main(void)
{
    printf("MIN = %f, MAX = %f\n", FLT_MIN, FLT_MAX);
    printf("MIN = %e, MAX = %e\n", FLT_MIN, FLT_MAX);
    return(0);
}


MIN = 0.000000, MAX = 340282346638528859811704183484516925440.000000
MIN = 1.175494e-38, MAX = 3.402823e+38
Jonathan Leffler