Mercurial > emacs
annotate lisp/calc/calc-frac.el @ 59667:308beae8def9
2005-01-21 Ren?yllingstad <listmailxemacs@kyllingstad.com>
* pcomplete.el: define pcomplete-read-event instead of read-event,
since it's not a complete read-event implementation
author | John Wiegley <johnw@newartisans.com> |
---|---|
date | Fri, 21 Jan 2005 10:26:05 +0000 |
parents | 4683e1bb5445 |
children | e0224a91347d f2ebccfa87d4 |
rev | line source |
---|---|
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calc-frac.el --- fraction functions for Calc |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
40785 | 4 |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
58655
4683e1bb5445
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
57848
diff
changeset
|
6 ;; Maintainer: Jay Belanger <belanger@truman.edu> |
40785 | 7 |
41384
5b80d417cf80
"This file is part of GNU Emacs." added.
Pavel Janík <Pavel@Janik.cz>
parents:
41264
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
5b80d417cf80
"This file is part of GNU Emacs." added.
Pavel Janík <Pavel@Janik.cz>
parents:
41264
diff
changeset
|
9 |
40785 | 10 ;; GNU Emacs is distributed in the hope that it will be useful, |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Commentary: |
40785 | 26 |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
27 ;;; Code: |
40785 | 28 |
29 ;; This file is autoloaded from calc-ext.el. | |
58655
4683e1bb5445
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
57848
diff
changeset
|
30 |
40785 | 31 (require 'calc-ext) |
32 (require 'calc-macs) | |
33 | |
34 (defun calc-fdiv (arg) | |
35 (interactive "P") | |
36 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
37 (calc-binary-op ":" 'calcFunc-fdiv arg 1))) |
40785 | 38 |
39 | |
40 (defun calc-fraction (arg) | |
41 (interactive "P") | |
42 (calc-slow-wrapper | |
43 (let ((func (if (calc-is-hyperbolic) 'calcFunc-frac 'calcFunc-pfrac))) | |
44 (if (eq arg 0) | |
45 (calc-enter-result 2 "frac" (list func | |
46 (calc-top-n 2) | |
47 (calc-top-n 1))) | |
48 (calc-enter-result 1 "frac" (list func | |
49 (calc-top-n 1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
50 (prefix-numeric-value (or arg 0)))))))) |
40785 | 51 |
52 | |
53 (defun calc-over-notation (fmt) | |
57848
28443b8e3b48
(calc-over-notation): Replaced `completing-read' with
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
54 (interactive "sFraction separator: ") |
40785 | 55 (calc-wrapper |
56 (if (string-match "\\`\\([^ 0-9][^ 0-9]?\\)[0-9]*\\'" fmt) | |
57 (let ((n nil)) | |
58 (if (/= (match-end 0) (match-end 1)) | |
59 (setq n (string-to-int (substring fmt (match-end 1))) | |
60 fmt (math-match-substring fmt 1))) | |
61 (if (eq n 0) (error "Bad denominator")) | |
62 (calc-change-mode 'calc-frac-format (list fmt n) t)) | |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
63 (error "Bad fraction separator format")))) |
40785 | 64 |
65 (defun calc-slash-notation (n) | |
66 (interactive "P") | |
67 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
68 (calc-change-mode 'calc-frac-format (if n '("//" nil) '("/" nil)) t))) |
40785 | 69 |
70 | |
71 (defun calc-frac-mode (n) | |
72 (interactive "P") | |
73 (calc-wrapper | |
74 (calc-change-mode 'calc-prefer-frac n nil t) | |
75 (message (if calc-prefer-frac | |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
76 "Integer division will now generate fractions" |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
77 "Integer division will now generate floating-point results")))) |
40785 | 78 |
79 | |
80 ;;;; Fractions. | |
81 | |
82 ;;; Build a normalized fraction. [R I I] | |
83 ;;; (This could probably be implemented more efficiently than using | |
84 ;;; the plain gcd algorithm.) | |
85 (defun math-make-frac (num den) | |
86 (if (Math-integer-negp den) | |
87 (setq num (math-neg num) | |
88 den (math-neg den))) | |
89 (let ((gcd (math-gcd num den))) | |
90 (if (eq gcd 1) | |
91 (if (eq den 1) | |
92 num | |
93 (list 'frac num den)) | |
94 (if (equal gcd den) | |
95 (math-quotient num gcd) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
96 (list 'frac (math-quotient num gcd) (math-quotient den gcd)))))) |
40785 | 97 |
98 (defun calc-add-fractions (a b) | |
99 (if (eq (car-safe a) 'frac) | |
100 (if (eq (car-safe b) 'frac) | |
101 (math-make-frac (math-add (math-mul (nth 1 a) (nth 2 b)) | |
102 (math-mul (nth 2 a) (nth 1 b))) | |
103 (math-mul (nth 2 a) (nth 2 b))) | |
104 (math-make-frac (math-add (nth 1 a) | |
105 (math-mul (nth 2 a) b)) | |
106 (nth 2 a))) | |
107 (math-make-frac (math-add (math-mul a (nth 2 b)) | |
108 (nth 1 b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
109 (nth 2 b)))) |
40785 | 110 |
111 (defun calc-mul-fractions (a b) | |
112 (if (eq (car-safe a) 'frac) | |
113 (if (eq (car-safe b) 'frac) | |
114 (math-make-frac (math-mul (nth 1 a) (nth 1 b)) | |
115 (math-mul (nth 2 a) (nth 2 b))) | |
116 (math-make-frac (math-mul (nth 1 a) b) | |
117 (nth 2 a))) | |
118 (math-make-frac (math-mul a (nth 1 b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
119 (nth 2 b)))) |
40785 | 120 |
121 (defun calc-div-fractions (a b) | |
122 (if (eq (car-safe a) 'frac) | |
123 (if (eq (car-safe b) 'frac) | |
124 (math-make-frac (math-mul (nth 1 a) (nth 2 b)) | |
125 (math-mul (nth 2 a) (nth 1 b))) | |
126 (math-make-frac (nth 1 a) | |
127 (math-mul (nth 2 a) b))) | |
128 (math-make-frac (math-mul a (nth 2 b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
129 (nth 1 b)))) |
40785 | 130 |
131 | |
132 ;;; Convert a real value to fractional form. [T R I; T R F] [Public] | |
133 (defun calcFunc-frac (a &optional tol) | |
134 (or tol (setq tol 0)) | |
135 (cond ((Math-ratp a) | |
136 a) | |
137 ((memq (car a) '(cplx polar vec hms date sdev intv mod)) | |
138 (cons (car a) (mapcar (function | |
139 (lambda (x) | |
140 (calcFunc-frac x tol))) | |
141 (cdr a)))) | |
142 ((Math-messy-integerp a) | |
143 (math-trunc a)) | |
144 ((Math-negp a) | |
145 (math-neg (calcFunc-frac (math-neg a) tol))) | |
146 ((not (eq (car a) 'float)) | |
147 (if (math-infinitep a) | |
148 a | |
149 (if (math-provably-integerp a) | |
150 a | |
151 (math-reject-arg a 'numberp)))) | |
152 ((integerp tol) | |
153 (if (<= tol 0) | |
154 (setq tol (+ tol calc-internal-prec))) | |
155 (calcFunc-frac a (list 'float 5 | |
156 (- (+ (math-numdigs (nth 1 a)) | |
157 (nth 2 a)) | |
158 (1+ tol))))) | |
159 ((not (eq (car tol) 'float)) | |
160 (if (Math-realp tol) | |
161 (calcFunc-frac a (math-float tol)) | |
162 (math-reject-arg tol 'realp))) | |
163 ((Math-negp tol) | |
164 (calcFunc-frac a (math-neg tol))) | |
165 ((Math-zerop tol) | |
166 (calcFunc-frac a 0)) | |
167 ((not (math-lessp-float tol '(float 1 0))) | |
168 (math-trunc a)) | |
169 ((Math-zerop a) | |
170 0) | |
171 (t | |
172 (let ((cfrac (math-continued-fraction a tol)) | |
173 (calc-prefer-frac t)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
174 (math-eval-continued-fraction cfrac))))) |
40785 | 175 |
176 (defun math-continued-fraction (a tol) | |
177 (let ((calc-internal-prec (+ calc-internal-prec 2))) | |
178 (let ((cfrac nil) | |
179 (aa a) | |
180 (calc-prefer-frac nil) | |
181 int) | |
182 (while (or (null cfrac) | |
183 (and (not (Math-zerop aa)) | |
184 (not (math-lessp-float | |
185 (math-abs | |
186 (math-sub a | |
187 (let ((f (math-eval-continued-fraction | |
188 cfrac))) | |
189 (math-working "Fractionalize" f) | |
190 f))) | |
191 tol)))) | |
192 (setq int (math-trunc aa) | |
193 aa (math-sub aa int) | |
194 cfrac (cons int cfrac)) | |
195 (or (Math-zerop aa) | |
196 (setq aa (math-div 1 aa)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
197 cfrac))) |
40785 | 198 |
199 (defun math-eval-continued-fraction (cf) | |
200 (let ((n (car cf)) | |
201 (d 1) | |
202 temp) | |
203 (while (setq cf (cdr cf)) | |
204 (setq temp (math-add (math-mul (car cf) n) d) | |
205 d n | |
206 n temp)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
207 (math-div n d))) |
40785 | 208 |
209 | |
210 | |
211 (defun calcFunc-fdiv (a b) ; [R I I] [Public] | |
212 (if (Math-num-integerp a) | |
213 (if (Math-num-integerp b) | |
214 (if (Math-zerop b) | |
215 (math-reject-arg a "*Division by zero") | |
216 (math-make-frac (math-trunc a) (math-trunc b))) | |
217 (math-reject-arg b 'integerp)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
218 (math-reject-arg a 'integerp))) |
40785 | 219 |
58655
4683e1bb5445
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
57848
diff
changeset
|
220 (provide 'calc-frac) |
4683e1bb5445
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
57848
diff
changeset
|
221 |
52401 | 222 ;;; arch-tag: 89d65274-0b3b-42d8-aacd-eaf86da5b4ea |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
223 ;;; calc-frac.el ends here |