14 obscure.ops - Obscure Mathematical Opcodes
20 Parrot's library of obscure mathematical ops. These turn common
21 trig expressions into a single op.
23 To use this library of ops, add this directive to your PIR:
25 .loadlib 'obscure_ops'
30 ###############################################################################
32 =head2 Obscure trigonometric operations
36 Abramowitz, M. and Stegum, C. A. (Eds.). Handbook of Mathematical
37 Functions with Formulas, Graphs, and Mathematical Tables, 9th printing.
38 New York: Dover, p. 78, 1972.
45 ########################################
47 =item B<covers>(out NUM, in NUM)
49 Set $1 to the coversine (in radians) of $2.
53 inline op covers(out NUM, in NUM) :advanced_math {
58 ########################################
60 =item B<exsec>(out NUM, in NUM)
62 Set $1 to the exsecant of $2 (given in radians).
67 inline op exsec(out NUM, in NUM) :advanced_math {
68 $1 = (1.0 / cos($2)) - 1.0;
72 ########################################
74 =item B<hav>(out NUM, in NUM)
76 Set $1 to the haversine (in radians) of $2.
80 inline op hav(out NUM, in NUM) {
81 $1 = 0.5 * (1.0 - cos($2));
85 ########################################
87 =item B<vers>(out NUM, in NUM)
89 Set $1 to the versine (in radians) of $2.
93 inline op vers(out NUM, in NUM) :advanced_math {
97 ########################################
99 =item B<gcd>(out INT, in INT, in INT)
101 Greatest Common divisor of $2 and $3.
105 inline op gcd(out INT, in INT, in INT) :advanced_math {
107 INTVAL a = $2 < 0 ? -$2 : $2;
108 INTVAL b = $3 < 0 ? -$3 : $3;
110 if (a==0) { $1=b; goto NEXT(); }
111 if (b==0) { $1=a; goto NEXT(); }
113 while (!((a | b) & 1)) {
121 else if (!(b & 1)) b>>=1;
122 else if (a<b) b = (b-a)>>1;
130 ########################################
132 =item B<lcm>(out INT, in INT, in INT)
134 Least Common Multiple of $2 and $3
138 inline op lcm(out INT, in INT, in INT) :advanced_math {
141 INTVAL a = $2 < 0 ? -$2 : $2;
142 INTVAL b = $3 < 0 ? -$3 : $3;
143 INTVAL saved_var1 = a, saved_var2 = b;
145 if (a==0 || b==0) { $1=0; goto NEXT(); }
147 while (!((a | b) & 1)) {
155 else if (!(b & 1)) b>>=1;
156 else if (a<b) b = (b-a)>>1;
162 $1 = saved_var1*saved_var2;
165 ########################################
167 =item B<fact>(out INT, in INT)
169 =item B<fact>(out NUM, in INT)
171 Factorial, n!. Calculates the product of 1 to N.
175 inline op fact(out INT, in INT) :advanced_math {
176 /* Coercing a negative to a UINT can get pretty ugly
177 * in this situation. */
187 inline op fact(out NUM, in INT) :advanced_math {
188 /* Coercing a negative to a UINT can get pretty ugly
189 * in this situation. */
206 ###############################################################################
210 Copyright (C) 2001-2009, Parrot Foundation.
214 This program is free software. It is subject to the same license
215 as the Parrot interpreter itself.
222 * c-file-style: "parrot"
224 * vim: expandtab shiftwidth=4: