Module pico8lib.number

Non-mathematical manipulation of numbers

Fields

const_number_minfrac Smallest representable positive fraction (1/32768)
const_number_maxint Maximum representable positive integer (32767)
const_number_maxnum Maximum representable positive number (32767 + 32767/32768)
const_number_minnum Minimum representable negative integer and number (-32768)

Random Functions

rand (x) Random value [0 to x-1] such as pixel or grid offsets
randi (x) Random value [0 to x] such as pixels in/on a rect
randp (x) Random value [1 to x-1] such as pixels in a rect
randpi (x) Random value [1 to x] such as lua list indices
randb (n, x) Random value [n to x-1]
randbi (n, x) Random value [n to x-1]
randfrac (n, x) Random value [n to x] for non-integer n and x

Miscellaneous

popcount_loop (n) Count the 1 bits in a number This version runs one loop per set bit, 26 tokens
popcount_magic (v) Count the 1 bits in a number This version runs in constant time, 42 tokens Constant time is 111% the one-set-bit time of the shorter loop function

Bit manipulation

rotr8 (n, b) Rotate an 8-bit integer n to the right by b bits
rotl8 (n, b) Rotate an 8-bit integer n to the left by b bits

Rounding

flrceil (x, d) Floor or Ceiling of a number
tozero (n) Round toward zero
toinf (n) Round toward infinity
round (n) Round to nearest integer, 0.5 rounds toward +Inf


Fields

const_number_minfrac
Smallest representable positive fraction (1/32768)
const_number_maxint
Maximum representable positive integer (32767)
const_number_maxnum
Maximum representable positive number (32767 + 32767/32768)
const_number_minnum
Minimum representable negative integer and number (-32768)

Random Functions

rand (x)
Random value [0 to x-1] such as pixel or grid offsets

Parameters:

  • x
randi (x)
Random value [0 to x] such as pixels in/on a rect

Parameters:

  • x
randp (x)
Random value [1 to x-1] such as pixels in a rect

Parameters:

  • x
randpi (x)
Random value [1 to x] such as lua list indices

Parameters:

  • x
randb (n, x)
Random value [n to x-1]

Parameters:

  • n
  • x
randbi (n, x)
Random value [n to x-1]

Parameters:

  • n
  • x (n to x]
randfrac (n, x)
Random value [n to x] for non-integer n and x

Parameters:

  • n
  • x

Miscellaneous

popcount_loop (n)
Count the 1 bits in a number This version runs one loop per set bit, 26 tokens

Parameters:

  • n algorithm from https //graphics.stanford.edu/~seander/bithacks.html
popcount_magic (v)
Count the 1 bits in a number This version runs in constant time, 42 tokens Constant time is 111% the one-set-bit time of the shorter loop function

Parameters:

  • v original implementation from https //www.lexaloffle.com/bbs/?pid=72850 [2020-02-06 16:25] felice: if i post code in a bbs post then it's free for all to use. credit is nice of course if it's significant. algorithm from https://support.amd.com/techdocs/25112.pdf or https://graphics.stanford.edu/~seander/bithacks.html

Bit manipulation

rotr8 (n, b)
Rotate an 8-bit integer n to the right by b bits

Parameters:

  • n
  • b
rotl8 (n, b)
Rotate an 8-bit integer n to the left by b bits

Parameters:

  • n
  • b

Rounding

flrceil (x, d)
Floor or Ceiling of a number

Parameters:

  • x number The number to floor or ceiling
  • d number 1 for floor, -1 for ceiling

Returns:

    number The floor'd or ceiling'd number
tozero (n)
Round toward zero

Parameters:

  • n
toinf (n)
Round toward infinity

Parameters:

  • n
round (n)
Round to nearest integer, 0.5 rounds toward +Inf

Parameters:

  • n
generated by LDoc 1.5.0 Last updated 2024-09-08 14:28:56