tags:

views:

3575

answers:

5

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.

+15  A: 

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.

Andrew Hare
+19  A: 

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);
fortran
+1 For pointing out the symmetry of of floating point numbers :)
Andrew Hare
What about C/C++ implementations which do not use IEEE 754 floats?
Steve Jessop
@onebyone, tell me one that doesn't... I think you cannot, because all C/C++ compilers use the native processor format for floating point numbers, and now I cannot think about any FPU that doesn't adhere to the IEEE 754 standard (maybe in the dark ages of computing, when everybody had his own in house formats...)
fortran
gcc's manual for -ffast-math says"Sets -fno-math-errno, -funsafe-math-optimizations, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and -fcx-limited-rangeThis option is not turned on by any -O option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications for math functions. It may, however, yield faster code for programs that do not require the guarantees of these specifications."Fast math is a common setting, and the Intel ICC for example defaults to it.All in all, not sure what this means for me :-)
Will
It means implementations don't use IEEE 754 arithmetic, but to be fair those options do still use IEEE representation. You might find some emulation libraries using non-IEEE representation, since not all processors have a native float format (although they may publish a C ABI that includes a format, corresponding to emulation libs supplied by the manufacturer). Hence not all compilers can use one. Just depends what you mean when you ask for "standard and/or portable", there's portable in principle and portable in practice.
Steve Jessop
@Will If you read carefully, it says math functions, not mentions anything about number format representation. I think you can tell apart those two very different things... The IEEE semantics specify that the approximation of the result of an floating point operation must be the same as if the operation was done with inifinite precission and then truncated (maybe there were some more implications, but my numerical analysis lessons seem very far away now)... I'm sure you could find more about this fascinating theme on the web ;-)
fortran
+15  A: 

-DBL_MAX in ANSI C

dfa
this seems the most standard and portable
Will
downvotes are pointless with an explaination
dfa
+4  A: 
- std::numeric_limits<double>::max()

should work just fine

Numeric limits

MadH
+1  A: 

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()
Christoph