* scroll.c (scroll_cost): Remove; unused.
[emacs.git] / lisp / calc / calc-frac.el
blob30894b406b5887acc517ff4f3551aa913b26f9f0
1 ;;; calc-frac.el --- fraction functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 ;; This file is autoloaded from calc-ext.el.
29 (require 'calc-ext)
30 (require 'calc-macs)
32 (defun calc-fdiv (arg)
33 (interactive "P")
34 (calc-slow-wrapper
35 (calc-binary-op ":" 'calcFunc-fdiv arg 1)))
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))))))))
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-number (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"))))
63 (defun calc-slash-notation (n)
64 (interactive "P")
65 (calc-wrapper
66 (calc-change-mode 'calc-frac-format (if n '("//" nil) '("/" nil)) t)))
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"))))
78 ;;;; Fractions.
80 ;;; Build a normalized fraction. [R I I]
81 ;;; (This could probably be implemented more efficiently than using
82 ;;; the plain gcd algorithm.)
83 (defun math-make-frac (num den)
84 (if (Math-integer-negp den)
85 (setq num (math-neg num)
86 den (math-neg den)))
87 (let ((gcd (math-gcd num den)))
88 (if (eq gcd 1)
89 (if (eq den 1)
90 num
91 (list 'frac num den))
92 (if (equal gcd den)
93 (math-quotient num gcd)
94 (list 'frac (math-quotient num gcd) (math-quotient den gcd))))))
96 (defun calc-add-fractions (a b)
97 (if (eq (car-safe a) 'frac)
98 (if (eq (car-safe b) 'frac)
99 (math-make-frac (math-add (math-mul (nth 1 a) (nth 2 b))
100 (math-mul (nth 2 a) (nth 1 b)))
101 (math-mul (nth 2 a) (nth 2 b)))
102 (math-make-frac (math-add (nth 1 a)
103 (math-mul (nth 2 a) b))
104 (nth 2 a)))
105 (math-make-frac (math-add (math-mul a (nth 2 b))
106 (nth 1 b))
107 (nth 2 b))))
109 (defun calc-mul-fractions (a b)
110 (if (eq (car-safe a) 'frac)
111 (if (eq (car-safe b) 'frac)
112 (math-make-frac (math-mul (nth 1 a) (nth 1 b))
113 (math-mul (nth 2 a) (nth 2 b)))
114 (math-make-frac (math-mul (nth 1 a) b)
115 (nth 2 a)))
116 (math-make-frac (math-mul a (nth 1 b))
117 (nth 2 b))))
119 (defun calc-div-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 2 b))
123 (math-mul (nth 2 a) (nth 1 b)))
124 (math-make-frac (nth 1 a)
125 (math-mul (nth 2 a) b)))
126 (math-make-frac (math-mul a (nth 2 b))
127 (nth 1 b))))
130 ;;; Convert a real value to fractional form. [T R I; T R F] [Public]
131 (defun calcFunc-frac (a &optional tol)
132 (or tol (setq tol 0))
133 (cond ((Math-ratp a)
135 ((memq (car a) '(cplx polar vec hms date sdev intv mod))
136 (cons (car a) (mapcar (function
137 (lambda (x)
138 (calcFunc-frac x tol)))
139 (cdr a))))
140 ((Math-messy-integerp a)
141 (math-trunc a))
142 ((Math-negp a)
143 (math-neg (calcFunc-frac (math-neg a) tol)))
144 ((not (eq (car a) 'float))
145 (if (math-infinitep a)
147 (if (math-provably-integerp a)
149 (math-reject-arg a 'numberp))))
150 ((integerp tol)
151 (if (<= tol 0)
152 (setq tol (+ tol calc-internal-prec)))
153 (calcFunc-frac a (list 'float 5
154 (- (+ (math-numdigs (nth 1 a))
155 (nth 2 a))
156 (1+ tol)))))
157 ((not (eq (car tol) 'float))
158 (if (Math-realp tol)
159 (calcFunc-frac a (math-float tol))
160 (math-reject-arg tol 'realp)))
161 ((Math-negp tol)
162 (calcFunc-frac a (math-neg tol)))
163 ((Math-zerop tol)
164 (calcFunc-frac a 0))
165 ((not (math-lessp-float tol '(float 1 0)))
166 (math-trunc a))
167 ((Math-zerop a)
170 (let ((cfrac (math-continued-fraction a tol))
171 (calc-prefer-frac t))
172 (math-eval-continued-fraction cfrac)))))
174 (defun math-continued-fraction (a tol)
175 (let ((calc-internal-prec (+ calc-internal-prec 2)))
176 (let ((cfrac nil)
177 (aa a)
178 (calc-prefer-frac nil)
179 int)
180 (while (or (null cfrac)
181 (and (not (Math-zerop aa))
182 (not (math-lessp-float
183 (math-abs
184 (math-sub a
185 (let ((f (math-eval-continued-fraction
186 cfrac)))
187 (math-working "Fractionalize" f)
188 f)))
189 tol))))
190 (setq int (math-trunc aa)
191 aa (math-sub aa int)
192 cfrac (cons int cfrac))
193 (or (Math-zerop aa)
194 (setq aa (math-div 1 aa))))
195 cfrac)))
197 (defun math-eval-continued-fraction (cf)
198 (let ((n (car cf))
199 (d 1)
200 temp)
201 (while (setq cf (cdr cf))
202 (setq temp (math-add (math-mul (car cf) n) d)
204 n temp))
205 (math-div n d)))
207 (defun calcFunc-fdiv (a b) ; [R I I] [Public]
208 (cond
209 ((Math-num-integerp a)
210 (cond
211 ((Math-num-integerp b)
212 (if (Math-zerop b)
213 (math-reject-arg a "*Division by zero")
214 (math-make-frac (math-trunc a) (math-trunc b))))
215 ((eq (car-safe b) 'frac)
216 (if (Math-zerop (nth 1 b))
217 (math-reject-arg a "*Division by zero")
218 (math-make-frac (math-mul (math-trunc a) (nth 2 b)) (nth 1 b))))
219 (t (math-reject-arg b 'integerp))))
220 ((eq (car-safe a) 'frac)
221 (cond
222 ((Math-num-integerp b)
223 (if (Math-zerop b)
224 (math-reject-arg a "*Division by zero")
225 (math-make-frac (cadr a) (math-mul (nth 2 a) (math-trunc b)))))
226 ((eq (car-safe b) 'frac)
227 (if (Math-zerop (nth 1 b))
228 (math-reject-arg a "*Division by zero")
229 (math-make-frac (math-mul (nth 1 a) (nth 2 b)) (math-mul (nth 2 a) (nth 1 b)))))
230 (t (math-reject-arg b 'integerp))))
232 (math-reject-arg a 'integerp))))
234 (provide 'calc-frac)
236 ;;; calc-frac.el ends here