Module pico8lib.math

Mathematical operations, mostly on numbers

Local Functions

gcd_recursive (a, b) Return the greatest common divisor of two numbers This implementation uses recursion
gcd (a, b) Return the greatest common divisor of two numbers This implementation is iterative
lcm (a, b) Return the least common multiple of two numbers

Roots

nthroot (n, x) Calculate the nth root of x nthroot(3,8)==2 because 2^3==8 All versions start with a guess based on the built in ^ operator This version performs newton's method until a convergent result or short loop is found Very accurate
nthroot_short (n, x) Calculate the nth root of x This version performs newton's method exactly 4 times Relatively accurate
nthroot_fast (n, x) Calculate the nth root of x This version performs newton's method exactly once, only for positive x Least accurate, still much better than built in ^ operator alone

Distance

dist (x, y) Distance to a point Uses dist_naive for small values, dist_trig for large tokens: 46
dist_naive (x, y) Distance to a point Naive distance calculation Overflows for dist^2>=32768 tokens: 15 cycles: 9 + 28 = 37 (lua+sys)
dist_trig (x, y) Distance to a point Uses trigonometry Error in range (0,0)-(127,127): max 0.0017, avg 0.0004 tokens: 23 cycles: 21 + 0 = 21 (lua+sys)
dist_squared (x, y) Distance squared to a point

Trigonometry

asin_octant (d) Arithmetic approximation of arcsin Intended for 0<=d<=.7071 Exact at d==0, d==sin(1/16), d==sin(1/8) Max error .0021 at d==sin(3/32) in given range asin() is the inverse of sin(); a=asin(d) when d=sin(a)

Interpolation

interp_linear (a, b, f) Linear interpolation between two values (a,b,0) == a (a,b,1) == b (a,b,0.25) is 1/4 of the way from a to b
interp_linear_inverse (a, b, v) Inverse Linear Interpolation "How far along the way from a to b is v?" (a,b,a) == 0 (a,b,b) == 1 (a,b,(a+b)/2) == 0.5
interp_sine (a, b, f) Sine interpolation (a,b,0) == a (a,b,1) == b (a,b,0.5) == 70.71% of the way from a to b


Local Functions

gcd_recursive (a, b)
Return the greatest common divisor of two numbers This implementation uses recursion

Parameters:

  • a number The first number
  • b number The second number

Returns:

    number The greatest common divisor, or zero

See also:

gcd (a, b)
Return the greatest common divisor of two numbers This implementation is iterative

Parameters:

  • a number The first number
  • b number The second number

Returns:

    number The greatest common divisor, or zero

See also:

lcm (a, b)
Return the least common multiple of two numbers

Parameters:

  • a number The first number
  • b number The second number

Returns:

    number The least common multiple

Roots

nthroot (n, x)
Calculate the nth root of x nthroot(3,8)==2 because 2^3==8 All versions start with a guess based on the built in ^ operator This version performs newton's method until a convergent result or short loop is found Very accurate

Parameters:

  • n
  • x if (n * x == 0) return x * 0 -- preserves non-number type of x, such as rational or complex class
nthroot_short (n, x)
Calculate the nth root of x This version performs newton's method exactly 4 times Relatively accurate

Parameters:

  • n
  • x
nthroot_fast (n, x)
Calculate the nth root of x This version performs newton's method exactly once, only for positive x Least accurate, still much better than built in ^ operator alone

Parameters:

  • n
  • x

Distance

dist (x, y)
Distance to a point Uses dist_naive for small values, dist_trig for large tokens: 46

Parameters:

  • x number X coordinate of the point
  • y number Y coordinate of the point

Returns:

    number Distance from 0,0 to x,y
dist_naive (x, y)
Distance to a point Naive distance calculation Overflows for dist^2>=32768 tokens: 15 cycles: 9 + 28 = 37 (lua+sys)

Parameters:

  • x number X coordinate of the point
  • y number Y coordinate of the point

Returns:

    number Distance from 0,0 to x,y
dist_trig (x, y)
Distance to a point Uses trigonometry Error in range (0,0)-(127,127): max 0.0017, avg 0.0004 tokens: 23 cycles: 21 + 0 = 21 (lua+sys)

Parameters:

  • x number X coordinate of the point
  • y number Y coordinate of the point

Returns:

    number Distance from 0,0 to x,y
dist_squared (x, y)
Distance squared to a point

Parameters:

  • x number X coordinate of the point
  • y number Y coordinate of the point

Returns:

    number Distance from 0,0 to x,y

Trigonometry

asin_octant (d)
Arithmetic approximation of arcsin Intended for 0<=d<=.7071 Exact at d==0, d==sin(1/16), d==sin(1/8) Max error .0021 at d==sin(3/32) in given range asin() is the inverse of sin(); a=asin(d) when d=sin(a)

Parameters:

  • d number

Returns:

    arcsin of d

Interpolation

interp_linear (a, b, f)
Linear interpolation between two values (a,b,0) == a (a,b,1) == b (a,b,0.25) is 1/4 of the way from a to b

Parameters:

  • a number Starting value
  • b number Ending value
  • f number [0-1] from a to b

Returns:

    number value between a and b
interp_linear_inverse (a, b, v)
Inverse Linear Interpolation "How far along the way from a to b is v?" (a,b,a) == 0 (a,b,b) == 1 (a,b,(a+b)/2) == 0.5

Parameters:

  • a number Starting value
  • b number Ending value
  • v number Value between a and b, inclusive

Returns:

    number [0-1] from a to b
interp_sine (a, b, f)
Sine interpolation (a,b,0) == a (a,b,1) == b (a,b,0.5) == 70.71% of the way from a to b

Parameters:

  • a number Starting value
  • b number Ending value
  • f number [0-1] from a to b

Returns:

    number value between a and b
generated by LDoc 1.5.0 Last updated 2024-09-08 14:28:56