Math Utilities #18

Parent #72Owner #29Flags readSource hellcore/hellcore.db

Aliases: Math Utilities, Math_Utils, trigonometric utilites, trig_utils

50 verbs · 20 properties · 0 children

Verbs

VerbSpecFlagsDefinerLines
xsinthis none thisrxd#189
xcosthis none thisrxd#189
factorialthis none thisrxd#1811
powthis none thisrxd#1822
fibonaccithis none thisrxd#1815
geometricthis none thisrxd#1813
divmodthis none thisrxd#189
combinationsthis none thisrxd#1818
permutationsthis none thisrxd#1816
simpsonthis none thisrxd#1812
partsthis none thisrxd#1810
sqrtthis none thisrxd#1815
divthis none thisrxd#182
modthis none thisrxd#187
expthis none thisrxd#1815
aexpthis none thisrxd#1826
randomthis none thisrxd#188
random_rangethis none thisrxd#188
is_primethis none thisrxd#1822
AND XORthis none thisrxd#1825
ORthis none thisrxd#181
NOTthis none thisrxd#189
BLFromIntthis none thisrxd#1814
IntFromBLthis none thisrxd#188
gcd greatest_common_divisorthis none thisrxd#1816
lcm least_common_multiplethis none thisrxd#186
are_rel_prime are_relatively_primethis none thisrxd#188
base_conversionthis none thisrxd#1844
normthis none thisrxd#1818
sinthis none thisrxd#1828
costhis none thisrxd#1814
tanthis none thisrxd#188
arcsin asinthis none thisrxd#1835
arccos acosthis none thisrxd#1813
arctan atanthis none thisrxd#189
deg2rads deg2radthis none thisrxd#182
rads2deg rad2degthis none thisrxd#182
precisionthis none thisrxd#185
roundthis none thisrxd#186
mean averagethis none thisrxd#184
sum_floatthis none thisrxd#1810
sum_int sumthis none thisrxd#1813
rintthis none thisrxd#184
gil_float_to_intthis none thisrxd#1822
approx_numberthis none thisrxd#1813
signthis none thisrxd#181
addthis none thisrxd#181
line_lenthis none thisrxd#183
constrainthis none thisrxd#188
points_to_anglethis none thisrxd#1816

Properties

PropertyDefinerFlagsOwnerValue
base_alphabet#18rc#29"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
tangents#18rc#29
list of 45{174, 349, 524, 699, 874, 1051, 1227, 1405, 1583, 1763, 1943, 2125, 2308, 2493, 2679, 2867, 3057, 3249, 3443, 3639, 3838, 4040, 4244, 4452, 4663, 4877, 5095, 5317, 5543, 5773, 6008, 6248, 6494, 6745, 7002, 7265, 7535, 7812, 8097, 8390, 8692, 9004, 9325, 9656, 10000}
factor#18rc#2910000
taylor#18rc#29100
and#18rc#29
list of 16{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2}, {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}, {0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 4, 4, 4, 4}, {0, 1, 0, 1, 4, 5, 4, 5, 0, 1, 0, 1, 4, 5, 4, 5}, {0, 0, 2, 2, 4, 4, 6, 6, 0, 0, 2, 2, 4, 4, 6, 6}, {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7}, {0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8}, {0, 1, 0, 1, 0, 1, 0, 1, 8, 9, 8, 9, 8, 9, 8, 9}, {0, 0, 2, 2, 0, 0, 2, 2, 8, 8, 10, 10, 8, 8, 10, 10}, {0, 1, 2, 3, 0, 1, 2, 3, 8, 9, 10, 11, 8, 9, 10, 11}, {0, 0, 0, 0, 4, 4, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12}, {0, 1, 0, 1, 4, 5, 4, 5, 8, 9, 8, 9, 12, 13, 12, 13}, {0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}
xor#18rc#29
list of 16{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14}, {2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13}, {3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12}, {4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11}, {5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14, 9, 8, 11, 10}, {6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9}, {7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8}, {8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7}, {9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7, 6}, {10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5}, {11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4}, {12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3}, {13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2}, {14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1}, {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}}
sines#18rc#29
list of 361{175, 349, 523, 698, 872, 1045, 1219, 1392, 1564, 1736, 1908, 2079, 2250, 2419, 2588, 2756, 2924, 3090, 3256, 3420, 3584, 3746, 3907, 4067, 4226, 4384, 4540, 4695, 4848, 5000, 5150, 5299, 5446, 5592, 5736, 5878, 6018, 6157, 6293, 6428, 6561, 6691, 6820, 6947, 7071, 7193, 7314, 7431, 7547, 7660, 7771, 7880, 7986, 8090, 8192, 8290, 8387, 8480, 8572, 8660, 8746, 8829, 8910, 8988, 9063, 9135, 9205, 9272, 9336, 9397, 9455, 9511, 9563, 9613, 9659, 9703, 9744, 9781, 9816, 9848, 9877, 9903, 9925, 9945, 9962, 9976, 9986, 9994, 9998, 10000, 9998, 9994, 9986, 9976, 9962, 9945, 9925, 9903, 9877, 9848, 9816, 9781, 9744, 9703, 9659, 9613, 9563, 9511, 9455, 9397, 9336, 9272, 9205, 9135, 9063, 8988, 8910, 8829, 8746, 8660, 8572, 8480, 8387, 8290, 8192, 8090, 7986, 7880, 7771, 7660, 7547, 7431, 7314, 7193, 7071, 6947, 6820, 6691, 6561, 6428, 6293, 6157, 6018, 5878, 5736, 5592, 5446, 5299, 5150, 5000, 4848, 4695, 4540, 4384, 4226, 4067, 3907, 3746, 3584, 3420, 3256, 3090, 2924, 2756, 2588, 2419, 2250, 2079, 1908, 1736, 1564, 1392, 1219, 1045, 872, 698, 523, 349, 175, 0, -175, -349, -523, -698, -872, -1045, -1219, -1392, -1564, -1736, -1908, -2079, -2250, -2419, -2588, -2756, -2924, -3090, -3256, -3420, -3584, -3746, -3907, -4067, -4226, -4384, -4540, -4695, -4848, -5000, -5150, -5299, -5446, -5592, -5736, -5878, -6018, -6157, -6293, -6428, -6561, -6691, -6820, -6947, -7071, -7193, -7314, -7431, -7547, -7660, -7771, -7880, -7986, -8090, -8192, -8290, -8387, -8480, -8572, -8660, -8746, -8829, -8910, -8988, -9063, -9135, -9205, -9272, -9336, -9397, -9455, -9511, -9563, -9613, -9659, -9703, -9744, -9781, -9816, -9848, -9877, -9903, -9925, -9945, -9962, -9976, -9986, -9994, -9998, -10000, -9998, -9994, -9986, -9976, -9962, -9945, -9925, -9903, -9877, -9848, -9816, -9781, -9744, -9703, -9659, -9613, -9563, -9511, -9455, -9397, -9336, -9272, -9205, -9135, -9063, -8988, -8910, -8829, -8746, -8660, -8572, -8480, -8387, -8290, -8192, -8090, -7986, -7880, -7771, -7660, -7547, -7431, -7314, -7193, -7071, -6947, -6820, -6691, -6561, -6428, -6293, -6157, -6018, -5878, -5736, -5592, -5446, -5299, -5150, -5000, -4848, -4695, -4540, -4384, -4226, -4067, -3907, -3746, -3584, -3420, -3256, -3090, -2924, -2756, -2588, -2419, -2250, -2079, -1908, -1736, -1564, -1392, -1219, -1045, -872, -698, -523, -349, -175, 0, 175}
pi#18rc#293.14159265358979
piscale#18rc#290.00872664625997164
help_msg#72rc#29
list of 56{"Trigonometric/Exponential functions:", " sin(a),cos(a),tan(a) -- returns 10000*(the value of the corresponding", " trigonometric function) angle a is in degrees.", " arctan([x,]y) -- returns arctan(y/x) in degrees in the range -179..180.", " x defaults to 10000. Quadrant is that of (x,y).", " exp(x[,n]) -- calculates e^x with an nth order taylor polynomial", " aexp(x) -- calculates 10000 e^(x/10000)", "", "Statistical functions:", " combinations(n,r) -- returns the number of combinations given n objects", " taken r at a time.", " permutations(n,r) -- returns the number of permutations possible given", " n objects taken r at a time.", "", "Number decomposition:", " div(n,d) -- correct version of / (handles negative numbers correctly)", " mod(n,d) -- correct version of % (handles negative numbers correctly)", " divmod(n,d) -- {div(n,d),mod(n,d)}", " parts(n,q[,i]) -- returns a list of two elements {integer,decimal fraction}", "", "Other math functions:", " sqrt(x) -- returns the largest integer n <= the square root of x", " pow(x,n) -- returns x^n", " factorial(x) -- returns x!", " norm(a,b,c,d,...) -- returns sqrt(a^2+b^2+c^2+...)", " sum(a,b,c,d,...) -- returns the sum of all arguments.", "", "Series:", " fibonacci(n) -- returns the 1st n fibonacci numbers in a list", " geometric(x,n) -- returns the value of the nth order geometric series at x", "", "Integer Properties:", " gcd(a,b) -- find the greatest common divisor of the two numbers", " lcm(a,b) -- find the least common multiple of the two numbers", " are_relatively_prime(a,b) -- return 1 if a and b are relatively prime", " is_prime(n) -- returns 1 if the number is a prime and 0 otherwise", " ", "Miscellaneous:", " random(n) -- returns a random number from 0..n if n > 0 or n..0 if n < 0", " random_range(n[,mean]) -- returns a random number from mean - n..mean + n", " with mean defaulting to 0", " simpson({a,b},{f(a),f((a+b)/2),f(b)}) -- returns the numerical", " approximation of an integral using simpson's rule", " base_conversion(num|string, oldbase, newbase [,sens]) -- converts the number", " given as first arg from oldbase to the newbase.", "", "Bitwise Arithmetic:", " AND(x,y) -- returns x AND y", " OR(x,y) -- returns x OR y", " XOR(x,y) -- returns x XOR y (XOR is the exclusive-or function)", " NOT(x) -- returns the complement of x", " All bitwise manipulation is of 32-bit values.", "", "Bitwise Conversions:", " BlFromInt(d) -- converts a decimal number d to a list of 1's and 0's, 32-bit", " IntFromBl(b) -- converts a list of 1's and 0's (any precision) to decimal"}
aliases#1rc#29{"Math Utilities", "Math_Utils", "trigonometric utilites", "trig_utils"}
description#1rc#29{"This is the Math Utilities utility package. See `help $math_utils' for more details."}
object_size#1r#29{39588, 1298433815}
hidden_verbs#1rc#29<clear>
phelp_msg#1rc#29<clear>
weight#1rc#29<clear>
owner_verbs#1rc#29<clear>
plural_name#1rc#29<clear>
client_image#1rc#29<clear>
listening#1rc#29<clear>

Ancestry

Ancestors (nearest first): #72 Generic Utilities Package#1 root

Children: none

Call graph

calls n18_0 #18:xsin n18_0->n18_0 n18_1 #18:xcos n18_0->n18_1 n18_1->n18_0 n18_1->n18_1 n18_9 #18:simpson n18_10 #18:parts n18_9->n18_10 n18_12 #18:div n18_6 #18:divmod n18_12->n18_6 n18_14 #18:exp n18_14->n18_10 n18_17 #18:random_range n18_16 #18:random n18_17->n18_16 n18_20 #18:OR n18_21 #18:NOT n18_20->n18_21 n18_19 #18:AND n18_20->n18_19 n18_22 #18:BLFromInt n18_21->n18_22 n18_23 #18:IntFromBL n18_21->n18_23 n18_25 #18:lcm n18_24 #18:gcd n18_25->n18_24 n18_26 #18:are_rel_prime n18_26->n18_24 n18_30 #18:cos n18_29 #18:sin n18_30->n18_29 n18_31 #18:tan n18_31->n18_30 n18_31->n18_29 n18_33 #18:arccos n18_32 #18:arcsin n18_33->n18_32 n18_34 #18:arctan n18_34->n18_33 n18_37 #18:precision n18_42 #18:rint n18_37->n18_42 n18_39 #18:mean n18_41 #18:sum_int n18_39->n18_41

Source

xsin

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"xsin(INT x) -- calculates the taylor approximation for the sine function";
2if (typeof(x = args[1]) != INT)
3return E_TYPE;
4endif
5if ((x * x) > this.taylor)
6return ((this:xsin(x / 2) * this:xcos((x + 1) / 2)) + (this:xsin((x + 1) / 2) * this:xcos(x / 2))) / 10000;
7else
8return (x * (17453000 - ((x * x) * 886))) / 100000;
9endif

xcos

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"xcos(INT x) -- calculates the taylor approximation for the cosine function";
2if (typeof(x = args[1]) != INT)
3return E_TYPE;
4endif
5if ((x * x) > this.taylor)
6return ((this:xcos(x / 2) * this:xcos((x + 1) / 2)) - (this:xsin(x / 2) * this:xsin((x + 1) / 2))) / 10000;
7else
8return (1000000000 - ((x * x) * (152309 + ((4 * x) * x)))) / 100000;
9endif

factorial

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"factorial(INT n) -- returns n factorial for 0 <= n (<= 12).";
2if ((number = args[1]) < 0)
3return E_INVARG;
4elseif (typeof(number) != INT)
5return E_TYPE;
6endif
7fact = 1;
8for i in [2..number]
9fact = fact * i;
10endfor
11return fact;

pow

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"pow(INT|FLOAT x,(INT)|(INT|FLOAT) n) -- returns x raised to the nth power. n must be >= 0. If x is an integer, n must be an integer. If x is a floating point number, n can be either.";
2{x, n} = args;
3if (n < 0)
4return E_INVARG;
5elseif ((typeof(x) == INT) && (typeof(n) == FLOAT))
6return E_TYPE;
7endif
8return x ^ n;
9"old code below";
10n = args[1];
11if (power % 2)
12ret = n;
13else
14ret = 1;
15endif
16while (power = power / 2)
17n = n * n;
18if (power % 2)
19ret = ret * n;
20endif
21endwhile
22return ret;

fibonacci

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"fibonacci(INT n) -- calculates the fibonacci numbers to the nth term";
2"and returns them in a list. n must be >= 0.";
3if (typeof(n = args[1]) != INT)
4return E_TYPE;
5elseif (n < 0)
6return E_INVARG;
7elseif (n == 0)
8return {0};
9else
10x = {0, 1};
11for i in [2..n]
12x = {@x, x[$ - 1] + x[$]};
13endfor
14return x;
15endif

geometric

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"geometric(INT|FLOAT x [,INT n]) -- calculates the value of the geometric series at x to the nth term. i.e., approximates 1/(1-x) when |x| < 1. This, of course, is impossible in MOO, but someone may find it useful in some way.";
2"n defaults to 5. n must be >= 0.";
3{n, ?order = 5} = args;
4if ((!(typeof(n) in {INT, FLOAT})) || (typeof(order) != INT))
5return E_TYPE;
6elseif (order <= 0)
7return E_INVARG;
8endif
9x = 1;
10for i in [1..order]
11x = x + (n ^ i);
12endfor
13return x;

divmod

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"divmod(INT n, INT d) => {q,r} such that n = dq + r";
2"  handles negative numbers correctly   0<=r<d if d>0, -d<r<=0 if d<0.";
3{n, d} = args;
4if ((typeof(n) != INT) && (typeof(d) != INT))
5return E_TYPE;
6endif
7r = ((n % d) + d) % d;
8q = (n - r) / d;
9return {q, r};

combinations

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"combinations(INT n, INT r) -- returns the number of ways one can choose r";
2"objects from n distinct choices.";
3"C(n,r) = n!/[r!(n-r)!]";
4"  overflow may occur if n>29...";
5{n, r} = args;
6if ((typeof(n) != INT) && (typeof(r) != INT))
7return E_TYPE;
8endif
9if (0 > (r = min(r, n - r)))
10return 0;
11else
12c = 1;
13n = n + 1;
14for i in [1..r]
15c = (c * (n - i)) / i;
16endfor
17return c;
18endif

permutations

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"permutations(INT n, INT r) -- returns the number of ways possible for one to";
2"order r distinct objects given n locations.";
3"P(n,r) = n!/(n-r)!";
4{n, r} = args;
5if ((typeof(n) != INT) && (typeof(r) != INT))
6return E_TYPE;
7endif
8if ((r < 1) || ((diff = n - r) < 0))
9return 0;
10else
11p = n;
12for i in [diff + 1..n - 1]
13p = p * i;
14endfor
15return p;
16endif

simpson

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"simpson({a,b},{f(a),f((a+b)/2),f(b)} [,INT ret-float])";
2" -- given two endpoints, a and b, and the functions value at a, (a+b)/2, and b, this will calculate a numerical approximation of the integral using simpson's rule.";
3"Entries can either be all INT or all FLOAT. Don't mix!";
4"If the optional 3rd argument is provided and true, the answer is returned as a floating point regardless of what the input was. Otherwise, if the input was all INT, the answer is returned as {integer,fraction}";
5{point, fcn, ?retfloat = 0} = args;
6if ((!retfloat) && (typeof(point[1]) == INT))
7numer = (point[2] - point[1]) * ((fcn[1] + (4 * fcn[2])) + fcn[3]);
8return this:parts(numer, 6);
9else
10numer = tofloat(point[2] - point[1]) * ((tofloat(fcn[1]) + (4.0 * tofloat(fcn[2]))) + tofloat(fcn[3]));
11return numer / 6.0;
12endif

parts

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"parts(INT n, INT q [,INT i]) -- returns a decomposition of n by q into integer and floating point parts with i = the number of digits after the decimal.";
2"i defaults to 5.";
3"warning: it is quite easy to hit maxint which results in unpredictable";
4"         results";
5{n, q, ?i = 5} = args;
6if (((typeof(n) != INT) && (typeof(q) != INT)) && (typeof(i) != INT))
7return E_TYPE;
8endif
9parts = {n / q, n % q};
10return {parts[1], (parts[2] * (10 ^ i)) / q};

sqrt

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"sqrt(INT|FLOAT n) => largest integer <= square root of n. Returns the same type as the input. (Backwards compatibility)";
2n = args[1];
3return (typeof(n) == INT) ? toint(sqrt(tofloat(n))) | sqrt(n);
4"Old code. Newton's method";
5if (n < 0)
6return E_RANGE;
7elseif (n)
8x1 = n;
9while (x1 > (x2 = (x1 + (n / x1)) / 2))
10x1 = x2;
11endwhile
12return x1;
13else
14return 0;
15endif

div

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"div(INT n, INT d) => q such that n = dq + r and  (0<=r<d if d>0, -d<r<=0 if d<0).";
2return this:divmod(@args)[1];

mod

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"A correct mod function.";
2"mod(INT n, INT d) => r such that n = dq + r and (0<=r<d if d>0 or -d<r<=0 if d<0).";
3{n, d} = args;
4if ((typeof(n) != INT) && (typeof(d) != INT))
5return E_TYPE;
6endif
7return ((n % d) + d) % d;

exp

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"exp(INT|FLOAT x[,INT n]) -- calculates an nth order taylor approximation for e^x.";
2"n defaults to 5. Any n given must be >= 0. you need to divide the result";
3"the answer will be returned as {integer part,fractional part} if the input x was an integer. If it is floating point, so will the answer (and this uses the builtin function.)";
4{x, ?n = 5} = args;
5if (typeof(x) == FLOAT)
6return exp(x);
7elseif ((typeof(x) != INT) && (typeof(n) != INT))
8return E_TYPE;
9endif
10ex = nfact = 1;
11for i in [0..n - 1]
12j = n - i;
13ex = (ex * x) + (nfact = nfact * j);
14endfor
15return this:parts(ex, nfact);

aexp

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"returns 10000 exp (x/10000)";
2"The accuracy seems to be ~0.1% for 0<x<4";
3x = args[1];
4if (x < 0)
5z = this:(verb)(-x);
6return (100000000 + (z / 2)) / z;
7elseif (x > 1000)
8z = this:(verb)(x / 2);
9if (z > 1073741823)
10return $maxint;
11"maxint for overflows";
12elseif (z > 460000)
13z = ((z + 5000) / 10000) * z;
14elseif (z > 30000)
15z = ((((z + 50) / 100) * z) + 50) / 100;
16else
17z = ((z * z) + 5000) / 10000;
18endif
19if (x % 2)
20return z + ((z + 5000) / 10000);
21else
22return z;
23endif
24else
25return ((10000 + x) + (((x * x) + 10000) / 20000)) + ((((x * x) * x) + 300000000) / 600000000);
26endif

random

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"random(INT n): returns a random integer in the following manner:";
2"random(n > 0) will return a integer in the range 0 to n";
3"random(n < 0) will return a integer in the range n to 0";
4if (typeof(prob = args[1]) != INT)
5return E_TYPE;
6endif
7mod = (prob < 0) ? -1 | 1;
8return (mod * random(abs(prob + mod))) - mod;

random_range

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"random_range(INT range [,INT mean]): returns a random integer within the given range from the mean. if the mean isn't given, it defaults to 0";
2"e.g., random_range(10) => -10..10";
3"      random_range(10,4) => -6..14";
4{range, ?mean = 0} = args;
5if ((typeof(range) != INT) && (typeof(mean) != INT))
6return E_TYPE;
7endif
8return mean + (((random(2) == 1) ? -1 | 1) * this:random(range));

is_prime

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"is_prime(INT number) returns 1 if the number is prime or 0 if it isn't.";
2"of course, only positive numbers are candidates for primality.";
3if (typeof(number = args[1]) != INT)
4return E_TYPE;
5endif
6if (number == 2)
7return 1;
8elseif ((number < 2) || ((number % 2) == 0))
9return 0;
10else
11choice = 3;
12while (((denom = choice * choice) <= number) && (denom > 0))
13if ((seconds_left() < 2) || (ticks_left() < 25))
14suspend(0);
15endif
16if ((number % choice) == 0)
17return 0;
18endif
19choice = choice + 2;
20endwhile
21endif
22return 1;

AND XOR

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"Only useful for integer input.";
2{x, y} = args;
3if ((typeof(x) != INT) && (typeof(y) != INT))
4return E_TYPE;
5endif
6table = this.(verb);
7if (xsgn = x < 0)
8x = x + $minint;
9endif
10if (ysgn = y < 0)
11y = y + $minint;
12endif
13power = 1;
14z = 0;
15while (x || y)
16z = z + (power * table[1 + (x % 16)][1 + (y % 16)]);
17x = x / 16;
18y = y / 16;
19power = power * 16;
20endwhile
21if (table[1 + xsgn][1 + ysgn])
22return z + $minint;
23else
24return z;
25endif

