OCaml的最大Int值

时间:2014-02-11 21:31:14

标签: int ocaml primitive

我刚接受采访,我需要将此值用于我想出的算法。在采访之后,我很好奇是否真的有办法获得最大的Int值。

我知道Int32.max_int和Int64.max_int。

然而,当我将Int32.max_int的值设置为int时,它超过了Int所具有的最大值。

# 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

2 个答案:

答案 0 :(得分:11)

$ ocaml
        OCaml version 4.01.0

# max_int;;
- : int = 4611686018427387903
# let a : int = max_int;;
val a : int = 4611686018427387903

<强>更新

对于它的价值,如果你使用的是32位系统,Int32.max_int仍然不适合int,即使你纠正了错误,认为最后的L(l)是一个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
# 

所以我说L不是问题。

答案 1 :(得分:2)

请注意,OCaml为了它自己的目的使用整数的高位。

因此int总是比机器native int少一点。模块Int32和Int64适用于需要相应的完整整数长度的应用程序,尤其是。 C库和函数的接口。

Toplevel测试:

# 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

希望这有助于理解它。