PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 17, 2022

[FIXED] What is the difference between int, nativeint, int64, and int32?

 October 17, 2022     integer, ocaml, types     No comments   

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 numbers:

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 ints are either 31 or 63-bit values.

Int32:

32-bit integers.

This module provides operations on the type int32 of signed 32-bit integers. Unlike the built-in int type, the type int32 is guaranteed to be exactly 32-bit wide on all platforms. All arithmetic operations over int32 are taken modulo 232.

Performance notice: values of type int32 occupy more memory space than values of type int, and arithmetic operations on int32 are generally slower than those on int. Use int32 only when the application requires exact 32-bit arithmetic.

Int64:

64-bit integers.

This module provides operations on the type int64 of signed 64-bit integers. Unlike the built-in int type, the type int64 is guaranteed to be exactly 64-bit wide on all platforms. All arithmetic operations over int64 are taken modulo 264

Performance notice: values of type int64 occupy more memory space than values of type int, and arithmetic operations on int64 are generally slower than those on int. Use int64 only when the application requires exact 64-bit arithmetic.

Nativeint:

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 over nativeint 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 type int, and arithmetic operations on nativeint are generally slower than those on int. Use nativeint only when the application requires the extra bit of precision over the int type.



Answered By - glennsl
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing