Issue
According to the manual, OCaml has 4 kinds of integer literals:
integer-literal ::= [-] (0…9) { 0…9 ∣ _ }
∣ [-] (0x ∣ 0X) (0…9 ∣ A…F ∣ a…f) { 0…9 ∣ A…F ∣ a…f ∣ _ }
∣ [-] (0o ∣ 0O) (0…7) { 0…7 ∣ _ }
∣ [-] (0b ∣ 0B) (0…1) { 0…1 ∣ _ }
int32-literal ::= integer-literal l
int64-literal ::= integer-literal L
nativeint-literal ::= integer-literal n
Inspecting the types of these literals (using utop) gives the following results:
123 : int
123l : int32
123L : int64
123n : nativeint
The meaning of int32
and int64
is obvious enough, but what is the difference between int
and nativeint
, and how do they relate to the explicitly sized 32/64 bit ints?
Solution
This is explained in various sections of the manual and standard library documentation:
Integer values are integer numbers from −230 to 230−1, that is −1073741824 to 1073741823. The implementation may support a wider range of integer values: on 64-bit platforms, the current implementation supports integers ranging from −262 to 262−1.
And the manual section on interfacing with C also gives some useful context to this:
An object of type value is either:
- an unboxed integer;
- or a pointer to a block inside the heap, allocated through one of the
caml_alloc_*
functions described in section 20.4.4.
Integer values encode 63-bit signed integers (31-bit on 32-bit architectures). They are unboxed (unallocated).
It is because one bit is used to distinguish between unboxed integers and pointers that regular int
s are either 31 or 63-bit values.
32-bit integers.
This module provides operations on the type
int32
of signed 32-bit integers. Unlike the built-inint
type, the typeint32
is guaranteed to be exactly 32-bit wide on all platforms. All arithmetic operations overint32
are taken modulo 232.Performance notice: values of type
int32
occupy more memory space than values of typeint
, and arithmetic operations onint32
are generally slower than those onint
. Useint32
only when the application requires exact 32-bit arithmetic.
64-bit integers.
This module provides operations on the type
int64
of signed 64-bit integers. Unlike the built-inint
type, the typeint64
is guaranteed to be exactly 64-bit wide on all platforms. All arithmetic operations over int64 are taken modulo 264Performance notice: values of type
int64
occupy more memory space than values of typeint
, and arithmetic operations onint64
are generally slower than those onint
. Useint64
only when the application requires exact 64-bit arithmetic.
Processor-native integers.
This module provides operations on the type
nativeint
of signed 32-bit integers (on 32-bit platforms) or signed 64-bit integers (on 64-bit platforms). This integer type has exactly the same width as that of a pointer type in the C compiler. All arithmetic operations overnativeint
are taken modulo 232 or 264 depending on the word size of the architecture.Performance notice: values of type
nativeint
occupy more memory space than values of typeint
, and arithmetic operations onnativeint
are generally slower than those on int. Usenativeint
only when the application requires the extra bit of precision over theint
type.
Answered By - glennsl Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.