PICO-8 Wiki
(→‎Examples: wrong sign on the concise ceil example)
Tags: Visual edit apiedit
(→‎See also: added ceil function)
Tags: Visual edit apiedit
Line 37: Line 37:
 
A more concise "ceiling" function: <code>-flr(-num)</code>
 
A more concise "ceiling" function: <code>-flr(-num)</code>
 
<pre>
 
<pre>
  +
function ceil(num)
  +
return -flr(-num)
  +
end
  +
 
print(-flr(-(5.9))) -- 6
 
print(-flr(-(5.9))) -- 6
   

Revision as of 08:08, 15 January 2017

flr( num )
Returns the integer portion (the "floor") of a number.
num
The number.

For positive numbers, the flr() function returns the integer portion of a number, such as 5 for 5.8.

More generally, flr() returns the closest integer to the number that is less than the number. For negative numbers, the result is more negative.

Examples

print(flr(5.9))   -- 5

print(flr(-5.2))  -- -6

print(flr(7))     -- 7

print(flr(-7))    -- -7

Pico-8 does not have a corresponding "ceiling" function. Here is an implementation that uses flr():

function ceil(num)
  if (flr(num) == num) return num
  return flr(num) + 1
end

print(ceil(5.9))   -- 6

print(ceil(-5.2))  -- -5

print(ceil(7))     -- 7

print(ceil(-7))    -- -7

A more concise "ceiling" function: -flr(-num)

function ceil(num)
  return -flr(-num)
end

print(-flr(-(5.9)))    -- 6

print(-flr(-(-5.2)))    -- -5

print(-flr(-(7)))  -- 7

print(-flr(-(-7)))   -- -7

See also