简体   繁体   中英

Why is FLT_MIN equal to zero?

limits.h specifies limits for non-floating point math types, eg 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?

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 (normalized) number greater than zero . (There are much smaller non-normalized numbers).

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

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

Whenever you will try to print the value of FLT_MIN from standard header file float.h ,you will get 0.000000(as you are seeing in your output screen). That is not actually a error. You are getting this result because the format specifier %f. Generally %f print 6 digits after the decimal point but in this case the signed negative value is so small that you need to print significant amount of digits after the decimal point.

I have used %.54f(machine dependent) to get the desired- result(0.000000000000000000000000000000000000011754943508222875 for my system).

//Check this on your system

#include<stdio.h>
#include<float.h>
int main()
{
    printf("Minimum signed float %.55f\n",FLT_MIN);
    printf("Minimum signed float %e\n",FLT_MIN);
    return 0;
}

//Output :-

// Minimum signed float 0.0000000000000000000000000000000000000117549435082228750

// Minimum signed float 1.175494e-038

I think now it's clear to you why you are getting 0.000000 for CHAR_MIN and how to get the correct result with the same format specifier.Though you can use %e for better formatted result.

Why is FLT_MIN equal to zero?

It is not equal to zero. It appears as 0.000000 due to using "%f" which prints 6 decimal digits after the decimal place . .
FLT_MIN often has a value about 1.17549435e-38.


Reference

Although this question has been answered as to why, I thought I would post the exact values for FLT_TRUE_MIN, FLT_MIN, FLT_MAX as well as their nearest float neighbors when float is binary32 .

// Approximate value, exact value
Before, FLT_TRUE_MIN, after
 0.00000000e+00 0.0
 1.40129846e-45 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125
 2.80259693e-45 0.0000000000000000000000000000000000000000000028025969286496341418474591665798322625605238837530315435141365677795821653717212029732763767242431640625
Before, FLT_MIN, after
 1.17549421e-38 0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875
 1.17549435e-38 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625
 1.17549449e-38 0.00000000000000000000000000000000000001175494490952133940450443629595204006810278684798281709160328881985245648433835441437622648663818836212158203125
Before, FLT_MAX, after
 3.40282326e+38 340282326356119256160033759537265639424.0
 3.40282347e+38 340282346638528859811704183484516925440.0
            inf inf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM