Commit | Line | Data |
---|---|---|
12f1951a CW |
1 | ;;; calc-frac.el --- fraction functions for Calc |
2 | ||
bf77c646 | 3 | ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
136211a9 | 4 | |
12f1951a | 5 | ;; Author: David Gillespie <daveg@synaptics.com> |
a1506d29 | 6 | ;; Maintainers: D. Goel <deego@gnufans.org> |
6e1c888a | 7 | ;; Colin Walters <walters@debian.org> |
136211a9 | 8 | |
a2cdc7f9 PJ |
9 | ;; This file is part of GNU Emacs. |
10 | ||
136211a9 EZ |
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 | ||
12f1951a | 26 | ;;; Commentary: |
136211a9 | 27 | |
12f1951a | 28 | ;;; Code: |
136211a9 EZ |
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 | ||
136211a9 EZ |
37 | (defun calc-fdiv (arg) |
38 | (interactive "P") | |
39 | (calc-slow-wrapper | |
bf77c646 | 40 | (calc-binary-op ":" 'calcFunc-fdiv arg 1))) |
136211a9 EZ |
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) | |
bf77c646 | 53 | (prefix-numeric-value (or arg 0)))))))) |
136211a9 EZ |
54 | |
55 | ||
56 | (defun calc-over-notation (fmt) | |
12f1951a CW |
57 | (interactive |
58 | (list | |
59 | (completing-read "Fraction separator: " (mapcar (lambda (s) | |
60 | (cons s 0)) | |
61 | '(":" "::" "/" "//" ":/")) | |
62 | nil t))) | |
136211a9 EZ |
63 | (calc-wrapper |
64 | (if (string-match "\\`\\([^ 0-9][^ 0-9]?\\)[0-9]*\\'" fmt) | |
65 | (let ((n nil)) | |
66 | (if (/= (match-end 0) (match-end 1)) | |
67 | (setq n (string-to-int (substring fmt (match-end 1))) | |
68 | fmt (math-match-substring fmt 1))) | |
69 | (if (eq n 0) (error "Bad denominator")) | |
70 | (calc-change-mode 'calc-frac-format (list fmt n) t)) | |
12f1951a | 71 | (error "Bad fraction separator format")))) |
136211a9 EZ |
72 | |
73 | (defun calc-slash-notation (n) | |
74 | (interactive "P") | |
75 | (calc-wrapper | |
bf77c646 | 76 | (calc-change-mode 'calc-frac-format (if n '("//" nil) '("/" nil)) t))) |
136211a9 EZ |
77 | |
78 | ||
79 | (defun calc-frac-mode (n) | |
80 | (interactive "P") | |
81 | (calc-wrapper | |
82 | (calc-change-mode 'calc-prefer-frac n nil t) | |
83 | (message (if calc-prefer-frac | |
12f1951a CW |
84 | "Integer division will now generate fractions" |
85 | "Integer division will now generate floating-point results")))) | |
136211a9 EZ |
86 | |
87 | ||
88 | ;;;; Fractions. | |
89 | ||
90 | ;;; Build a normalized fraction. [R I I] | |
91 | ;;; (This could probably be implemented more efficiently than using | |
92 | ;;; the plain gcd algorithm.) | |
93 | (defun math-make-frac (num den) | |
94 | (if (Math-integer-negp den) | |
95 | (setq num (math-neg num) | |
96 | den (math-neg den))) | |
97 | (let ((gcd (math-gcd num den))) | |
98 | (if (eq gcd 1) | |
99 | (if (eq den 1) | |
100 | num | |
101 | (list 'frac num den)) | |
102 | (if (equal gcd den) | |
103 | (math-quotient num gcd) | |
bf77c646 | 104 | (list 'frac (math-quotient num gcd) (math-quotient den gcd)))))) |
136211a9 EZ |
105 | |
106 | (defun calc-add-fractions (a b) | |
107 | (if (eq (car-safe a) 'frac) | |
108 | (if (eq (car-safe b) 'frac) | |
109 | (math-make-frac (math-add (math-mul (nth 1 a) (nth 2 b)) | |
110 | (math-mul (nth 2 a) (nth 1 b))) | |
111 | (math-mul (nth 2 a) (nth 2 b))) | |
112 | (math-make-frac (math-add (nth 1 a) | |
113 | (math-mul (nth 2 a) b)) | |
114 | (nth 2 a))) | |
115 | (math-make-frac (math-add (math-mul a (nth 2 b)) | |
116 | (nth 1 b)) | |
bf77c646 | 117 | (nth 2 b)))) |
136211a9 EZ |
118 | |
119 | (defun calc-mul-fractions (a b) | |
120 | (if (eq (car-safe a) 'frac) | |
121 | (if (eq (car-safe b) 'frac) | |
122 | (math-make-frac (math-mul (nth 1 a) (nth 1 b)) | |
123 | (math-mul (nth 2 a) (nth 2 b))) | |
124 | (math-make-frac (math-mul (nth 1 a) b) | |
125 | (nth 2 a))) | |
126 | (math-make-frac (math-mul a (nth 1 b)) | |
bf77c646 | 127 | (nth 2 b)))) |
136211a9 EZ |
128 | |
129 | (defun calc-div-fractions (a b) | |
130 | (if (eq (car-safe a) 'frac) | |
131 | (if (eq (car-safe b) 'frac) | |
132 | (math-make-frac (math-mul (nth 1 a) (nth 2 b)) | |
133 | (math-mul (nth 2 a) (nth 1 b))) | |
134 | (math-make-frac (nth 1 a) | |
135 | (math-mul (nth 2 a) b))) | |
136 | (math-make-frac (math-mul a (nth 2 b)) | |
bf77c646 | 137 | (nth 1 b)))) |
136211a9 EZ |
138 | |
139 | ||
136211a9 EZ |
140 | ;;; Convert a real value to fractional form. [T R I; T R F] [Public] |
141 | (defun calcFunc-frac (a &optional tol) | |
142 | (or tol (setq tol 0)) | |
143 | (cond ((Math-ratp a) | |
144 | a) | |
145 | ((memq (car a) '(cplx polar vec hms date sdev intv mod)) | |
146 | (cons (car a) (mapcar (function | |
147 | (lambda (x) | |
148 | (calcFunc-frac x tol))) | |
149 | (cdr a)))) | |
150 | ((Math-messy-integerp a) | |
151 | (math-trunc a)) | |
152 | ((Math-negp a) | |
153 | (math-neg (calcFunc-frac (math-neg a) tol))) | |
154 | ((not (eq (car a) 'float)) | |
155 | (if (math-infinitep a) | |
156 | a | |
157 | (if (math-provably-integerp a) | |
158 | a | |
159 | (math-reject-arg a 'numberp)))) | |
160 | ((integerp tol) | |
161 | (if (<= tol 0) | |
162 | (setq tol (+ tol calc-internal-prec))) | |
163 | (calcFunc-frac a (list 'float 5 | |
164 | (- (+ (math-numdigs (nth 1 a)) | |
165 | (nth 2 a)) | |
166 | (1+ tol))))) | |
167 | ((not (eq (car tol) 'float)) | |
168 | (if (Math-realp tol) | |
169 | (calcFunc-frac a (math-float tol)) | |
170 | (math-reject-arg tol 'realp))) | |
171 | ((Math-negp tol) | |
172 | (calcFunc-frac a (math-neg tol))) | |
173 | ((Math-zerop tol) | |
174 | (calcFunc-frac a 0)) | |
175 | ((not (math-lessp-float tol '(float 1 0))) | |
176 | (math-trunc a)) | |
177 | ((Math-zerop a) | |
178 | 0) | |
179 | (t | |
180 | (let ((cfrac (math-continued-fraction a tol)) | |
181 | (calc-prefer-frac t)) | |
bf77c646 | 182 | (math-eval-continued-fraction cfrac))))) |
136211a9 EZ |
183 | |
184 | (defun math-continued-fraction (a tol) | |
185 | (let ((calc-internal-prec (+ calc-internal-prec 2))) | |
186 | (let ((cfrac nil) | |
187 | (aa a) | |
188 | (calc-prefer-frac nil) | |
189 | int) | |
190 | (while (or (null cfrac) | |
191 | (and (not (Math-zerop aa)) | |
192 | (not (math-lessp-float | |
193 | (math-abs | |
194 | (math-sub a | |
195 | (let ((f (math-eval-continued-fraction | |
196 | cfrac))) | |
197 | (math-working "Fractionalize" f) | |
198 | f))) | |
199 | tol)))) | |
200 | (setq int (math-trunc aa) | |
201 | aa (math-sub aa int) | |
202 | cfrac (cons int cfrac)) | |
203 | (or (Math-zerop aa) | |
204 | (setq aa (math-div 1 aa)))) | |
bf77c646 | 205 | cfrac))) |
136211a9 EZ |
206 | |
207 | (defun math-eval-continued-fraction (cf) | |
208 | (let ((n (car cf)) | |
209 | (d 1) | |
210 | temp) | |
211 | (while (setq cf (cdr cf)) | |
212 | (setq temp (math-add (math-mul (car cf) n) d) | |
213 | d n | |
214 | n temp)) | |
bf77c646 | 215 | (math-div n d))) |
136211a9 EZ |
216 | |
217 | ||
218 | ||
219 | (defun calcFunc-fdiv (a b) ; [R I I] [Public] | |
220 | (if (Math-num-integerp a) | |
221 | (if (Math-num-integerp b) | |
222 | (if (Math-zerop b) | |
223 | (math-reject-arg a "*Division by zero") | |
224 | (math-make-frac (math-trunc a) (math-trunc b))) | |
225 | (math-reject-arg b 'integerp)) | |
bf77c646 | 226 | (math-reject-arg a 'integerp))) |
136211a9 | 227 | |
ab5796a9 | 228 | ;;; arch-tag: 89d65274-0b3b-42d8-aacd-eaf86da5b4ea |
bf77c646 | 229 | ;;; calc-frac.el ends here |