# Is there a way to represent infinity in Erlang / Elixir?

I want to compare if a value x is greater than a another value y but also less than infinity ?

So that I can say x belongs to the interval [y, infinity)

I mean is there something in Elixir / Erlang like Python3.5+

``````import math
infinity = math.inf
infnity > 1
infinity > 100000000
infnity > x
``````

?

There is a simple hack for you.
`:infinity` is greater than any number here. Because check the order of comparison in elixir `number < atom < reference < function < port < pid < tuple < map < list < bitstring`

Atom in Elixir is greater than any Number

7 Likes

Might be a problem if you need negative infinity though

6 Likes

Yeah but you can special case it by negating the float value and then invert comparing it to positive `:infinity` to test for negative infinity.

1 Like

Erlang/Elixir does not have a built-in infinity value(nor does it have a Not-a-Number value), because its floating-point arithmetic is not fully IEEE754-compliant (it predates that standard).

Depending on what you are doing, maybe you might want to look at the decimal library to allow arbitrary-precision arithmetic, with support for (both positive and negative) infinity.

6 Likes

This is a silly question I suspect, butâ€¦ I have to ask it.
Iâ€™d assume that any finite number is great than -Infinity and less than +Infinity, then whatâ€™s the use for an entity representing it/them in code?

Mostly for generic code, like the usual `max` function or so.

1 Like

Thank you @OvermindDL1, Iâ€™m in the dark, but got intrigued I will dig a bitâ€¦

Ok, as @OvermindDL1 said this was for generic code.
One way I found to workaround it was to create an `infinity/1` and `minus_infinity/1` functions. Both of these take a number as input and return that value +/- a constant ( integer one ). That way whenever I wanted to compare `x > infinity` Iâ€™d write `x > infinity(x)`.

3 Likes

Setting timeouts to :infinity is the most common use case for it. In fact the builtin type â€śtimeoutâ€ť is â€śnon_neg_integer | :infinityâ€ť

I suspect raising on NaN/infinity also fits better in the â€śfail fastâ€ť philosophy of Erlang; itâ€™d rather crash early than proceed with bad data.

Technically IEEE754 has qNaN and nNaN where nNaN is â€śfail fastâ€ť but I donâ€™t think in practice anyone uses it.

1 Like

Can I ask why you need a value for infinity in the case stated, why check if x is less than infinity, surely if itâ€™s any numeric value it would be true. Your only interested in if x is greater than y and a valid number. As your not setting an upper range limit for y itâ€™s just a check if x > y or am i missing something here, that check could only return true if both were valid numbers .
I can understand use in a timeout as you essentially want an indefinite wait. But in a comparison itâ€™s pointless running a check that always returns true
I suppose in elixir you could just say x is greater than the head of y if itâ€™s an array list etc if you just want to know if x is greater that the first number in range y that is

In elixir >/< is not â€śgreater thanâ€ť/â€śless thanâ€ť, itâ€™s the sorting comparison operator, so it applies to every data. It will generate a deterministic response for any two terms no matter what. It might not be what you expect (as in the case for DateTime values) but it will be what you expect for numbers and :infinity.

2 Likes

Thanks for clarifying I was talking in terms of comparing numbers, a number is finite and therefore always less than infinity was my logic so checking if a numeric value is less than infinity is sort of pointless as it would always be true in all cases

:infinity is an atom. You can make a parameter number or :infinity, which makes comparisons useful.

For example:

``````def wait_for_done(timeout) do
if timeout > 500, do: IO.puts("it could be a while")