Minimum double value in C / C ++

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

DBL_MIN in float.h is the smallest positive number.

+67
c ++ c math
Jul 20 '09 at 13:23
source share
8 answers

-DBL_MAX in ANSI C , which is defined in float.h.

+102
Jul 20 '09 at 13:29
source share

Floating-point numbers (IEEE 754) are symmetric, so if you can represent the largest value ( DBL_MAX or numeric_limits<double>::max() ) just add a minus sign.

And then this is a cool way:

 double f; (*((long long*)&f))= ~(1LL<<52); 
+65
Jul 20 '09 at 13:27
source share

Try the following:

 -1 * numeric_limits<double>::max() 

Link: numeric_limits

This class is specialized for each of the fundamental types, with its members returning or various values ​​defining properties that are of the type of the particular platform on which it compiles.

+31
Jul 20 '09 at 13:26
source share

In C use

 #include <float.h> const double lowest_double = -DBL_MAX; 

In C ++ pre-11 use

 #include <limits> const double lowest_double = -std::numeric_limits<double>::max(); 

In C ++ 11 onwards use

 #include <limits> constexpr double lowest_double = std::numeric_limits<double>::lowest(); 
+28
Jul 15 '13 at 12:38
source share

Are you looking for actual infinity or minimum end value? If the first, use

 -numeric_limits<double>::infinity() 

which only works if

 numeric_limits<double>::has_infinity 

Otherwise you should use

 numeric_limits<double>::lowest() 

which is introduced in C ++ 11.

If lowest() not available, you can return to

 -numeric_limits<double>::max() 

which, in principle, may differ from lowest() , but usually this is not in practice.

+20
Jul 20 '09 at 13:48
source share
 - std::numeric_limits<double>::max() 

should work fine

Numerical restrictions

+7
Jul 20 '09 at 13:37
source share

A truly portable C ++ solution

As from C ++ 11 you can use numeric_limits<double>::lowest() . According to the standard, it returns exactly what you are looking for:

The final value of x is such that there is no other final value of y, where y < x .
The value for all specializations in which is_bounded != false .

Demo version




There are many incompatible answers in C ++!

There will be many answers for -std::numeric_limits<double>::max() .

Fortunately, they will work well in most cases. Floating-point coding schemes decompose the number in the mantissa and exponent, and most of them (for example, the popular IEEE-754 ) use a separate character bit that does not belong to the mantissa. This allows you to convert the largest positive value to the smallest negation by simply clicking the sign:

enter image description here

Why are they not tolerated?

The standard does not impose any floating point standard.

I agree that my argument is a little theoretical, but suppose that some eccentric compiler developer would use a revolutionary coding scheme with a mantissa encoded in two versions of two additions . The coding of the two additions is not symmetrical. for example, for a signed 8 bit char, the maximum positive is 127, but the minimum minus is -128. Thus, we could imagine that floating point encoding shows similar asymmetric behavior.

I am not aware of any coding scheme like this, but the fact is that the standard does not guarantee that switching the sign gives the expected result . So this popular answer (sorry guys!) Cannot be considered a fully portable standard solution! / * at least if you are not claiming that numeric_limits<double>::is_iec559 true * /

+7
Sep 22 '16 at 20:37
source share

The original question is about infinity. So why not use

 #define Infinity ((double)(42 / 0.0)) 

as defined by IEEE? Of course, this can be denied.

+1
Sep 22 '16 at 19:33
source share



All Articles