minimum double value in C/C++

后端 未结 10 1436
天命终不由人
天命终不由人 2020-12-04 08:41

Is there a standard and/or portable way to represent the smallest negative value (e.g. to use negative infinity) in a C(++) program?

DBL_MIN in float.h is the smalle

10条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 09:28

    If you do not have float exceptions enabled (which you shouldn't imho), you can simply say:

    double neg_inf = -1/0.0;
    

    This yields negative infinity. If you need a float, you can either cast the result

    float neg_inf = (float)-1/0.0;
    

    or use single precision arithmetic

    float neg_inf = -1.0f/0.0f;
    

    The result is always the same, there is exactly one representation of negative infinity in both single and double precision, and they convert to each other as you would expect.

提交回复
热议问题