Fandom

Pico-8 Wiki

Math

162pages on
this wiki
Add New Page
Comments0 Share

Pico-8 can do mathematical operations on numbers, including arithmetic and trigonometric functions. All operations use Pico-8's number value type, a 16-bit fixed point type with a range of -32768.0 to 32676.0.

To assist with common game graphics tasks, Pico-8's trigonometric functions use an input range of 0.0 to 1.0, which is equivalent to a range of 0 to 2 PI radians, or 0 to 360 degrees.

Increasing angle values go clockwise around the circle. Another way of thinking about this is that the result of sin() is inverted to match the orientation of the y-axis, with increasing y values going down the display. See sin() and atan2() for further explanation.

For more information:

Approximating log Edit

Pico-8 does not have a built-in logarithm function. You can create one based on a lookup table, adjusted for your desired level of accuracy. Here's a simple log-base-10 function:

log10_table = {
 0, 0.3, 0.475,
 0.6, 0.7, 0.775,
 0.8375, 0.9, 0.95, 1
}

function log10(n)
 if (n < 1) return nil
 local t = 0
 while n > 10 do
  n /= 10
  t += 1
 end
 return log10_table[flr(n)] + t
end

for x=5,500,10 do
 printh('log10('..x..') = '..log10(x))
end

This function takes advantage of the facts that log10(a * b) = log10(a) + log10(b), and log10(10^t) = t. It divides the value by 10 repeatedly until the value is between 1 and 10, keeping track of the number of divisions (t). It uses a lookup table to approximate the log of the value in this range, then adds in the log of the 10's.

You can change the base by replacing "10" with the new base wherever you see it, and changing the table with new approximations for the integers from 1 to 10.

You can get more accuracy by extending the table to 100 entries (log(1) to log(100)), and changing the while loop to while n > 100.

Keep in mind that the Pico-8 number type has a positive max of 32767, so that's your effective domain. log10(32767) is 4.51543..., so that's your range. You may want more digits of accuracy if you're doing a lot of work within that domain.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.