Open AnimeshSinha1309 opened 4 years ago
Personally, I have always used 1e18
as infinity for both floats and for long longs, and it has never failed me.
Some other times, when everything is unclear, I have resorted to assigning -1
to the initial value and then checking for it when reassigning values.
What do you think?
1e18
certainly works with long long, but is it not too small in floats, specially cause we usually need it when comparing to infinite slopes, and it's very likely that there will be a slope that is not infinite and still greater than 1e18
in magnitude. Plus we should avoid using magic numbers in code. LLONG_MAX, and FLOAT_MAX, and other constants might help us out. If we are too close to overflow, we can define our infinities in terms of those constants, how does that sound?
I am fine with any constant tbh. I used 1e18 only because it is simpler to write.
The only problem there is occurs when you start adding an INFINITY to itself. For example, in Bellman Ford, you might end up adding inifinity several times over and get an overflow.
We have a lot of infinities floating around in the code, some for integers, and some for floats. All of them are inconsistent and use different constants from the C library. We need to decide on what to use and convert everything to it.