Math Utilities #26

Parent #79Owner #36Flags readSource RPG Core/rpgcore-patched.db

Aliases: Math Utilities, Math_Utils, trigonometric utilites, trig_utils

37 verbs · 13 properties · 0 children

Verbs

VerbSpecFlagsDefinerLines
xsinthis none thisrxd#269
xcosthis none thisrxd#269
factorialthis none thisrxd#2611
powthis none thisrxd#2622
fibonaccithis none thisrxd#2615
geometricthis none thisrxd#2613
divmodthis none thisrxd#269
combinationsthis none thisrxd#2618
permutationsthis none thisrxd#2616
simpsonthis none thisrxd#2612
partsthis none thisrxd#2610
sqrtthis none thisrxd#2615
divthis none thisrxd#262
modthis none thisrxd#267
expthis none thisrxd#2615
aexpthis none thisrxd#2626
randomthis none thisrxd#268
random_rangethis none thisrxd#268
is_primethis none thisrxd#2622
AND XORthis none thisrxd#2625
ORthis none thisrxd#261
NOTthis none thisrxd#269
BLFromIntthis none thisrxd#2614
IntFromBLthis none thisrxd#268
gcd greatest_common_divisorthis none thisrxd#2616
lcm least_common_multiplethis none thisrxd#266
are_rel_prime are_relatively_primethis none thisrxd#268
base_conversionthis none thisrxd#2644
normthis none thisrxd#2618
sumthis none thisrxd#267
sinthis none thisrxd#2628
costhis none thisrxd#2614
tanthis none thisrxd#268
arcsin asinthis none thisrxd#2635
arccos acosthis none thisrxd#2613
arctan atanthis none thisrxd#269
roundthis none thisrxd#265

Properties

PropertyDefinerFlagsOwnerValue
base_alphabet#26rc#36"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
tangents#26rc#36
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#26rc#3610000
taylor#26rc#36100
and#26rc#36
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#26rc#36
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#26rc#36
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}
help_msg#79rc#36
list of 57{"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}", " round(x) -- integer from float, rounded up or down from the x.5 boundary", "", "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"}
key#1c#36<clear>
aliases#1rc#36{"Math Utilities", "Math_Utils", "trigonometric utilites", "trig_utils"}
description#1rc#36{"This is the Math Utilities utility package. See `help $math_utils' for more details."}
object_size#1r#36{42067, -1090650497}
html#1rc#36<clear>

Ancestry

Ancestors (nearest first): #79 Generic Utilities Package#1 Root Class

Children: none

Call graph

calls n26_0 #26:xsin n26_0->n26_0 n26_1 #26:xcos n26_0->n26_1 n26_1->n26_0 n26_1->n26_1 n26_9 #26:simpson n26_10 #26:parts n26_9->n26_10 n26_12 #26:div n26_6 #26:divmod n26_12->n26_6 n26_14 #26:exp n26_14->n26_10 n26_17 #26:random_range n26_16 #26:random n26_17->n26_16 n26_20 #26:OR n26_21 #26:NOT n26_20->n26_21 n26_19 #26:AND n26_20->n26_19 n26_22 #26:BLFromInt n26_21->n26_22 n26_23 #26:IntFromBL n26_21->n26_23 n26_25 #26:lcm n26_24 #26:gcd n26_25->n26_24 n26_26 #26:are_rel_prime n26_26->n26_24 n26_31 #26:cos n26_30 #26:sin n26_31->n26_30 n26_32 #26:tan n26_32->n26_31 n26_32->n26_30 n26_34 #26:arccos n26_33 #26:arcsin n26_34->n26_33 n26_35 #26:arctan n26_35->n26_34

Source

xsin

Spec this none thisFlags rxdOwner #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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) != INT)
4return E_TYPE;
5elseif ((n = args[1]) < 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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

Referenced by

none

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

Referenced by

none

Source

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

NOT

Spec this none thisFlags rxdOwner #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

Referenced by

none

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 #36Definer #26

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

sum

Spec this none thisFlags rxdOwner #36Definer #26

Referenced by

Source

1":sum(INT|FLOAT num, num, num ...) => Total of all arguments added together.";
2":sum({num, num, num, ...}) will also work.";
3total = (typeof((typeof(x = args[1]) == LIST) ? x[1] | x) == INT) ? 0 | 0.0;
4for number in ((typeof(x) == LIST) ? x | args)
5total = total + number;
6endfor
7return total;

sin

Spec this none thisFlags rxdOwner #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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 #36Definer #26

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);

round

Spec this none thisFlags rxdOwner #2Definer #26

Referenced by

Source

1"returns integer from float, rounded up or down from the x.5 boundary";
2fnum = args[1];
3extra = fnum - tofloat(toint(fnum));
4posneg = (fnum > 0.0) ? 1 | -1;
5return (abs(extra) >= 0.5) ? toint(fnum) + posneg | toint(fnum);