Mercurial > emacs
annotate lisp/calc/calc-frac.el @ 41264:0759b2de09c1
(calc-over-notation): Use `completing-read'.
Change all toplevel `setq' forms to `defvar' forms, and move them
before their first use. Use `when', `unless'. Remove trailing
periods from error forms. Add description and headers suggested by
Emacs Lisp coding conventions.
author | Colin Walters <walters@gnu.org> |
---|---|
date | Mon, 19 Nov 2001 07:34:00 +0000 |
parents | 73f364fd8aaa |
children | 5b80d417cf80 |
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> |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
6 ;; Maintainer: Colin Walters <walters@debian.org> |
40785 | 7 |
8 ;; GNU Emacs is distributed in the hope that it will be useful, | |
9 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
10 ;; accepts responsibility to anyone for the consequences of using it | |
11 ;; or for whether it serves any particular purpose or works at all, | |
12 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
13 ;; License for full details. | |
14 | |
15 ;; Everyone is granted permission to copy, modify and redistribute | |
16 ;; GNU Emacs, but only under the conditions described in the | |
17 ;; GNU Emacs General Public License. A copy of this license is | |
18 ;; supposed to have been given to you along with GNU Emacs so you | |
19 ;; can know your rights and responsibilities. It should be in a | |
20 ;; file named COPYING. Among other things, the copyright notice | |
21 ;; and this notice must be preserved on all copies. | |
22 | |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
23 ;;; Commentary: |
40785 | 24 |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Code: |
40785 | 26 |
27 ;; This file is autoloaded from calc-ext.el. | |
28 (require 'calc-ext) | |
29 | |
30 (require 'calc-macs) | |
31 | |
32 (defun calc-Need-calc-frac () nil) | |
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) | |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
54 (interactive |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
55 (list |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
56 (completing-read "Fraction separator: " (mapcar (lambda (s) |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
57 (cons s 0)) |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
58 '(":" "::" "/" "//" ":/")) |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
59 nil t))) |
40785 | 60 (calc-wrapper |
61 (if (string-match "\\`\\([^ 0-9][^ 0-9]?\\)[0-9]*\\'" fmt) | |
62 (let ((n nil)) | |
63 (if (/= (match-end 0) (match-end 1)) | |
64 (setq n (string-to-int (substring fmt (match-end 1))) | |
65 fmt (math-match-substring fmt 1))) | |
66 (if (eq n 0) (error "Bad denominator")) | |
67 (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
|
68 (error "Bad fraction separator format")))) |
40785 | 69 |
70 (defun calc-slash-notation (n) | |
71 (interactive "P") | |
72 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
73 (calc-change-mode 'calc-frac-format (if n '("//" nil) '("/" nil)) t))) |
40785 | 74 |
75 | |
76 (defun calc-frac-mode (n) | |
77 (interactive "P") | |
78 (calc-wrapper | |
79 (calc-change-mode 'calc-prefer-frac n nil t) | |
80 (message (if calc-prefer-frac | |
41264
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
81 "Integer division will now generate fractions" |
0759b2de09c1
(calc-over-notation): Use `completing-read'.
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
82 "Integer division will now generate floating-point results")))) |
40785 | 83 |
84 | |
85 ;;;; Fractions. | |
86 | |
87 ;;; Build a normalized fraction. [R I I] | |
88 ;;; (This could probably be implemented more efficiently than using | |
89 ;;; the plain gcd algorithm.) | |
90 (defun math-make-frac (num den) | |
91 (if (Math-integer-negp den) | |
92 (setq num (math-neg num) | |
93 den (math-neg den))) | |
94 (let ((gcd (math-gcd num den))) | |
95 (if (eq gcd 1) | |
96 (if (eq den 1) | |
97 num | |
98 (list 'frac num den)) | |
99 (if (equal gcd den) | |
100 (math-quotient num gcd) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
101 (list 'frac (math-quotient num gcd) (math-quotient den gcd)))))) |
40785 | 102 |
103 (defun calc-add-fractions (a b) | |
104 (if (eq (car-safe a) 'frac) | |
105 (if (eq (car-safe b) 'frac) | |
106 (math-make-frac (math-add (math-mul (nth 1 a) (nth 2 b)) | |
107 (math-mul (nth 2 a) (nth 1 b))) | |
108 (math-mul (nth 2 a) (nth 2 b))) | |
109 (math-make-frac (math-add (nth 1 a) | |
110 (math-mul (nth 2 a) b)) | |
111 (nth 2 a))) | |
112 (math-make-frac (math-add (math-mul a (nth 2 b)) | |
113 (nth 1 b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
114 (nth 2 b)))) |
40785 | 115 |
116 (defun calc-mul-fractions (a b) | |
117 (if (eq (car-safe a) 'frac) | |
118 (if (eq (car-safe b) 'frac) | |
119 (math-make-frac (math-mul (nth 1 a) (nth 1 b)) | |
120 (math-mul (nth 2 a) (nth 2 b))) | |
121 (math-make-frac (math-mul (nth 1 a) b) | |
122 (nth 2 a))) | |
123 (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
|
124 (nth 2 b)))) |
40785 | 125 |
126 (defun calc-div-fractions (a b) | |
127 (if (eq (car-safe a) 'frac) | |
128 (if (eq (car-safe b) 'frac) | |
129 (math-make-frac (math-mul (nth 1 a) (nth 2 b)) | |
130 (math-mul (nth 2 a) (nth 1 b))) | |
131 (math-make-frac (nth 1 a) | |
132 (math-mul (nth 2 a) b))) | |
133 (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
|
134 (nth 1 b)))) |
40785 | 135 |
136 | |
137 ;;; Convert a real value to fractional form. [T R I; T R F] [Public] | |
138 (defun calcFunc-frac (a &optional tol) | |
139 (or tol (setq tol 0)) | |
140 (cond ((Math-ratp a) | |
141 a) | |
142 ((memq (car a) '(cplx polar vec hms date sdev intv mod)) | |
143 (cons (car a) (mapcar (function | |
144 (lambda (x) | |
145 (calcFunc-frac x tol))) | |
146 (cdr a)))) | |
147 ((Math-messy-integerp a) | |
148 (math-trunc a)) | |
149 ((Math-negp a) | |
150 (math-neg (calcFunc-frac (math-neg a) tol))) | |
151 ((not (eq (car a) 'float)) | |
152 (if (math-infinitep a) | |
153 a | |
154 (if (math-provably-integerp a) | |
155 a | |
156 (math-reject-arg a 'numberp)))) | |
157 ((integerp tol) | |
158 (if (<= tol 0) | |
159 (setq tol (+ tol calc-internal-prec))) | |
160 (calcFunc-frac a (list 'float 5 | |
161 (- (+ (math-numdigs (nth 1 a)) | |
162 (nth 2 a)) | |
163 (1+ tol))))) | |
164 ((not (eq (car tol) 'float)) | |
165 (if (Math-realp tol) | |
166 (calcFunc-frac a (math-float tol)) | |
167 (math-reject-arg tol 'realp))) | |
168 ((Math-negp tol) | |
169 (calcFunc-frac a (math-neg tol))) | |
170 ((Math-zerop tol) | |
171 (calcFunc-frac a 0)) | |
172 ((not (math-lessp-float tol '(float 1 0))) | |
173 (math-trunc a)) | |
174 ((Math-zerop a) | |
175 0) | |
176 (t | |
177 (let ((cfrac (math-continued-fraction a tol)) | |
178 (calc-prefer-frac t)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
179 (math-eval-continued-fraction cfrac))))) |
40785 | 180 |
181 (defun math-continued-fraction (a tol) | |
182 (let ((calc-internal-prec (+ calc-internal-prec 2))) | |
183 (let ((cfrac nil) | |
184 (aa a) | |
185 (calc-prefer-frac nil) | |
186 int) | |
187 (while (or (null cfrac) | |
188 (and (not (Math-zerop aa)) | |
189 (not (math-lessp-float | |
190 (math-abs | |
191 (math-sub a | |
192 (let ((f (math-eval-continued-fraction | |
193 cfrac))) | |
194 (math-working "Fractionalize" f) | |
195 f))) | |
196 tol)))) | |
197 (setq int (math-trunc aa) | |
198 aa (math-sub aa int) | |
199 cfrac (cons int cfrac)) | |
200 (or (Math-zerop aa) | |
201 (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
|
202 cfrac))) |
40785 | 203 |
204 (defun math-eval-continued-fraction (cf) | |
205 (let ((n (car cf)) | |
206 (d 1) | |
207 temp) | |
208 (while (setq cf (cdr cf)) | |
209 (setq temp (math-add (math-mul (car cf) n) d) | |
210 d n | |
211 n temp)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
212 (math-div n d))) |
40785 | 213 |
214 | |
215 | |
216 (defun calcFunc-fdiv (a b) ; [R I I] [Public] | |
217 (if (Math-num-integerp a) | |
218 (if (Math-num-integerp b) | |
219 (if (Math-zerop b) | |
220 (math-reject-arg a "*Division by zero") | |
221 (math-make-frac (math-trunc a) (math-trunc b))) | |
222 (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
|
223 (math-reject-arg a 'integerp))) |
40785 | 224 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
225 ;;; calc-frac.el ends here |