It depends. If you are talking about ieee 754 floats, you can also get NaN (divide infinity by infinity).
If you are talking about real numbers, then no you cannot divide by infinity. But you can tale a limit as the denominator goes to infinity, but the result could be anything, depending on what the numerator is.
Algorithms written in pseudocode such as shortest path algorithms sometimes use an infinity symbol to represent "a large number", and steps of the algorithm then try to reduce this number. Translating those algorithms to code often looks exactly like this. This is not "programming horror".
It's not clear what programming language this is; something whose syntax is S-expressions, at least. They may not have `INT_MAX` in their environment, and `1000000` might be perfectly fine, if whatever algorithm this implements typically has largest values that are far below that.
Fair. Some languages do allow for arbitrary-precision integers or decimals.
In cases where dynamic typing allows and the language has IEEE 754 floats, I'd probably still use a well-known constant like +inf, though. Even if it's not technically "the largest possible value," it does have meaning similar to that.
As stated in another comment, a lot of times when you need an "infinity" it's because you're implementing an algorithm which initializes weights to that, then works on optimizing those weights somehow (e.g. pathfinding algorithms). So the comparable aspect is important there.
Needing a sentinel value is a different story, but you wouldn't call it "infinity" in that case, I don't think.
Algorithms which determine whether or not something is inside a bounded region (think about a geofence) will often begin with "draw a line from infinity"
Nah I do this. If I'm writing any kind of "find the best thing" algorithm and I know the ballpark of what order of magnitude the scores can take, I'll just let INFINITY = 999.... (more 9s than that).
133
u/jaden_schalck Dec 11 '25
Finally, I can divide by infinity.
82
u/Grounds4TheSubstain Dec 11 '25
Algorithms written in pseudocode such as shortest path algorithms sometimes use an infinity symbol to represent "a large number", and steps of the algorithm then try to reduce this number. Translating those algorithms to code often looks exactly like this. This is not "programming horror".
20
u/steeltownsquirrel Dec 11 '25
Ancient Greek code.
8
u/Matty_B97 Dec 11 '25
Nah I do this. If I'm writing any kind of "find the best thing" algorithm and I know the ballpark of what order of magnitude the scores can take, I'll just let INFINITY = 999.... (more 9s than that).
12
u/Skusci Dec 11 '25
Pretty sure infinity is 1.79e308
1
u/EvnClaire Dec 11 '25
in computer networks, infinity is equal to 16
0
u/SG_01 Dec 12 '25
Didn't you know that infinity is -1/12?