Modeling infinity for the largest double value

后端 未结 9 1381
予麋鹿
予麋鹿 2020-12-31 05:10

The question is about modeling infinity in C++ for the double data type. I need it in a header file, so we cannot use functions like numeric_limits

相关标签:
9条回答
  • 2020-12-31 05:44

    Not sure why you can't use std::numeric_limits in a header file. But there is also this carried over from ANSI C:

    #include <cfloat>
    
    DBL_MAX
    
    0 讨论(0)
  • 2020-12-31 05:45

    Wouldn't this work?

    const double infinity =  1.0/0.0;
    
    0 讨论(0)
  • 2020-12-31 05:50

    I thought the answer was "42.0" ;)

    This article might be of interest:

    http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    Or this:

    http://www.cplusplus.com/reference/clibrary/cfloat/

    MAXimum Maximum finite representable floating-point number:

    FLT_MAX  1E+37
    DBL_MAX  1E+37
    LDBL_MAX 1E+37  
    
    0 讨论(0)
  • 2020-12-31 05:50

    From Wikipedia:

    0x 7ff0 0000 0000 0000   = Infinity
    0x fff0 0000 0000 0000   = −Infinity
    
    0 讨论(0)
  • 2020-12-31 05:53

    numeric_limits functions are all constexpr so they work just fine as compile time constants (assuming you're using the current version of C++). So std::numeric_limits<double>::infinity() ought to work in any context.

    Even if you're using an older version, this will still work anywhere that you don't require a compile time constant. It's not clear from your question if your use really needs a compile time constant or not; just being in a header doesn't necessarily require it.

    If you are using an older version, and you really do need a compile time constant, the macro INFINITY in cmath should work for you. It's actually the float value for infinity, but it can be converted to a double.

    0 讨论(0)
  • 2020-12-31 05:57

    Maybe in your C++ environment you have float.h, see http://www.gamedev.net/topic/392211-max-value-for-double-/ (DBL_MAX)

    0 讨论(0)
提交回复
热议问题