minimum double value in C/C++

后端 未结 10 1431
天命终不由人
天命终不由人 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:32

    Are you looking for actual infinity or the minimal finite value? If the former, use

    -numeric_limits::infinity()
    

    which only works if

    numeric_limits::has_infinity
    

    Otherwise, you should use

    numeric_limits::lowest()
    

    which was introduces in C++11.

    If lowest() is not available, you can fall back to

    -numeric_limits::max()
    

    which may differ from lowest() in principle, but normally doesn't in practice.

提交回复
热议问题