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

Here :point_right: Infinity in Elixir/Erlang?

7 Likes

Might be a problem if you need negative infinity though :slight_smile:

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. :slight_smile:

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 :slight_smile: 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")
  receive do
    :done -> :ok 
    after timeout -> :timeout   
  end
end

Works for integers and :infinity

I understand an atom for infinity makes sense for timeouts, just that the original question asked in this post seems to be asking about mathematical comparisons. Still wasnt sure if the poster needs to check value x is less than infinity or just within range y, or needs infinity as a kind of wait forever. Do you get what I mean, the context is a little vague. Just was interested to know why he needed that check, and what does it solve.

Understanding these ways to compare are super helpful though

If you want to get mathematical about it the space is a almost-well ordered adjoint space (https://en.m.wikipedia.org/wiki/Axiom_of_adjunction) which happens to contain N and R (it’s not perfectly well ordered because for example (1 !== 1.0) && !( 1 < 1.0 || 1 > 1.0))