Why is FLT_MIN equal to zero?

◇◆丶佛笑我妖孽 提交于 2019-11-27 12:23:20
Nick Forge

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!