Merge remote-tracking branch 'origin/master' into for-steve
[sxemacs] / lisp / number.el
1 ;;; number.el -- Enhanced Number Types for SXEmacs
2
3 ;; Copyright (C) 2005 by Sebastian Freundt
4
5 ;; Author: Sebastian Freundt <hroptatyr@sxemacs.org
6 ;; Created: Fri Nov  4 16:09:10 2005 UTC
7 ;; Keywords: lisp, number
8
9 ;; This file is part of SXEmacs.
10
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Synched up with: Not in FSF
25
26 ;;; Comments:
27 ;; This file provides some additional functionality not worth
28 ;; implementing in C.
29 ;;
30 ;; Note to myself: number.el is a stupid name :(
31
32 \f
33 (defun coprimep (number1 number2 &rest numbers)
34   "Return non-nil iff the arguments are coprime, nil otherwise.
35 Numbers are coprime if their gcd is 1."
36   (= 1 (apply #'gcd number1 number2 numbers)))
37 (defalias 'relatively-prime-p #'coprimep)
38
39 (defun ^-1 (number)
40   "Return the reciprocal of NUMBER."
41   (// number))
42
43 (defun positivep (number)
44   "Return t if OBJECT is a positive number.
45
46 We call a number object positive iff it is comparable
47 and it is nonnegative and not a zero."
48   (and (nonnegativep number)
49        (not (zerop number))))
50
51 \f
52 ;; coercion abbrevs
53 (defsubst int (number &optional precision)
54   "Return the ordinary integer numerically equal to NUMBER.
55 The optional argument PRECISION is unused.
56
57 This is a convenience function analogous to `float'."
58   (coerce-number number 'int precision))
59
60 (defsubst bigz (number &optional precision)
61   "Return the MPZ number numerically equal to NUMBER.
62 The optional argument PRECISION is unused.
63
64 This is a convenience function analogous to `float'."
65   (coerce-number number 'bigz precision))
66
67 (defsubst bigq (number &optional precision)
68   "Return the MPQ number numerically equal to NUMBER.
69 The optional argument PRECISION is unused.
70
71 This is a convenience function analogous to `float'."
72   (coerce-number number 'bigq precision))
73
74 (defsubst rational (number &optional precision)
75   "Return a rational most suitable to represent NUMBER.
76 The optional argument PRECISION is unused.
77
78 This is a convenience function analogous to `float'."
79   (coerce-number number 'rational precision))
80
81
82 ;;(defun float ...)
83 ;; already implemented
84
85 (defsubst bigf (number &optional precision)
86   "Return the MPF number numerically equal to NUMBER.
87 If optional argument PRECISION is non-nil, its value
88 \(an integer\) is used as precision.
89
90 This is a convenience function analogous to `float'."
91   (coerce-number number 'bigf precision))
92
93 (defsubst bigfr (number &optional precision)
94   "Return the MPFR number numerically equal to NUMBER.
95 If optional argument PRECISION is non-nil, its value
96 \(an integer\) is used as precision.
97
98 This is a convenience function analogous to `float'."
99   (coerce-number number 'bigfr precision))
100
101 (defsubst real (number &optional precision)
102   "Return a real with respect to `read-real-as' numerically
103 equal to NUMBER.
104 If optional argument PRECISION is non-nil, its value
105 \(an integer\) is used as precision.
106
107 This is a convenience function analogous to `float'."
108   (coerce-number number 'real precision))
109
110 (defsubst bigg (number &optional precision)
111   "Return the Gaussian number numerically equal to NUMBER.
112 The optional argument PRECISION is unused.
113
114 This is a convenience function analogous to `float'."
115   (coerce-number number 'bigg precision))
116
117 (defsubst bigc (number &optional precision)
118   "Return the MPC number numerically equal to NUMBER.
119 If optional argument PRECISION is non-nil, its value
120 \(an integer\) is used as precision.
121
122 This is a convenience function analogous to `float'."
123   (coerce-number number 'bigc precision))
124
125 (defsubst quatern (number &optional precision)
126   "Return the MPC number numerically equal to NUMBER.
127 If optional argument PRECISION is non-nil, its value
128 \(an integer\) is used as precision.
129
130 This is a convenience function analogous to `float'."
131   (coerce-number number 'quatern precision))
132 \f
133 ;;; dealing with norms, valuations and such things
134 (defun canonical-valuation (number)
135   "Return the canonical valuation of NUMBER."
136   (cond ((archimedeanp number)
137          (abs number))))
138
139 \f
140 (provide 'number)
141
142 ;;; number.el ends here