package tablecloth-native
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=0c71dd4035d6fa4978baabc3c932dba3
sha512=44ba09f1ff43e61403703cc244e91e0f8062bd9da998f031430d701a4de148b02878f7f881303f6ded261176f21926ab5ba00a313510ed8e2d2f252b3fd00054
doc/tablecloth-native/Tablecloth/Int/index.html
Module Tablecloth.Int
Fixed precision integers
The platform-dependant signed integer type.
An int is a whole number.
ints are subject to overflow, meaning that Int.maximumValue + 1 = Int.minimumValue.
If you need to work with integers larger than maximumValue (or smaller than minimumValue you can use the Integer module.
Valid syntax for ints includes:
0
42
9000
1_000_000
1_000_000
0xFF (* 255 in hexadecimal *)
0x000A (* 10 in hexadecimal *)Note: The number of bits used for an int is platform dependent.
When targeting Bucklescript https://bucklescript.github.io/docs/en/common-data-types.html#int.
When targeting native OCaml uses 31-bits on 32-bit platforms and 63-bits on 64-bit platforms which means that int math is well-defined in the range -2 ** 30 to 2 ** 30 - 1 for 32bit platforms -2 ** 62 to 2 ** 62 - 1 for 64bit platforms.
Outside of that range, the behavior is determined by the compilation target.
You can read about the reasons for OCaml's unusual integer sizes here.
Historical Note: The name int comes from the term integer). It appears that the int abbreviation was introduced in the programming language ALGOL 68.
Today, almost all programming languages use this abbreviation.
Constants
val zero : tThe literal 0 as a named value
val one : tThe literal 1 as a named value
val maximumValue : tThe maximum representable int on the current platform
val maximum_value : tval minimumValue : tThe minimum representable int on the current platform
val minimum_value : tCreate
val fromString : string -> t optionAttempt to parse a string into a int.
Examples
Int.fromString "0" = Some 0.Int.fromString "42" = Some 42.Int.fromString "-3" = Some (-3)Int.fromString "123_456" = Some 123_456Int.fromString "0xFF" = Some 255Int.fromString "0x00A" = Some 10Int.fromString "Infinity" = NoneInt.fromString "NaN" = Noneval from_string : string -> t optionOperators
Note You do not need to open the Int module to use the (+), (-), (*), (**), (mod) or (/) operators, these are available as soon as you open Tablecloth
Subtract numbers
Int.subtract 4 3 = 1Alternatively the operator can be used:
4 - 3 = 1See Int.subtract
Multiply ints like
Int.multiply 2 7 = 14Alternatively the operator can be used:
(2 * 7) = 14See Int.multiply
Integer division
Notice that the remainder is discarded.
Exceptions
Throws Division_by_zero when the divisor is 0.
Examples
Int.divide 3 ~by:2 = 127 / 5 = 5See Int.divide
Floating point division
Examples
Int.(3 /. 2) = 1.5Int.(27 /. 5) = 5.25Int.(8 /. 4) = 2.0Exponentiation, takes the base first, then the exponent.
Examples
Int.power ~base:7 ~exponent:3 = 343Alternatively the ** operator can be used:
7 ** 3 = 343Flips the 'sign' of an integer so that positive integers become negative and negative integers become positive. Zero stays as it is.
Examples
Int.negate 8 = (-8)Int.negate (-7) = 7Int.negate 0 = 0Alternatively the ~- operator can be used:
~-(7) = (-7)See Int.negate
Get the absolute value of a number.
Examples
Int.absolute 8 = 8Int.absolute (-7) = 7Int.absolute 0 = 0Perform modular arithmetic.
If you intend to use modulo to detect even and odd numbers consider using Int.isEven or Int.isOdd.
The modulo function works in the typical mathematical way when you run into negative numbers
Use Int.remainder for a different treatment of negative numbers.
Examples
Int.modulo ~by:3 (-4) = 1Int.modulo ~by:3 (-3 )= 0Int.modulo ~by:3 (-2) = 2Int.modulo ~by:3 (-1) = 1Int.modulo ~by:3 0 = 0Int.modulo ~by:3 1 = 1Int.modulo ~by:3 2 = 2Int.modulo ~by:3 3 = 0Int.modulo ~by:3 4 = 1See Int.modulo
Get the remainder after division. Here are bunch of examples of dividing by four:
Use Int.modulo for a different treatment of negative numbers.
Examples
List.map
~f:(Int.remainder ~by:4)
[(-5); (-4); (-3); (-2); (-1); 0; 1; 2; 3; 4; 5] =
[(-1); 0; (-3); (-2); (-1); 0; 1; 2; 3; 0; 1]Returns the larger of two ints
Examples
Int.maximum 7 9 = 9Int.maximum (-4) (-1) = (-1)Returns the smaller of two ints
Examples
Int.minimum 7 9 = 7Int.minimum (-4) (-1) = (-4)Query
val isEven : t -> boolCheck if an int is even
Examples
Int.isEven 8 = trueInt.isEven 7 = falseInt.isEven 0 = trueval is_even : t -> boolval isOdd : t -> boolCheck if an int is odd
Examples
Int.isOdd 7 = trueInt.isOdd 8 = falseInt.isOdd 0 = falseval is_odd : t -> boolClamps n within the inclusive lower and upper bounds.
Exceptions
Throws an Invalid_argument exception if lower > upper
Examples
Int.clamp ~lower:0 ~upper:8 5 = 5Int.clamp ~lower:0 ~upper:8 9 = 8Int.clamp ~lower:(-10) ~upper:(-5) 5 = (-5)Checks if n is between lower and up to, but not including, upper.
Exceptions
Throws an Invalid_argument exception if lower > upper
Examples
Int.inRange ~lower:2 ~upper:4 3 = trueInt.inRange ~lower:5 ~upper:8 4 = falseInt.inRange ~lower:(-6) ~upper:(-2) (-3) = trueConvert
val toFloat : t -> floatval to_float : t -> floatval toString : t -> stringConvert an int into a string representation.
Guarantees that
Int.(fromString (toString n)) = Some n Examples
Int.toString 3 = "3"Int.toString (-3) = "-3"Int.to_sString 0 = "0"val to_string : t -> stringCompare
The unique identity for Comparator