Search code examples
intocamlprimitive

Max Int Value for OCaml


I just came off an interview where I needed to use this value for the algorithm I came up with. After the interview I was curious if there was actually a way to get the max Int value.

I am aware of Int32.max_int and Int64.max_int.

However when I set Int32.max_int's value to an int it exceeded the max value an Int have have.

# Int32.max_int;;
- : int32 = 2147483647l
# let a: int = 21474836471;;
Characters 13-24:
  let a: int = 21474836471;;
               ^^^^^^^^^^^
Error: Integer literal exceeds the range of representable integers of type int

Solution

  • $ ocaml
            OCaml version 4.01.0
    
    # max_int;;
    - : int = 4611686018427387903
    # let a : int = max_int;;
    val a : int = 4611686018427387903
    

    Update

    For what it's worth, if you're on a 32-bit system, Int32.max_int still doesn't fit into an int, even if you correct the mistake of thinking that the L (l) at the end is a 1:

    # Int32.max_int;;
    - : int32 = 2147483647l
    # let i : int = 21474836471 (* Mistake *);;
    Characters 14-25:
      let i : int = 21474836471 (* Mistake *);;
                    ^^^^^^^^^^^
    Error: Integer literal exceeds the range of representable integers of type int
    # let i : int = 2147483647 (* No mistake *);;
    Characters 14-24:
      let i : int = 2147483647 (* No mistake *);;
                    ^^^^^^^^^^
    Error: Integer literal exceeds the range of representable integers of type int
    # 
    

    So I'd say the L wasn't the problem.