Search code examples
algorithminfinity

How to represent -infinity in programming


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 small number.

When computing -infinity - 3, or -infinity + 5 it should also result -infinity.

I tried initializing it with INT_MIN, but when I compute INT_MIN - 1 I get the upper limit, so I can't make a test like: if(value < INT_MIN) var = INT_MIN;

So how can I do that?


Solution

  • You could define a number as -infinite and, when adding or substracting something from a number, you do first check if the variable was equal to that pseudo-number. If so you just leave it as it was.

    But you might find library functions giving you that functionality implemented, e.g Double in Java or std in c++.