1

In languages like python we have functions like math.inf or float("inf") to represent an infinite value that can still be used as a number.

Does Gleam contain a function or operation that performs a similar task?

I looked through the gleam/int and the rest of stdlib and couldn't find anything that matched what I needed to do.

2
  • 2
    I don't know the first thing about Gleam, but I found this on tour.gleam.run: "When running on the Erlang virtual machine ints have no maximum and minimum size. When running on JavaScript runtimes ints are represented using JavaScript's 64 bit floating point numbers" Commented Sep 13, 2024 at 15:16
  • Thanks, I guess I will limit my program to the 64 bit float that JS uses. Commented Sep 16, 2024 at 19:14

1 Answer 1

0

Be advised that I have been learning Gleam for fun all of maybe three afternoons, but I do have a way you can kind of do this: You can define your own type that is an Int that can also be Inf (or MinInf). I wouldn't recommend it much probably, but some algorithms do look a lot nicer if you can just use an Int value that you can be certain will always be larger than any normal Int, so this does that. Only problem is, you're then using your own type instead of the built-in Int, so the normal operators don't work anymore, and you can't overload them either. (I'm sure there's a reason for that, but I'd love to be able to myself at the moment.) Anyway here's the code:

import gleam/io

pub type IntOrInf{
  Integer(Int)
  Inf
  MinInf
}

pub fn is_greater_or_equal(a: IntOrInf, compared_to b: IntOrInf) -> Bool {
  case a, b {
    Integer(a), Integer(b) -> a >= b
    Integer(_), Inf        -> False
    Inf,        Integer(_) -> True
    Integer(_), MinInf     -> True
    MinInf,     Integer(_) -> False
    Inf,        MinInf     -> True
    MinInf,     Inf        -> False
    Inf,        Inf        -> True
    MinInf,     MinInf     -> True
  }
}

pub fn is_greater(a: IntOrInf, than b: IntOrInf) -> Bool {
  is_greater_or_equal(a, b) && a != b
}

pub fn is_less(a: IntOrInf, than b: IntOrInf) -> Bool {
  !is_greater_or_equal(a, b)
}

pub fn is_less_or_equal(a: IntOrInf, compared_to b: IntOrInf) -> Bool {
  is_less(a, b) || a == b
}

pub fn main() {
  io.debug(is_less(Integer(5), than: Inf))
  io.debug(is_less(Integer(5), than: MinInf))
  io.debug(is_greater(Inf, MinInf))
  io.debug(is_greater(Inf, than: Inf))
  io.debug(is_greater_or_equal(Inf, compared_to: Inf))
  io.debug(Inf == Inf)
  // et cetera...
}

I haven't tested all of the cases nor put much though into it, so it might not be 100% correct, but you get the idea, I hope.

Edit: Maybe it'd be better to define a function that works like int.compare, which returns an object of type Order, which can have the values (variants?) Eq, Gt or Lt. That way you can use the IntOrInf in the standard list.sort.

Edit 2: The comparison function can be written more compactly like below. Note that now the order of the cases is important, because we want to get True when comparing Inf to Inf or MinInf to MinInf.

pub fn is_greater_or_equal(a: IntOrInf, compared_to b: IntOrInf) -> Bool {
  case a, b {
    Integer(a), Integer(b) -> a >= b
    Inf,        _          -> True
    _,          Inf        -> False
    _,          MinInf     -> True
    MinInf,     _          -> False
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.