SD Core for Windows WC2.0-0

SD Basic - Math Functions

Arithmetic, division and remainders, rounding, powers, trigonometry and random numbers.

This page covers the numeric side of SD BASIC: the arithmetic operators, the functions that divide and round, powers and logarithms, trigonometry, the functions that reduce a dynamic array to a number, bit manipulation, and the control over how many decimal places a number shows when it becomes a string.

SD folds case, so a program may be written in either case. Keywords are shown here in lower case. Braces mark optional parts of a syntax line; in the tables, italics mark something you supply and bold marks a word typed as it stands.

Where this port behaves differently from the older OpenQM and SD documentation, the difference is called out rather than left for you to discover.

Numbers in SD BASIC#

SD holds a number as either an integer or a floating-point value and moves between them as needed. You do not declare which; an expression that can stay integral does, and one that cannot becomes floating point.

Everything in SD is ultimately a string, so a variable holding "12" is a number wherever a number is wanted. A string that is not numeric evaluates as zero in an arithmetic context, and num() below is how you test before relying on that.

Arithmetic operators#

Operator Meaning
+ add
- subtract, and unary minus
* multiply
/ divide — the result may be fractional
** · ^ raise to a power. The two spellings are the same operator

There are also compound assignment operators — +=, -=, *=, /= and := for concatenation — so total += n is total = total + n.

There is no integer-divide operator. / always divides fully:

print 7 / 2        ;* prints 3.5

For a whole-number result use div(), idiv() or rdiv(), described below. // is not integer division — it is the marker for an unnamed common block, as in common // a, b, c.

Precedence#

Operators lower in this table are applied first. Anything in parentheses is evaluated before the expression around it.

Operators
1 ** ^
2 * /
3 + -
4 the fmt operator
5 : concatenation
6 < > = # <= >= <> matches, and their word forms lt gt eq ne le ge
7 and & or !

and and or have the same precedence, which is not what most languages do. They are applied left to right as they are met, so a or b and c is (a or b) and c — not a or (b and c). Parenthesise any condition that mixes them. This has been true since OpenQM and is not a change made by this port, but it surprises people arriving from C, Python or SQL, where and binds tighter.

Dividing by zero#

Dividing by zero stops the program with a runtime error, in /, div(), idiv() and rdiv() alike. (There is no option that turns the error into a warning that yields zero. Test the divisor before you divide.)

Dividing to a whole number#

Three functions divide and return an integer, and they do not round the same way. Picking the wrong one is the commonest numeric bug in MultiValue code, so the differences are set out in full.

Format#

div(dividend, divisor)
idiv(dividend, divisor)
rdiv(dividend, divisor)
Function Rule
div() divides and discards the fraction, always toward zero
idiv() toward zero when both arguments are integers, downwards when either is floating point — see the warning below
rdiv() divides and rounds to the nearest integer, with a half rounded away from zero

The four side by side#

Expression Result Expression Result
div(7, 2) 3 rdiv(7, 2) 4
div(-7, 2) -3 rdiv(-7, 2) -4
div(7, -2) -3 rdiv(7, -2) -4
div(-7.5, 2) -3 rdiv(5, 2) 3
idiv(7, 2) 3 rdiv(-5, 2) -3
idiv(-7, 2) -3

idiv() changes its rounding when a floating-point number is involved, and nothing in the call shows it. With two integers it truncates toward zero; if either argument is floating point it takes the floor, which for a negative result is a different number:

idiv(-7, 2) ;* -3 both arguments are integers idiv(-7.0, 2) ;* -4 one argument is floating point

The two differ only for negative results — for positive ones truncation and floor agree. If the sign can be negative, use div(), which truncates toward zero whatever the argument types are. This behavior is inherited from OpenQM and has been kept for compatibility; it is not new in this port.

div() returns a whole number even when the arguments are fractional: div(7.5, 2) is 3.

Remainders#

mod(dividend, divisor)
rem(dividend, divisor)

Both give the remainder of a division and they differ on the sign. mod() takes the sign of the divisor; rem() takes the sign of the dividend.

Expression mod rem
(7, 3) 1 1
(-7, 3) 2 -1
(7, -3) -2 1
(-7, -3) -1 -1

mod() is the mathematical modulus — the result always lies between zero and the divisor — which is what you want for wrapping a value into a range, such as an index into a cyclic table. rem() is the remainder C and most other languages give, and is what you want when you are reconstructing a value from a quotient and a remainder.

Neither raises a divide-by-zero error. A divisor of zero returns the dividend unchanged: mod(7, 0) and rem(7, 0) are both 7. A loop that relies on a remainder shrinking will not terminate if the divisor can reach zero, and nothing will tell you.

