Bijou64.Zigzag (Bijou64 v0.1.2)

Copy Markdown View Source

Zigzag mapping between signed and unsigned integers.

Interleaves positive and negative numbers so that small-magnitude values map to small unsigned values: 0 → 0, -1 → 1, 1 → 2, -2 → 3, 2 → 4, …

Works on arbitrary-precision Elixir integers.

Summary

Functions

Maps a non-negative integer back to a signed integer.

Maps a signed integer to a non-negative integer.

Functions

from_unsigned(n)

@spec from_unsigned(non_neg_integer()) :: integer()

Maps a non-negative integer back to a signed integer.

iex> Bijou64.Zigzag.from_unsigned(0)
0

iex> Bijou64.Zigzag.from_unsigned(1)
-1

iex> Bijou64.Zigzag.from_unsigned(2)
1

iex> Bijou64.Zigzag.from_unsigned(3)
-2

to_unsigned(n)

@spec to_unsigned(integer()) :: non_neg_integer()

Maps a signed integer to a non-negative integer.

iex> Bijou64.Zigzag.to_unsigned(0)
0

iex> Bijou64.Zigzag.to_unsigned(-1)
1

iex> Bijou64.Zigzag.to_unsigned(1)
2

iex> Bijou64.Zigzag.to_unsigned(-2)
3