Elixir v1.5.2 Integer View Source

Functions for working with integers.

Link to this section Summary

Functions

Returns the ordered digits for the given integer

Performs a floored integer division

Returns the greatest common divisor of the two given integers

Determines if an integer is even

Determines if integer is odd

Computes the modulo remainder of an integer division

Parses a text representation of an integer

Returns a charlist which corresponds to the text representation of the given integer

Returns a charlist which corresponds to the text representation of integer in the given base

Returns a binary which corresponds to the text representation of integer

Returns a binary which corresponds to the text representation of integer in the given base

Returns the integer represented by the ordered digits

Link to this section Functions

Link to this function digits(integer, base \\ 10) View Source
digits(integer(), pos_integer()) :: [integer(), ...]

Returns the ordered digits for the given integer.

An optional base value may be provided representing the radix for the returned digits. This one must be an integer >= 2.

Examples

iex> Integer.digits(123)
[1, 2, 3]

iex> Integer.digits(170, 2)
[1, 0, 1, 0, 1, 0, 1, 0]

iex> Integer.digits(-170, 2)
[-1, 0, -1, 0, -1, 0, -1, 0]
Link to this function floor_div(dividend, divisor) View Source
floor_div(integer(), neg_integer() | pos_integer()) :: integer()

Performs a floored integer division.

Raises an ArithmeticError exception if one of the arguments is not an integer, or when the divisor is 0.

Integer.floor_div/2 performs floored integer division. This means that the result is always rounded towards negative infinity.

If you want to perform truncated integer division (rounding towards zero), use Kernel.div/2 instead.

Examples

iex> Integer.floor_div(5, 2)
2
iex> Integer.floor_div(6, -4)
-2
iex> Integer.floor_div(-99, 2)
-50
Link to this function gcd(integer1, integer2) View Source
gcd(integer(), integer()) :: pos_integer()
gcd(0, 0) :: 0

Returns the greatest common divisor of the two given integers.

The greatest common divisor (GCD) of integer1 and integer2 is the largest positive integer that divides both integer1 and integer2 without leaving a remainder.

By convention, gcd(0, 0) returns 0.

Examples

iex> Integer.gcd(2, 3)
1

iex> Integer.gcd(8, 12)
4

iex> Integer.gcd(8, -12)
4

iex> Integer.gcd(10, 0)
10

iex> Integer.gcd(7, 7)
7

iex> Integer.gcd(0, 0)
0
Link to this macro is_even(integer) View Source (macro)

Determines if an integer is even.

Returns true if the given integer is an even number, otherwise it returns false.

Allowed in guard clauses.

Examples

iex> Integer.is_even(10)
true

iex> Integer.is_even(5)
false

iex> Integer.is_even(-10)
true

iex> Integer.is_even(0)
true
Link to this macro is_odd(integer) View Source (macro)

Determines if integer is odd.

Returns true if the given integer is an odd number, otherwise it returns false.

Allowed in guard clauses.

Examples

iex> Integer.is_odd(5)
true

iex> Integer.is_odd(6)
false

iex> Integer.is_odd(-5)
true

iex> Integer.is_odd(0)
false

Computes the modulo remainder of an integer division.

Integer.mod/2 uses floored division, which means that the result will always have the sign of the divisor.

Raises an ArithmeticError exception if one of the arguments is not an integer, or when the divisor is 0.

Examples

iex> Integer.mod(5, 2)
1
iex> Integer.mod(6, -4)
-2
Link to this function parse(binary, base \\ 10) View Source
parse(binary(), 2..36) :: {integer(), binary()} | :error

Parses a text representation of an integer.

An optional base to the corresponding integer can be provided. If base is not given, 10 will be used.

If successful, returns a tuple in the form of {integer, remainder_of_binary}. Otherwise :error.

Raises an error if base is less than 2 or more than 36.

If you want to convert a string-formatted integer directly to a integer, String.to_integer/1 or String.to_integer/2 can be used instead.

Examples

iex> Integer.parse("34")
{34, ""}

iex> Integer.parse("34.5")
{34, ".5"}

iex> Integer.parse("three")
:error

iex> Integer.parse("34", 10)
{34, ""}

iex> Integer.parse("f4", 16)
{244, ""}

iex> Integer.parse("Awww++", 36)
{509216, "++"}

iex> Integer.parse("fab", 10)
:error

iex> Integer.parse("a2", 38)
** (ArgumentError) invalid base 38
Link to this function to_charlist(integer) View Source
to_charlist(integer()) :: charlist()

Returns a charlist which corresponds to the text representation of the given integer.

Inlined by the compiler.

Examples

iex> Integer.to_charlist(123)
'123'

iex> Integer.to_charlist(+456)
'456'

iex> Integer.to_charlist(-789)
'-789'

iex> Integer.to_charlist(0123)
'123'
Link to this function to_charlist(integer, base) View Source
to_charlist(integer(), 2..36) :: charlist()

Returns a charlist which corresponds to the text representation of integer in the given base.

base can be an integer between 2 and 36.

Inlined by the compiler.

Examples

iex> Integer.to_charlist(100, 16)
'64'

iex> Integer.to_charlist(-100, 16)
'-64'

iex> Integer.to_charlist(882681651, 36)
'ELIXIR'
Link to this function to_string(integer) View Source
to_string(integer()) :: String.t()

Returns a binary which corresponds to the text representation of integer.

Inlined by the compiler.

Examples

iex> Integer.to_string(123)
"123"

iex> Integer.to_string(+456)
"456"

iex> Integer.to_string(-789)
"-789"

iex> Integer.to_string(0123)
"123"
Link to this function to_string(integer, base) View Source
to_string(integer(), 2..36) :: String.t()

Returns a binary which corresponds to the text representation of integer in the given base.

base can be an integer between 2 and 36.

Inlined by the compiler.

Examples

iex> Integer.to_string(100, 16)
"64"

iex> Integer.to_string(-100, 16)
"-64"

iex> Integer.to_string(882681651, 36)
"ELIXIR"
Link to this function undigits(digits, base \\ 10) View Source
undigits([integer()], integer()) :: integer()

Returns the integer represented by the ordered digits.

An optional base value may be provided representing the radix for the digits. This one can be an integer >= 2.

Examples

iex> Integer.undigits([1, 2, 3])
123

iex> Integer.undigits([1, 4], 16)
20

iex> Integer.undigits([])
0