]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-frac.el
Style cleanup; don't put closing parens on their
[gnu-emacs] / lisp / calc / calc-frac.el
1 ;; Calculator for GNU Emacs, part II [calc-frac.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
13
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
21
22
23
24 ;; This file is autoloaded from calc-ext.el.
25 (require 'calc-ext)
26
27 (require 'calc-macs)
28
29 (defun calc-Need-calc-frac () nil)
30
31
32 (defun calc-fdiv (arg)
33 (interactive "P")
34 (calc-slow-wrapper
35 (calc-binary-op ":" 'calcFunc-fdiv arg 1)))
36
37
38 (defun calc-fraction (arg)
39 (interactive "P")
40 (calc-slow-wrapper
41 (let ((func (if (calc-is-hyperbolic) 'calcFunc-frac 'calcFunc-pfrac)))
42 (if (eq arg 0)
43 (calc-enter-result 2 "frac" (list func
44 (calc-top-n 2)
45 (calc-top-n 1)))
46 (calc-enter-result 1 "frac" (list func
47 (calc-top-n 1)
48 (prefix-numeric-value (or arg 0))))))))
49
50
51 (defun calc-over-notation (fmt)
52 (interactive "sFraction separator (:, ::, /, //, :/): ")
53 (calc-wrapper
54 (if (string-match "\\`\\([^ 0-9][^ 0-9]?\\)[0-9]*\\'" fmt)
55 (let ((n nil))
56 (if (/= (match-end 0) (match-end 1))
57 (setq n (string-to-int (substring fmt (match-end 1)))
58 fmt (math-match-substring fmt 1)))
59 (if (eq n 0) (error "Bad denominator"))
60 (calc-change-mode 'calc-frac-format (list fmt n) t))
61 (error "Bad fraction separator format."))))
62
63 (defun calc-slash-notation (n)
64 (interactive "P")
65 (calc-wrapper
66 (calc-change-mode 'calc-frac-format (if n '("//" nil) '("/" nil)) t)))
67
68
69 (defun calc-frac-mode (n)
70 (interactive "P")
71 (calc-wrapper
72 (calc-change-mode 'calc-prefer-frac n nil t)
73 (message (if calc-prefer-frac
74 "Integer division will now generate fractions."
75 "Integer division will now generate floating-point results."))))
76
77
78
79
80
81 ;;;; Fractions.
82
83 ;;; Build a normalized fraction. [R I I]
84 ;;; (This could probably be implemented more efficiently than using
85 ;;; the plain gcd algorithm.)
86 (defun math-make-frac (num den)
87 (if (Math-integer-negp den)
88 (setq num (math-neg num)
89 den (math-neg den)))
90 (let ((gcd (math-gcd num den)))
91 (if (eq gcd 1)
92 (if (eq den 1)
93 num
94 (list 'frac num den))
95 (if (equal gcd den)
96 (math-quotient num gcd)
97 (list 'frac (math-quotient num gcd) (math-quotient den gcd))))))
98
99 (defun calc-add-fractions (a b)
100 (if (eq (car-safe a) 'frac)
101 (if (eq (car-safe b) 'frac)
102 (math-make-frac (math-add (math-mul (nth 1 a) (nth 2 b))
103 (math-mul (nth 2 a) (nth 1 b)))
104 (math-mul (nth 2 a) (nth 2 b)))
105 (math-make-frac (math-add (nth 1 a)
106 (math-mul (nth 2 a) b))
107 (nth 2 a)))
108 (math-make-frac (math-add (math-mul a (nth 2 b))
109 (nth 1 b))
110 (nth 2 b))))
111
112 (defun calc-mul-fractions (a b)
113 (if (eq (car-safe a) 'frac)
114 (if (eq (car-safe b) 'frac)
115 (math-make-frac (math-mul (nth 1 a) (nth 1 b))
116 (math-mul (nth 2 a) (nth 2 b)))
117 (math-make-frac (math-mul (nth 1 a) b)
118 (nth 2 a)))
119 (math-make-frac (math-mul a (nth 1 b))
120 (nth 2 b))))
121
122 (defun calc-div-fractions (a b)
123 (if (eq (car-safe a) 'frac)
124 (if (eq (car-safe b) 'frac)
125 (math-make-frac (math-mul (nth 1 a) (nth 2 b))
126 (math-mul (nth 2 a) (nth 1 b)))
127 (math-make-frac (nth 1 a)
128 (math-mul (nth 2 a) b)))
129 (math-make-frac (math-mul a (nth 2 b))
130 (nth 1 b))))
131
132
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))
177 (math-eval-continued-fraction cfrac)))))
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))))
200 cfrac)))
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))
210 (math-div n d)))
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))
221 (math-reject-arg a 'integerp)))
222
223 ;;; calc-frac.el ends here