// //------------------ DOK FILE -------------------------------------------------- +Header intLib =pkg bios =title Integers =short conversions, random, basic operations =desc // // //------------------ PROTO ----------------------------------------------------- Int +Proto Int =type Int =mode type =pkg bios =impl native =link =desc This is the type of integers. Minimacy integers are signed 64-bits numbers, from about -2.3e18 to 2.3e18. As any other Minimacy type, an integer may be nil, which is considered as zero for arithmetic operations or comparisons other than equality. In the Minimacy syntax, an integer may be written in decimal or in hexadecimal. > 123 >-> Int: 123 (0x7B) > 0d123 >-> Int: 123 (0x7B) > 0x123 >-> Int: 291 (0x123) // // //------------------ PROTO ----------------------------------------------------- intRand +Proto intRand =type fun -> Int =mode function =pkg bios =impl native =link =desc This function returns a positive 32 bits integer, based on a pseudo random algorithm with a global seed value. At each request of this function: seed = (seed * 0x5DEECE66D + 0xB) mod 2**48 return seed / 2**16 The seed is initialized to zero. It can be changed with the [[intRandSetSeed]] function. // +arg result =type Int =desc A 32 bits pseudo-random integer // // //------------------ PROTO ----------------------------------------------------- intShortFormat +Proto intShortFormat =type fun Int Int -> Str =mode function =pkg bios =impl mcy =link =desc This function computes a string to represent an approximated value, expressed in kilo, mega, giga, tera. > intShortFormat (1000, 65123) -> Str: "65k" > intShortFormat (1024, 65123) >-> Str: "63k" > intShortFormat (1000, 2343242432432) >-> Str: "2T" // +arg div =type Int =desc An integer, usually 1000 or 1024 // +arg v =type Int =desc An integer to display // +arg result =type Str =desc The string to display // // //------------------ PROTO ----------------------------------------------------- intWithDelimiter +Proto intWithDelimiter =type fun Str Int -> Str =mode function =pkg bios =impl mcy =link =desc This function returns the representation of value in decimal with a separator every 3 digits. intWithDelimiter (" ", 12345678) >-> Str: "12 345 678" > intWithDelimiter (",", 12345678) >-> Str: "12,345,678" // +arg separator =type Str =desc A string used as a separator // +arg value =type Int =desc An integer // +arg result =type Str =desc The resulting string // // //------------------ PROTO ----------------------------------------------------- strFromInt +Proto strFromInt =type fun Int -> Str =mode function =pkg bios =impl native =link =desc This function returns the representation of value in decimal. strFromInt (12345678) >-> Str: "12345678" // +arg value =type Int =desc An integer // +arg result =type Str =desc The resulting string // // //------------------ PROTO ----------------------------------------------------- intPad +Proto intPad =type fun Int Int -> Str =mode function =pkg bios =impl mcy =link =desc This function returns the representation of the positive value in decimal, with at least N digits. The padding is made of zeros. > intPad (3, 12) >-> Str: "012" > intPad (3, 1234) >-> Str: "1234" // +arg N =type Int =desc A number of digits // +arg value =type Int =desc A positive integer // +arg result =type Str =desc The resulting string // // //------------------ PROTO ----------------------------------------------------- floatFromInt +Proto floatFromInt =type fun Int -> Float =mode function =pkg bios =impl native =link =desc This function converts an integer into a float. See also [[intFromFloat]]. > floatFromInt (123) >-> Float: 123 // +arg value =type Int =desc An integer // +arg result =type Float =desc The resulting float // // //------------------ PROTO ----------------------------------------------------- hexFromInt +Proto hexFromInt =type fun Int -> Str =mode function =pkg bios =impl native =link =desc This function returns the hexadecimal representation of the integers. > hexFromInt (1234) >-> Str: "4D2" > hexFromInt (-1234) >-> Str: "FFFFFFFFFFFFFB2E" // +arg value =type Int =desc An integer // +arg result =type Str =desc The resulting hexadecimal string. // // //------------------ PROTO ----------------------------------------------------- abs +Proto abs =type fun Int -> Int =mode function =pkg bios =impl native =link =desc This function returns the absolute value. > abs (-123) >-> Int: 123 (0x7B) // +arg value =type Int =desc An integer // +arg result =type Int =desc The absolute value // // //------------------ PROTO ----------------------------------------------------- bitTest +Proto bitTest =type fun Int Int -> Bool =mode function =pkg bios =impl native =link =desc This function returns true only if a&b is non null. // +arg a =type Int =desc An integer // +arg b =type Int =desc An integer // +arg result =type Bool =desc true if a and b have at least one bit in common // // //------------------ PROTO ----------------------------------------------------- max +Proto max =type fun Int Int -> Int =mode function =pkg bios =impl native =link =desc This function returns the greater of the two values a and b. // +arg a =type Int =desc An integer // +arg b =type Int =desc An integer // +arg result =type Int =desc The greater value // // //------------------ PROTO ----------------------------------------------------- min +Proto min =type fun Int Int -> Int =mode function =pkg bios =impl native =link =desc This function returns the lower of the two values a and b. // +arg a =type Int =desc An integer // +arg b =type Int =desc An integer // +arg result =type Int =desc The lower value // // //------------------ PROTO ----------------------------------------------------- range +Proto range =type fun Int Int Int -> Bool =mode function =pkg bios =impl mcy =link =desc This function returns true if the value is min or higher, and max or lower. > range (10, 0, 20) >-> Bool: true > range (10, 0, 10) >-> Bool: true > range (10, 0, 9) >-> Bool: false // +arg value =type Int =desc An integer // +arg min =type Int =desc An integer // +arg max =type Int =desc An integer // +arg result =type Bool =desc True is value is between min and max // // //------------------ PROTO ----------------------------------------------------- signExtend +Proto signExtend =type fun Int Int -> Int =mode Function =pkg bios =impl native =link =desc This function generalizes the sign extension to any bit length. For example with 6 bits: > signExtend (0x1f, 6) >-> Int: 31 (0x1f) > signExtend (0x20, 6) >-> Int: -32 (0xffffffffffffffe0) In this case, the function ignore everything above the first 6 bits, and extends the sign based on bit 5. // +arg input =type Int =desc An integer // +arg nb =type Int =desc A number of bits // +arg result =type Int =desc The resulting signed integer // // //------------------ PROTO ----------------------------------------------------- signExtend16 +Proto signExtend16 =type fun Int -> Int =mode Function =pkg bios =impl native =link =desc This function considers the input value as a 16 bits integer, and extends its sign. > signExtend16 (0x7fff) >-> Int: 32767 (0x7fff) > signExtend16 (0x8000) >-> Int: -32768 (0xffffffffffff8000) // +arg input =type Int =desc An integer // +arg result =type Int =desc The resulting signed integer // // //------------------ PROTO ----------------------------------------------------- signExtend24 +Proto signExtend24 =type fun Int -> Int =mode Function =pkg bios =impl native =link =desc This function considers the input value as a 24 bits integer, and extends its sign. > signExtend24 (0x7fffff) >-> Int: 8388607 (0x7fffff) > signExtend24 (0x800000) >-> Int: -8388608 (0xffffffffff800000) // +arg input =type Int =desc An integer // +arg result =type Int =desc The resulting signed integer // // //------------------ PROTO ----------------------------------------------------- signExtend32 +Proto signExtend32 =type fun Int -> Int =mode Function =pkg bios =impl native =link =desc This function considers the input value as a 32 bits integer, and extends its sign. > signExtend32 (0x7fffffff) >-> Int: 2147483647 (0x7fffffff) > signExtend32 (0x80000000) >-> Int: -2147483648 (0xffffffff80000000) // +arg input =type Int =desc An integer // +arg result =type Int =desc The resulting signed integer // // //------------------ PROTO ----------------------------------------------------- signExtend8 +Proto signExtend8 =type fun Int -> Int =mode Function =pkg bios =impl native =link =desc This function considers the input value as a 32 bits integer, and extends its sign. > signExtend8 (0x7f) >-> Int: 127 (0x7f) > signExtend8 (0x80) >-> Int: -128 (0xffffffffffffff80) // +arg input =type Int =desc An integer // +arg result =type Int =desc The resulting signed integer // // //------------------ PROTO ----------------------------------------------------- signBit +Proto signBit =type Int =mode constant =pkg bios =impl native =link =desc