OR

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1return this:NOT(this:AND(this:NOT(args[1]), this:NOT(args[2])));

NOT

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1return -(1 + args[1]);
2"";
3"... here's what it used to be ...";
4bl1 = this:BLFromInt(args[1]);
5blOut = {};
6for i in [1..32]
7blOut = {@blOut, !bl1[i]};
8endfor
9return this:IntFromBL(blOut);

BLFromInt

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"BlFromInt(INT x) => converts the number provided into a 32 bit binary number, which is returned via a 32 element LIST of 1's and 0's. Note that this verb was originally written to be used with the $math_utils verbs: AND, NOT, OR, XOR, but has since been taken out of them.";
2if (typeof(x = args[1]) != INT)
3return E_TYPE;
4endif
5l = {};
6firstbit = x < 0;
7if (firstbit)
8x = x + $minint;
9endif
10for i in [1..31]
11l = {x % 2, @l};
12x = x / 2;
13endfor
14return {firstbit, @l};

IntFromBL

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"IntFromBl(LIST of 1's and 0's) => converts the 32 bit binary representation given by the list of 1's and 0's and converts it to a normal decimal number. Note that this verb was originally written to be used with the $math_utils verbs: AND, NOT, OR, XOR, but has since been taken out of them.";
2bl = args[1];
3x = 0;
4for l in (bl)
5x = x * 2;
6x = x + l;
7endfor
8return x;

gcd greatest_common_divisor

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"gcd(INT num1,INT num2): find the greatest common divisor of the two numbers";
2"using the division algorithm. the absolute values of num1 and num2 are";
3"used without loss of generality.";
4num1 = abs(args[1]);
5num2 = abs(args[2]);
6max = max(num1, num2);
7min = min(num1, num2);
8if (r1 = max % min)
9while (r2 = min % r1)
10min = r1;
11r1 = r2;
12endwhile
13return r1;
14else
15return min;
16endif

lcm least_common_multiple

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"lcm(INT num1,INT num2): find the least common multiple of the two numbers.";
2"we shall use the positive lcm value without loss of generality.";
3"since we have gcd already, we'll just use lcm*gcd = num1*num2";
4num1 = abs(args[1]);
5num2 = abs(args[2]);
6return (num1 * num2) / this:gcd(num1, num2);

are_rel_prime are_relatively_prime

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"are_rel_prime(INT num1,INT num2): returns 1 if num1 and num2 are relatively";
2"prime.";
3"since we have gcd, this is pretty easy.";
4if (this:gcd(args[1], args[2]) == 1)
5return 1;
6else
7return 0;
8endif

base_conversion

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"Call with first arg either a number or a string, being the number";
2"desired for conversion. capital letters denote values from 10-35;";
3"lowercase letters from 36 to 61. Maximal base is 62.";
4"You will be unable to use the extra 26 lowercases as separate unless";
5"you pass a nonzero fourth argument. Passing zero or none uses the";
6"default value, which is to have AAAA=aaaa.";
7"The second and third arguments should be the base of the number and";
8"the base you want it in, respectively.";
9"Any of the arguments can be strings or nums, but high-base numbers";
10"will need to be strings. This returns a string.";
11"Any problems, talk to Ozymandias.";
12sensitive = 0;
13if (length(args) < 3)
14return E_INVARG;
15elseif (length(args) == 4)
16sensitive = toint(args[4]);
17endif
18result = 0;
19thenum = tostr(args[1]);
20origbase = toint(args[2]);
21newbase = toint(args[3]);
22if ((((origbase < 2) || (newbase < 2)) || (origbase > 62)) || (newbase > 62))
23return E_INVARG;
24endif
25for which in [1..length(thenum)]
26value = index(this.base_alphabet, thenum[which], sensitive);
27if ((!value) || (value > origbase))
28return E_INVARG;
29endif
30result = ((result * origbase) + value) - 1;
31endfor
32thestring = "";
33if (result < 0)
34return E_INVARG;
35endif
36while (result)
37if ((which = (result % newbase) + 1) <= length(this.base_alphabet))
38thestring = this.base_alphabet[which] + thestring;
39else
40return E_INVARG;
41endif
42result = result / newbase;
43endwhile
44return thestring;

