Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Math

RANDOMIZE · abs · acos · asin · atn · atn2 · ceil · clamp · cos · exp · fix · fixed · floor · int · lbound · lerp · log · log10 · mag · max · min · pi · pow · rnd · round · sgn · sin · sqr · tan · ubound


RANDOMIZE

Seed the engine’s random numbers (rnd), from seed or from the clock.

RANDOMIZE [seed]
randomize([seed])

Parameters

  • seed AS NUMBER = NULL - The seed; the clock when omitted.

Example

SCREEN 320, 240
RANDOMIZE 42
first = rnd
RANDOMIZE 42
PRINT rnd = first

Notes

It’s also the function randomize, for when you want what it gives back.

abs

Returns the absolute value of a number.

abs(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT abs(-7), abs(2.5)

acos

Returns the arccosine (inverse cosine) of a number, in radians. The argument must be in the range -1 to 1; outside it is a runtime error, because the result would be NaN, which is not a representable value.

acos(number)

Parameters

  • number - A value in the range -1 to 1

Example

SCREEN 320, 240
angle = acos(0.5)
PRINT angle, angle * 180 / pi

asin

Returns the arcsine (inverse sine) of a number, in radians. The argument must be in the range -1 to 1; outside it is a runtime error, because the result would be NaN, which is not a representable value.

asin(number)

Parameters

  • number - A value in the range -1 to 1

Example

SCREEN 320, 240
PRINT asin(1), asin(0)

atn

Returns the arctangent (inverse tangent) in radians.

atn(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT atn(1) * 4

atn2

Returns the two-argument arctangent (atan2) in radians. Computes the angle from the positive X axis to the point (x, y), handling all four quadrants correctly. This is the preferred function for computing angles between two points or from velocity components.

atn2(y, x)

Parameters

  • y - The Y component (vertical delta or sine component)
  • x - The X component (horizontal delta or cosine component)

Example

SCREEN 320, 240
here = (100, 100)
target = (150, 150)
d = target - here
PRINT "facing", atn2(d.y, d.x) * 180 / pi, "degrees"

ceil

Returns the smallest integer greater than or equal to the number (rounds up).

ceil(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT ceil(2.1), ceil(-2.1), ceil(3)

clamp

Constrains a value to lie within a specified range. If value is less than min, returns min. If value is greater than max, returns max. Otherwise returns value unchanged.

clamp(value, min, max)

Parameters

  • value - The value to clamp
  • min - The minimum bound
  • max - The maximum bound

Example

SCREEN 320, 240
hp = 130
PRINT clamp(hp, 0, 100), clamp(-5, 0, 100), clamp(42, 0, 100)

cos

Returns the cosine of an angle in radians.

cos(angle)

Parameters

  • angle - Angle in radians

Example

SCREEN 320, 240
PRINT cos(0), cos(pi)
x = 160 + cos(pi / 3) * 50
PRINT x

exp

Returns e raised to the power of number (e^x). A result too large to hold (above about EXP(709)) is a runtime error rather than infinity, which is not a representable value.

exp(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT exp(0), exp(1)

fix

Returns the integer part by truncating towards zero.

fix(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT fix(3.7), fix(-3.7)

fixed

Rounds a number to a fixed number of decimal places and returns it as a number. Useful for smoothing noisy inputs such as joystick axis values. For exact textual display, use fixed_str instead (it avoids floating-point representation artifacts).

fixed(value, places)

Parameters

  • value - Any numeric expression
  • places - Number of decimal places (0-15; negative values are treated as 0)

Example

SCREEN 320, 240
PRINT fixed(3.14159, 2), fixed(-0.56789, 1)

floor

Returns the largest integer less than or equal to the number (rounds down).

floor(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT floor(2.9), floor(-2.1)

int

Returns the largest integer less than or equal to number (floor function).

int(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT int(3.7), int(-3.7)

lbound

Returns the lower bound (lowest valid index) of an array. Always returns 0 since arrays use 0-based indexing, in every dimension. Accepts a partial index (arr(0)) the same way UBOUND does.

lbound(array)

Parameters

  • array - An array variable, or a partial index of one (arr(i), arr(i, j))

Example

SCREEN 320, 240
scores = [10, 20, 30]
FOR i = lbound(scores) ... ubound(scores)
    PRINT i, scores(i)
END FOR

lerp

Linear interpolation between two values by factor t. When t=0 returns a, when t=1 returns b, when t=0.5 returns the midpoint. Works with both scalar values and coordinates for smooth 2D/3D interpolation.

lerp(a, b, t)

Parameters

  • a - Start value (scalar or coordinate)
  • b - End value (scalar or coordinate)
  • t - Interpolation factor (typically 0 to 1)

Example

SCREEN 320, 240
PRINT lerp(0, 100, 0.25)
PRINT lerp((0, 0), (100, 50), 0.5)

log

Returns the natural logarithm (base e) of a number. The argument must be above 0: LOG(0) and a negative argument are runtime errors, because the result would be an infinity or NaN, neither of which is a representable value.

log(number)

Parameters

  • number - Any positive numeric expression

Example

SCREEN 320, 240
PRINT log(1), log(exp(2))

log10

Returns the base-10 logarithm of a number. The argument must be above 0: LOG10(0) and a negative argument are runtime errors, because the result would be an infinity or NaN, neither of which is a representable value.

log10(number)

Parameters

  • number - A positive numeric expression

Example

SCREEN 320, 240
PRINT log10(1000), log10(1)

mag

Returns the magnitude (Euclidean length) of a coordinate vector. For scalar values, returns the absolute value. Use MAG(a - b) to compute the distance between two coordinates.

mag(value)

Parameters

  • value - A coordinate or scalar value

Example

SCREEN 320, 240
a = (0, 0)
b = (3, 4)
PRINT mag(b - a), mag(-7)

max

Returns the larger of two values. Preserves the original value type (integer or float) of the result.

max(a, b)

Parameters

  • a - First value to compare
  • b - Second value to compare

Example

SCREEN 320, 240
PRINT max(3, 7), max(2.5, 1)

min

Returns the smaller of two values. Preserves the original value type (integer or float) of the result.

min(a, b)

Parameters

  • a - First value to compare
  • b - Second value to compare

Example

SCREEN 320, 240
PRINT min(3, 7), min(2.5, 1)

pi

The mathematical constant pi, 3.141592653589793, as a Float. A pseudo-variable, not a function — no parentheses.

pi

Example

SCREEN 320, 240
r = 10
PRINT pi, pi * r * r

pow

Returns base raised to the power of exponent. Also available as the ^ operator: base ^ exponent. The result is always a float, so fractional and negative exponents work. ^ binds tighter than every arithmetic operator including unary minus, so -2 ^ 2 is -(2 ^ 2) = -4; write (-2) ^ 2 to raise a negative base. It is right-associative, so 2 ^ 3 ^ 2 is 2 ^ (3 ^ 2) = 512. A result too large to hold (10 ^ 400), or one that is not a number, is a runtime error rather than an infinity or NaN, which are not representable values.

pow(base, exponent)

Parameters

  • base - The base number
  • exponent - The power to raise the base to

Example

SCREEN 320, 240
PRINT pow(2, 10), pow(9, 0.5)

rnd

Returns a random number between 0 and 1 (exclusive of 1).

rnd

Example

SCREEN 320, 240
RANDOMIZE 7
roll = int(rnd * 6) + 1
PRINT "rolled", roll

round

Returns the number rounded to the nearest integer.

round(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT round(2.4), round(2.6), round(-2.6)

sgn

Returns the sign of a number: 1 if positive, -1 if negative, 0 if zero.

sgn(number)

Parameters

  • number - Any numeric expression

Example

SCREEN 320, 240
PRINT sgn(-5), sgn(0), sgn(3.2)

sin

Returns the sine of an angle in radians.

sin(angle)

Parameters

  • angle - Angle in radians

Example

SCREEN 320, 240
PRINT sin(0), sin(pi / 2)
y = 120 + sin(pi / 6) * 40
PRINT y

sqr

Returns the square root of a number. A negative argument is a runtime error (the result would be NaN, which is not a representable value).

sqr(number)

Parameters

  • number - A non-negative numeric expression (a negative value is a runtime error)

Example

SCREEN 320, 240
PRINT sqr(16), sqr(2)

tan

Returns the tangent of an angle in radians.

tan(angle)

Parameters

  • angle - Angle in radians

Example

SCREEN 320, 240
PRINT tan(0), tan(pi / 4)

ubound

Returns the upper bound (highest valid index) of an array’s outermost dimension. Returns -1 if the array is empty or the argument is not an array.

ubound(array)

Parameters

  • array - An array variable, or a partial index of one (arr(i), arr(i, j))

Example

SCREEN 320, 240
scores = [10, 20, 30]
PRINT ubound(scores), ubound([])