-2

Expand your thoughts upon this:

#define INFINITY ((1 << (8*sizeof (int) - 6)) - 4)

Is expanded?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 3
    In what context? Why do you need a value representing infinity? And why `((1 << (8*sizeof (int) - 6)) - 4)`? Why not use `float` or `double` since they can represent positive/negative infinity? – In silico May 15 '11 at 04:48
  • 1
    I would usually say something like "a loop that will eventually burn your processor"... So yeah, it depends on the context. – Andy Ibanez May 15 '11 at 04:50
  • if (distances[i*MAX_ZONES+j]==INFINITY) – Question Marx May 15 '11 at 04:52
  • And in what context is a distance equal to infinity? – Jesse Emond May 15 '11 at 04:53
  • 1
    The IEEE 754 representation for 32-bit `float` "infinity" as an integer is `0x7F800000`, which that equation does not yield. – Mike DeSimone May 15 '11 at 04:54
  • It yields `0x03FFFFFC`, so sign is `+`, exponent is `0x07`, and mantissa is `0x7FFFFC`, which is `0x1.FFFFF8 * 2 ^ -120` which Python tells me is `1.5046324103201211e-36`. Not a value of particular interest. (Ref: [IEEE 754-1985](http://en.wikipedia.org/wiki/IEEE_754-1985).) – Mike DeSimone May 17 '11 at 04:05

2 Answers2

5

Why not

numeric_limits<float>::infinity() 

or

numeric_limits<double>::infinity()

?

Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
2

Use numeric_limits from <limits> header file, as

numeric_limits<float>::infinity() 

See this : http://www.cplusplus.com/reference/std/limits/numeric_limits/

Nawaz
  • 353,942
  • 115
  • 666
  • 851