2 The config function sets or reads the value of a configuration
3 parameter. The first argument is a string which names the parameter
4 to be set or read. If only one argument is given, then the current
5 value of the named parameter is returned. If two arguments are given,
6 then the named parameter is set to the value of the second argument,
7 and the old value of the parameter is returned. Therefore you can
8 change a parameter and restore its old value later. The possible
9 parameters are explained in the next section.
11 The scale function multiplies or divides a number by a power of 2.
12 This is used for fractional calculations, unlike the << and >>
13 operators, which are only defined for integers. For example,
16 The quomod function is used to obtain both the quotient and remainder
17 of a division in one operation. The first two arguments a and b are
18 the numbers to be divided. The last two arguments c and d are two
19 variables which will be assigned the quotient and remainder. For
20 nonnegative arguments, the results are equivalent to computing a//b
21 and a%b. If a is negative and the remainder is nonzero, then the
22 quotient will be one less than a//b. This makes the following three
23 properties always hold: The quotient c is always an integer. The
24 remainder d is always 0 <= d < b. The equation a = b * c + d always
25 holds. This function returns 0 if there is no remainder, and 1 if
26 there is a remainder. For examples, quomod(10, 3, x, y) sets x to 3,
27 y to 1, and returns the value 1, and quomod(-4, 3.14159, x, y) sets x
28 to -2, y to 2.28318, and returns the value 1.
30 The eval function accepts a string argument and evaluates the
31 expression represented by the string and returns its value.
32 The expression can include function calls and variable references.
33 For example, eval("fact(3) + 7") returns 13. When combined with
34 the prompt function, this allows the calculator to read values from
35 the user. For example, x=eval(prompt("Number: ")) sets x to the
36 value input by the user.
38 The digit and bit functions return individual digits of a number,
39 either in base 10 or in base 2, where the lowest digit of a number
40 is at digit position 0. For example, digit(5678, 3) is 5, and
41 bit(0b1000100, 2) is 1. Negative digit positions indicate places
42 to the right of the decimal or binary point, so that for example,
43 digit(3.456, -1) is 4.
45 The ptest builtin is a primality testing function. The
46 1st argument is the suspected prime to be tested. The
47 absolute value of the 2nd argument is an iteration count.
49 If ptest is called with only 2 args, the 3rd argument is
50 assumed to be 0. If ptest is called with only 1 arg, the
51 2nd argument is assumed to be 1. Thus, the following
58 Normally ptest performs a some checks to determine if the
59 value is divisable by some trivial prime. If the 2nd
60 argument is < 0, then the trivial check is omitted.
62 For example, ptest(a,10) performs the same work as:
64 ptest(a,-3) (7 tests without trivial check)
65 ptest(a,-7,3) (3 more tests without the trivial check)
67 The ptest function returns 0 if the number is definitely not
68 prime, and 1 is the number is probably prime. The chance
69 of a number which is probably prime being actually composite
70 is less than 1/4 raised to the power of the iteration count.
71 For example, for a random number p, ptest(p, 10) incorrectly
72 returns 1 less than once in every million numbers, and you
73 will probably never find a number where ptest(p, 20) gives
76 The first 3 args of nextcand and prevcand functions are the same
77 arguments as ptest. But unlike ptest, nextcand and prevcand return
78 the next and previous values for which ptest is true.
80 For example, nextcand(2^1000) returns 2^1000+297 because
81 2^1000+297 is the smallest value x > 2^1000 for which
82 ptest(x,1) is true. And for example, prevcand(2^31-1,10,5)
83 returns 2147483629 (2^31-19) because 2^31-19 is the largest
84 value y < 2^31-1 for which ptest(y,10,5) is true.
86 The nextcand and prevcand functions also have a 5 argument form:
88 nextcand(num, count, skip, modval, modulus)
89 prevcand(num, count, skip, modval, modulus)
91 return the smallest (or largest) value ans > num (or < num) that
92 is also == modval % modulus for which ptest(ans,count,skip) is true.
94 The builtins nextprime(x) and prevprime(x) return the
95 next and previous primes with respect to x respectively.
96 As of this release, x must be < 2^32. With one argument, they
97 will return an error if x is out of range. With two arguments,
98 they will not generate an error but instead will return y.
100 The builtin function pix(x) returns the number of primes <= x.
101 As of this release, x must be < 2^32. With one argument, pix(x)
102 will return an error if x is out of range. With two arguments,
103 pix(x,y) will not generate an error but instead will return y.
105 The builtin function factor may be used to search for the
106 smallest factor of a given number. The call factor(x,y)
107 will attempt to find the smallest factor of x < min(x,y).
108 As of this release, y must be < 2^32. If y is omitted, y
109 is assumed to be 2^32-1.
111 If x < 0, factor(x,y) will return -1. If no factor <
112 min(x,y) is found, factor(x,y) will return 1. In all other
113 cases, factor(x,y) will return the smallest prime factor
114 of x. Note except for the case when abs(x) == 1, factor(x,y)
117 If factor is called with y that is too large, or if x or y
118 is not an integer, calc will report an error. If a 3rd argument
119 is given, factor will return that value instead. For example,
120 factor(1/2,b,c) will return c instead of issuing an error.
122 The builtin lfactor(x,y) searches a number of primes instead
123 of below a limit. As of this release, y must be <= 203280221
124 (y <= pix(2^32-1)). In all other cases, lfactor is operates
125 in the same way as factor.
127 If lfactor is called with y that is too large, or if x or y
128 is not an integer, calc will report an error. If a 3rd argument
129 is given, lfactor will return that value instead. For example,
130 lfactor(1/2,b,c) will return c instead of issuing an error.
132 The lfactor function is slower than factor. If possible factor
133 should be used instead of lfactor.
135 The builtin isprime(x) will attempt to determine if x is prime.
136 As of this release, x must be < 2^32. With one argument, isprime(x)
137 will return an error if x is out of range. With two arguments,
138 isprime(x,y) will not generate an error but instead will return y.
140 The functions rcin, rcmul, rcout, rcpow, and rcsq are used to
141 perform modular arithmetic calculations for large odd numbers
142 faster than the usual methods. To do this, you first use the
143 rcin function to convert all input values into numbers which are
144 in a format called REDC format. Then you use rcmul, rcsq, and
145 rcpow to multiply such numbers together to produce results also
146 in REDC format. Finally, you use rcout to convert a number in
147 REDC format back to a normal number. The addition, subtraction,
148 negation, and equality comparison between REDC numbers are done
149 using the normal modular methods. For example, to calculate the
150 value 13 * 17 + 1 (mod 11), you could use:
156 t4 = rcmul(t1, t2, p);
158 answer = rcout(t5, p);
160 The swap function exchanges the values of two variables without
161 performing copies. For example, after:
167 then x is 19 and y is 17. This function should not be used to
168 swap a value which is contained within another one. If this is
169 done, then some memory will be lost. For example, the following
175 The hash function returns a relatively small non-negative integer
176 for one or more input values. The hash values should not be used
177 across runs of the calculator, since the algorithms used to generate
178 the hash value may change with different versions of the calculator.
180 The base function allows one to specify how numbers should be
181 printed. The base function provides a numeric shorthand to the
182 config("mode") interface. With no args, base() will return the
183 current mode. With 1 arg, base(val) will set the mode according to
184 the arg and return the previous mode.
186 The following convention is used to declare modes:
191 2 "binary" binary fractions
192 8 "octal" octal fractions
193 10 "real" decimal floating point
194 16 "hex" hexadecimal fractions
195 -10 "int" decimal integer
196 1/3 "frac" decimal fractions
197 1e20 "exp" decimal exponential
199 For convenience, any non-integer value is assumed to mean "frac",
200 and any integer >= 2^64 is assumed to mean "exp".
202 ## Copyright (C) 1999-2007 Landon Curt Noll
204 ## Calc is open software; you can redistribute it and/or modify it under
205 ## the terms of the version 2.1 of the GNU Lesser General Public License
206 ## as published by the Free Software Foundation.
208 ## Calc is distributed in the hope that it will be useful, but WITHOUT
209 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
210 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
211 ## Public License for more details.
213 ## A copy of version 2.1 of the GNU Lesser General Public License is
214 ## distributed with calc under the filename COPYING-LGPL. You should have
215 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
216 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
218 ## @(#) $Revision: 30.1 $
219 ## @(#) $Id: builtin.end,v 30.1 2007/03/16 11:10:42 chongo Exp $
220 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/builtin.end,v $
222 ## Under source code control: 1995/07/10 01:17:53
223 ## File existed as early as: 1995
225 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
226 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/