How to represent -infinity in programming

前端 未结 6 1767
野趣味
野趣味 2021-01-17 01:18

How can I represent -infinity in C++, Java, etc.?

In my exercise, I need to initialize a variable with -infinity to show that it\'s a very

6条回答
  •  既然无缘
    2021-01-17 01:48

    If there was the possibility that a number was not there, instead of picking a number from its domain to represent 'not there', I would pick a type with both every integer I care about, and a 'not there' state.

    A (deferred) C++1y proposal for optional is an example of that: an optional is either absent, or an integer. To access the integer, you first ask if it is there, and if it is you 'dereference' the optional to get it.

    Making infectious optionals: ones that, on almost any binary operation, infect the result if either value is absent, should be an easy extension of this idea.

提交回复
热议问题