Truncation, sign and magnitude#

int(expr)
abs(expr)
neg(expr)
int() discards the fractional part, toward zero. int(3.7) is 3 and int(-3.7) is -3. It does not round: use rdiv(n, 1) or add 0.5 if you want rounding
abs() the magnitude, with any sign removed. abs(-3.5) is 3.5
neg() the value with its sign reversed. neg(3) is -3

int() is affected by a configuration parameter. INTPREC sets how many decimal places are considered before truncating, which stops a value that is 2.9999999999 only because of floating-point representation from truncating to 2. It defaults to 11 and may be set from 0 to 14. config('INTPREC') reports the value in force, and config gpl is not where to look — the whole parameter list is shown by the config verb.

Powers, roots and logarithms#

pwr(base, exponent)
sqrt(expr)
exp(expr)
ln(expr)
pwr() base raised to exponent. Identical to the ** and ^ operators, so pwr(2, 10), 2 ** 10 and 2 ^ 10 all give 1024. A fractional exponent works: pwr(2, 0.5) is 1.4142
sqrt() the square root. sqrt(2) is 1.4142
exp() e raised to the given power. exp(1) is 2.7183
ln() the natural logarithm, base e. ln(10) is 2.3026

There is no base-10 logarithm function. Divide by the natural log of ten: ln(x) / ln(10).

sqrt() and ln() stop the program on a negative argument — "SQRT() attempted for negative value" and "LN() attempted for negative value". Neither returns an error code you can test, so guard the argument yourself if it can go negative.

The four decimal places shown above are not the precision of the calculation — they are the default precision applied when the number is turned into a string for printing. See Controlling how numbers print.

Trigonometry#

sin(expr)     asin(expr)
cos(expr)     acos(expr)
tan(expr)     atan(expr)

The arguments and results are in degrees, not radians. This catches people out constantly, because almost every other language's maths library uses radians. There is no radian variant.

Expression Result Expression Result
sin(30) 0.5 asin(0.5) 30
cos(60) 0.5 acos(0.5) 60
tan(45) 1 atan(1) 45

To work in radians, convert: multiply by 180 / 3.14159265358979 on the way in, and divide on the way out.

Random numbers#

rnd(limit)
randomize {seed}

rnd() returns a whole number from 0 to limit minus one — so rnd(10) gives 0 through 9, confirmed over 500 draws. A negative limit gives a negative result in the mirrored range: rnd(-10) returns 0 down to -9.

randomize seeds the generator. With a seed the sequence repeats identically every run — randomize 42 followed by three rnd(1000) calls gives the same three numbers every time — which is what you want for a reproducible test. With no argument it seeds from the clock, which is what you want in production.

randomize 42
for i = 1 to 5
   print rnd(100)
next i

This is not a cryptographic random number generator and must not be used to make passwords, tokens or keys. It is the C library's rand(), seeded from a 32-bit value. For anything security-bearing use sdencrypt() and the facilities described under Security rather than building your own.

Whole numbers above and below: ceil, floor, roundup, rounddown#

ceil(value)
floor(value)
roundup(value, step)
rounddown(value, step)

ceil() is the smallest whole number not below the value and floor() the greatest not above it. Where int() drops the fraction, floor(-3.1) is -4 and ceil(-3.1) is -3.

roundup() goes to the next multiple of step away from zero and rounddown() to the previous one toward zero; a value that is already a multiple is unchanged. With a step of 3, roundup(10, 3) is 12, rounddown(10, 3) is 9, roundup(-4, 3) is -6 and rounddown(-4, 3) is -3. A step under 1 gives 0.

All four take a dynamic array and answer with one of the same shape.

Numbers of any length: sadd, ssub, smul, sdiv, scmp#

sadd(a, b)        a + b
ssub(a, b)        a - b
smul(a, b)        a * b
sdiv(a, b)        a / b
scmp(a, b)        -1, 0 or 1

These do arithmetic on numbers held as strings of digits, of any length, exactly: no digit is lost to the size of a machine number. Each argument may have a sign and a decimal point. The answer is a string with no leading zeros and no trailing zeros after the point.

x = sadd('99999999999999999999', '1')       ;* '100000000000000000000'
y = smul('123456789012345678', '1000')      ;* '123456789012345678000'
z = sdiv('1', '3')                          ;* '0.333333333333333'

sdiv() gives at most 15 decimal places; further places are cut off, not rounded. Dividing by zero stops the program with a run-time error. scmp() answers -1 when a is less than b, 0 when they are equal and 1 when a is greater; '1.50' and '1.5' are equal.

Continued in#

SD Basic - Numeric Tests and Formatting — reducing an array to a number, numeric tests, bit operations, checksums and how numbers print.