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 smallest positive number.
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 smallest positive number.
Try this:
-1 * numeric_limits<double>::max()
Reference: numeric_limits
This class is specialized for each of the fundamental types, with its members returning or set to the different values that define the properties that type has in the specific platform in which it compiles.
Floating point numbers (IEEE 754) are symmetrical, so if you can represent the greatest value (DBL_MAX
or numeric_limits<double>::max()
), just prepend a minus sign.
And then is the cool way:
double f;
(*((long long*)&f))= ~(1LL<<52);
Are you looking for actual infinity or the minimal finite value? If the former, use
-numeric_limits<double>::infinity()
which only works if
numeric_limits<double>::has_infinity
Otherwise, you'll have to use
-numeric_limits<double>::max()