C语言 为什么 FLT_MIN 等于零?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2528039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:58:09  来源:igfitidea点击:

Why is FLT_MIN equal to zero?

cfloating-pointnumeric-limits

提问by Nick Forge

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

limits.h指定非浮点数学类型的限制,例如INT_MININT_MAX。这些值是您可以使用 int 表示的最负值和最正值。

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

float.h有相关定义FLT_MINFLT_MAX。如果您执行以下操作:

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

You get the following output:

您将获得以下输出:

FLT_MIN = 0.000000, FLT_MAX = 340282346638528859811704183484516925440.000000

FLT_MAXis equal to a really large number, as you would expect, but why does FLT_MINequal zero instead of a really large negative number?

FLT_MAX正如您所料,等于一个非常大的数,但为什么FLT_MIN等于零而不是一个非常大的负数?

回答by Nick Forge

It's not actually zero, but it might look like zero if you inspect it using printfor NSLogby using %f.
According to float.h(at least in Mac OS X 10.6.2), FLT_MINis described as:

它实际上不是零,但如果您使用printfNSLog使用%f.
根据float.h(至少在 Mac OS X 10.6.2 中),FLT_MIN被描述为:

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

Note the positivein that sentence: FLT_MINrefers to the minimum (normalized) number greater than zero. (There are much smaller non-normalized numbers).

请注意该句子中的正数FLT_MIN指的是大于零的最小(标准化)数。(非标准化数字要小得多)。

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

如果您想要最小浮点数(包括负数),请使用-FLT_MAX.

回答by Jonathan Leffler

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.

'%f' 格式以固定格式打印 6 位小数。由于 FLT_MIN 小得多,因此在定点上看起来为零。如果您使用 '%e' 或 '%g' 格式,您会得到更好的格式答案。与 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

回答by chux - Reinstate Monica

Why is FLT_MINequal to zero?

为什么FLT_MIN等于零?

It is not, it appears as 0.000000due to using "%f"which prints 6 decimal digits after the ..
FLT_MINoften has a value about 1.17549435e-38.

不是,它似乎是0.000000由于使用"%f"which 在..
FLT_MIN通常具有大约 1.17549435e-38 的值。



Reference

参考

Although this question has been answered as to why, I thought I would post the exactvalues for FLT_TRUE_MIN, FLT_MIN, FLT_MAXas well as their nearest floatneighbors when floatis binary32.

虽然这个问题已经回答了为什么,但我想我会在is binary32时发布它们的确切FLT_TRUE_MIN, FLT_MIN, FLT_MAX以及它们最近的float邻居。float

// 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

回答by Apurba Ghosh

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.

每当您尝试从标准头文件 float.h 打印 FLT_MIN 的值时,您将得到 0.000000(如您在输出屏幕中看到的)。这实际上不是错误。你得到这个结果是因为格式说明符 %f。通常 %f 在小数点后打印 6 位数字,但在这种情况下,带符号的负值非常小,您需要在小数点后打印大量数字。

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

我已经使用 %.54f(取决于机器)来获得所需的结果(对于我的系统,0.000000000000000000000000000000000000011754943508222875)。

//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

// 最小有符号浮点数 0.0000000000000000000000000000000000000117549435082228750

// Minimum signed float 1.175494e-038

// 最小有符号浮点数 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.

我想现在你很清楚为什么你会得到 0.000000 的 CHAR_MIN 以及如何使用相同的格式说明符获得正确的结果。虽然你可以使用 %e 来获得更好的格式化结果。