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