norm

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1":norm(a,b,c,d...) => sqrt(a^2+b^2+c^2+...)";
2m = max(max(@args), -min(@args));
3logm = length(tostr(m));
4if (logm <= 4)
5s = 0;
6for a in (args)
7s = s + (a * a);
8endfor
9return toint(sqrt(tofloat(s)));
10else
11factor = toint("1" + "0000000"[1..logm - 4]);
12s = 0;
13for a in (args)
14a = a / factor;
15s = s + (a * a);
16endfor
17return toint(sqrt(tofloat(s))) * factor;
18endif

sin

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"Copied from Trig_Utils (#25800):sin by Obvious (#54879) Fri Nov 17 06:07:39 1995 PST";
2theta = args[1];
3if (typeof(theta) == FLOAT)
4return sin(theta);
5elseif (typeof(theta) == INT)
6degtheta = theta % 360;
7mintheta = 0;
8elseif (typeof(theta) == LIST)
9degtheta = theta[1] % 360;
10mintheta = theta[2] % 60;
11else
12return E_INVARG;
13endif
14if (mintheta < 0)
15mintheta = mintheta + 60;
16degtheta = degtheta - 1;
17endif
18while (degtheta < 1)
19degtheta = degtheta + 360;
20endwhile
21if (mintheta == 0)
22return this.sines[degtheta];
23endif
24lim1 = this.sines[degtheta];
25lim2 = this.sines[degtheta + 1];
26delta = lim2 - lim1;
27result = (((delta * mintheta) + 30) / 60) + lim1;
28return result;

cos

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"Copied from Trig_Utils (#25800):cos by Obvious (#54879) Fri Nov 17 06:07:50 1995 PST";
2theta = args[1];
3if (typeof(theta) == FLOAT)
4return cos(theta);
5elseif (typeof(theta) == INT)
6degtheta = 90 - theta;
7mintheta = 0;
8elseif (typeof(theta) == LIST)
9degtheta = 89 - theta[1];
10mintheta = 60 - theta[2];
11else
12return;
13endif
14return this:sin({degtheta, mintheta});

tan

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"Copied from Trig_Utils (#25800):tan by Obvious (#54879) Fri Nov 17 06:07:53 1995 PST";
2{theta} = args;
3if (typeof(theta) == FLOAT)
4return tan(theta);
5endif
6sine = this:sin(theta);
7cosine = this:cos(theta);
8return ((sine * 10000) + ((cosine + 1) / 2)) / cosine;

arcsin asin

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"Copied from Trig_Utils (#25800):arcsin by Obvious (#54879) Fri Nov 17 06:08:01 1995 PST";
2{given} = args;
3if (typeof(given) == FLOAT)
4return asin(given);
5endif
6given = abs(given);
7if (given > 10000)
8return E_RANGE;
9endif
10i = 1;
11while (given > this.sines[i])
12i = i + 1;
13endwhile
14if (given == this.sines[i])
15if (args[1] < 0)
16return {-i, 0};
17else
18return {i, 0};
19endif
20endif
21degrees = i - 1;
22if (i == 1)
23lower = 0;
24else
25lower = this.sines[i - 1];
26endif
27upper = this.sines[i];
28delta1 = given - lower;
29delta2 = upper - lower;
30minutes = ((delta1 * 60) + ((delta2 + 1) / 2)) / delta2;
31if (args[1] < 0)
32degrees = -degrees;
33minutes = -minutes;
34endif
35return {degrees, minutes};

arccos acos

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"Copied from Trig_Utils (#25800):arccos by Obvious (#54879) Fri Nov 17 06:08:08 1995 PST";
2given = args[1];
3if (typeof(given) == FLOAT)
4return acos(given);
5endif
6arcsin = this:arcsin(given);
7degrees = 89 - arcsin[1];
8minutes = 60 - arcsin[2];
9if (minutes > 60)
10minutes = minutes - 60;
11degrees = degrees + 1;
12endif
13return {degrees, minutes};

arctan atan

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"Copied from Trig_Utils (#25800):arctan by Obvious (#54879) Fri Nov 17 06:08:18 1995 PST";
2given = args[1];
3if (typeof(given) == FLOAT)
4return atan(given);
5endif
6reciprocal = ((given * given) / 10000) + 10000;
7reciprocal = sqrt(reciprocal * 10000);
8cosine = 100000000 / reciprocal;
9return this:arccos(cosine);

deg2rads deg2rad

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1":deg2rad(FLOAT Degrees) => FLOAT Radians";
2return tofloat(args[1]) * 0.0174532925199433;

rads2deg rad2deg

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1":rads2deg(FLOAT Radians) => FLOAT Degrees";
2return tofloat(args[1]) * 57.2957795130824;

precision

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1":precision(FLOAT Number, INT Digits of Precision) => FLOAT Number";
2"Cuts the given number to the given digits of precision.  Uses rounding.";
3{digits, pre} = args;
4mult = 10.0 ^ pre;
5return this:rint(digits * mult) / mult;

