浮动小于FLT_MIN。为什么FLT_TRUE_MIN? [英] floats smaller than FLT_MIN. why FLT_TRUE_MIN?

查看:632
本文介绍了浮动小于FLT_MIN。为什么FLT_TRUE_MIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了试图看看在下溢的情况下会发生什么,我发现我可以使浮点数比FLT_MIN小得多。我在OS 10.9上使用xcode 5.1。语言方言是gnu99。

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

int main(int argc,const char * argv [])
{
float underflow = FLT_MIN * 0.0000004;
$ b printf(Float min是%f或%e.\\\
下流是%f或%e \ nMin float exp是%d.\\\
,FLT_MIN,FLT_MIN,下溢,下溢,FLT_MIN_10_EXP);

返回0;





$ p $浮动最小值是0.000000或1.175494e -38。
下溢是0.000000或4.203895e-45

最小浮动exp是-37。


  1. 有没有更有效的方法来证明数据类型的限制?
  2. 为什么FLT_MIN实际上不是最小的浮点值?我应该使用其他常量吗?输入之前的问题后,我发现FLT_TRUE_MIN。这个数字是什么? 2个可能性使得低于最低限度:


    1. float 范围:

      典型的 float 数字有2个范围:从 FLT_MAX 下降到<$ c $的全精度(正常范围) c> FLT_MIN
      和第二个范围,精度从 FLT_MIN 下降到 FLT_TRUE_MIN 。这个第二个范围,称为subnormal通常提供大约10 ^ -7更多的范围。
      $ b


      FLT_TRUE_MIN 是最小正浮点数

      $ FLT_MIN 是最小正规浮点数点的数量

      FLT_MIN_10_EXP 是最小的负整数,使得10次上升到该范围内

      C11dr§5.2.4.2.2



      <一般而言 0<数学运算如下: double



      printf() float 传递给 double 。 C允许代码进行优化,使得传递给 printf()的值可以是<$ c $的 double 乘积c> FLT_MIN * 0.0000004 。

        float underflow = FLT_MIN * 0.0000004; 
      printf(%e \\\
      ,下溢);

      如果输出为 4.701976e-45 而不是 4.203895e-45 ,情况就是这样。







    请注意subnormal。对于低于正常(或反常规)数字的令人信服的理由在于以下问题。

      float a, b; 
    ... //以某种方式设置a和b。

    //是否等于2?
    if(a == b)foo();
    if((a - b)== 0)foo();

    如果没有低于正常的数字,那么在 FLT_MIN 将会有一个非零的数学差异,远远低于 FLT_MIN ,结果会变成 0.0
    $ b $ p

    如果子数目不正确,每对不同的 float s的区别可以用< $ C> 0.0 。 **



    **除 +0.0,-0.0 外。签名的零有自己的特点。


    In an attempt to see what would happen in the case of a float underflow I found that I could make float numbers much smaller than FLT_MIN. I'm using xcode 5.1 on OS 10.9. The language dialect is gnu99.

    #include <stdio.h>
    #include <stdlib.h>
    #include <float.h>
    
    int main(int argc, const char * argv[])
    {
        float underflow = FLT_MIN * 0.0000004;
    
        printf("Float min is %f or %e.\nUnderflow is %f or %e\nMin float exp is %d.\n", FLT_MIN, FLT_MIN, underflow, underflow, FLT_MIN_10_EXP);
    
        return 0;
    }
    

    Prints:
    Float min is 0.000000 or 1.175494e-38.
    Underflow is 0.000000 or 4.203895e-45
    Min float exp is -37.

    1. Is there a more effective method of demonstrating the limits of data types?
    2. Why is FLT_MIN not actually the smallest float value? Are there other constants that I'm supposed to be using? After typing the previous question I found FLT_TRUE_MIN. What is this number?

    解决方案

    2 possibilities to get "below minimum":

    1. float range:

      Typical float numbers have 2 ranges: full precision (normal range) from FLT_MAX down to FLT_MIN and a 2nd range with reducing precision from FLT_MIN down to FLT_TRUE_MIN. This 2nd range, called "subnormal" typically provides about 10^-7 more range.

      FLT_TRUE_MIN is the "minimum positive floating-point number"

      FLT_MIN is the "minimum normalized positive floating-point number"

      FLT_MIN_10_EXP is the "minimum negative integer such that 10 raised to that power is in the range of normalized floating-point numbers"

      C11dr §5.2.4.2.2

      In general 0 < FLT_TRUE_MIN <= FLT_MIN <= 10^FLT_MIN_10_EXP <= 10^-37

    2. Math performed as double.

      printf() coverts each float passed to it to a double. C allows code to optimize such that the value passed to printf() may be the double product of FLT_MIN * 0.0000004.

      float underflow = FLT_MIN * 0.0000004;
      printf("%e\n", underflow);
      

      Had the output been 4.701976e-45 rather than 4.203895e-45, this would have been the case.


    Note on "subnormal". A compelling reason for subnormal (or denormal) numbers lies in the following problem.

    float a,b;
    ... // somehow a and b are set.
    
    // Are the 2 below equivalent?
    if (a == b) foo();
    if ((a - b) == 0) foo();
    

    Without subnormal numbers, 2 nearly the same value numbers near FLT_MIN would have a non-zero mathematical difference much below FLT_MIN and the result would round to 0.0.

    With subnormal numbers, the difference of every pair of different floats is representable by something other than 0.0. **

    ** Except +0.0, -0.0. Signed zeros have their own peculiarities.

    这篇关于浮动小于FLT_MIN。为什么FLT_TRUE_MIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