Question

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
Was it helpful?

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.

OTHER TIPS

Be aware that OCaml uses the high order bit of an integer for it's own purposes.

So an int is always one bit less than the machine native int. Modules Int32 and Int64 are for applications which need the corresponding full integer length, esp. interfaces to C libraries and functions.

Toplevel test:

# max_int;;        (* on 64 bit system *)
- : int = 4611686018427387903
# Int64.max_int;;  (* the lower case l is uppercase on my system *)
- : int64 = 9223372036854775807L
# let n = 9223372036854775807L;; (* correct type inference *)
val n : int64 = 9223372036854775807L

Hope this helps understanding it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top