round

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1"Usage: round(INT number, INT round)";
2"Rounds 'number' off to the nearest multiple of 'round'.";
3"Rounds UP numbers exactly half way in between two round possibilities.";
4{what, round} = args;
5low = (what / round) * round;
6return (what < (low + (round / 2))) ? low | (low + round);

mean average

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1"Usage: mean(INT, INT, ... )";
2"       mean({INT, INT, ...})";
3"Returns the average of all integers provided.";
4return this:sum(rlist = (typeof(args[1]) == LIST) ? args[1] | args) / length(rlist);

sum_float

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

none

Source

1":sum_float(FLOAT num, num, num ...) => Total of all arguments added together.";
2":sum_float({num, num, num, ...}) will also work.";
3{?total = 0.0, @rest} = args;
4if (typeof(total) == LIST)
5{?total = 0.0, @rest} = total;
6endif
7for number in (rest)
8total = total + number;
9endfor
10return total;

sum_int sum

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1":sum_int(INT num, num, num ...) => Total of all arguments added together.";
2":sum_int({num, num, num, ...}) will also work.";
3"(...also named :sum for backward compatibility).";
4"Use :sum_float to sum a list of floats.";
5{?total = 0, @rest} = args;
6if (typeof(total) == LIST)
7{?total = 0, @rest} = total;
8endif
9for number in (rest)
10total = total + number;
11endfor
12return total;
13"... N.B.  For the sake of backward compatibility this routine will also return the float sum of a non-empty lists of floats, but using it this way should not be encouraged.";

rint

Spec this none thisFlags rxdOwner #29Definer #18

Referenced by

Source

1":rint(FLOAT Number) => FLOAT Number";
2"Returns the given floating-point number rounded to the nearest integer, as a floating-point number.  In case of ties, rounds away from 0.";
3{f} = args;
4return trunc((f > 0.0) ? f + 0.5 | (f - 0.5));

gil_float_to_int

Spec this none thisFlags rxdOwner #361Definer #18

Referenced by

Source

1"Given a float, r = an int.  Use the fraction as a random chance to add 1.";
2f = args[1];
3if (typeof(f) == FLOAT)
4sign = (f >= 0.0) ? 1 | -1;
5elseif (typeof(f) == INT)
6sign = (f >= 0) ? 1 | -1;
7else
8return 1;
9endif
10f = abs(f);
11if (typeof(f) == FLOAT)
12frem = f - tofloat(fint = toint(f));
13if (random(100) < toint(frem * 100.0))
14r = fint + 1;
15else
16r = fint;
17endif
18else
19r = toint(f);
20endif
21r = max(r, 0);
22return r * sign;

approx_number

Spec this none thisFlags rxdOwner #361Definer #18

Referenced by

none

Source

1":approx_number(INT number, INT precision)";
2"For a precision of 0, return the number exactly.  For >0, approximate.";
3NUM = args[1];
4precision = args[2];
5if (precision == 0)
6return NUM;
7elseif (precision == 1)
8return (NUM - 2) + random(3);
9else
10pval = {1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000}[precision];
11NUM = (NUM / pval) * pval;
12return NUM;
13endif

sign

Spec this none thisFlags rxdOwner #361Definer #18

Referenced by

none

Source

1return (args[1] >= 0) ? 1 | -1;

add

Spec this none thisFlags rxdOwner #361Definer #18

Referenced by

none

Source

1return args[1] + args[2];

line_len

Spec this none thisFlags rxdOwner #361Definer #18

Referenced by

none

Source

1{x1, y1, x2, y2} = args;
2dist = sqrt((tofloat(x2 - x1) ^ 2.0) + (tofloat(y2 - y1) ^ 2.0));
3return toint(dist);

constrain

Spec this none thisFlags rxdOwner #361Definer #18

Referenced by

none

Source

1{n, min, max} = args;
2while (n < min)
3n = n + (max - min);
4endwhile
5while (n > max)
6n = n - (max - min);
7endwhile
8return n;

points_to_angle

Spec this none thisFlags rxdOwner #361Definer #18

Referenced by

none

Source

1{x1, y1, x2, y2} = args;
2if (x2 == x1)
3if (y2 > y1)
4theta = 0.0;
5else
6theta = 3.14159265;
7endif
8else
9theta = atan(tofloat(y2 - y1) / tofloat(x2 - x1));
10if (x2 > x1)
11theta = (3.14159265 / 2.0) - theta;
12else
13theta = (3.14159265 * 1.5) - theta;
14endif
15endif
16return mod(180 - toint(theta * 57.295779), 360);