Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / calc / calc-frac.el
CommitLineData
12f1951a
CW
1;;; calc-frac.el --- fraction functions for Calc
2
58ba2f8f 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
8b72699e 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
136211a9 5
12f1951a 6;; Author: David Gillespie <daveg@synaptics.com>
e8fff8ed 7;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
136211a9 8
a2cdc7f9
PJ
9;; This file is part of GNU Emacs.
10
662c9c64 11;; GNU Emacs is free software: you can redistribute it and/or modify
7c671b23 12;; it under the terms of the GNU General Public License as published by
662c9c64
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
7c671b23 15
136211a9 16;; GNU Emacs is distributed in the hope that it will be useful,
7c671b23
GM
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
662c9c64 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
136211a9 23
12f1951a 24;;; Commentary:
136211a9 25
12f1951a 26;;; Code:
136211a9
EZ
27
28;; This file is autoloaded from calc-ext.el.
136211a9 29
3ccef44f 30(require 'calc-ext)
136211a9
EZ
31(require 'calc-macs)
32
136211a9
EZ
33(defun calc-fdiv (arg)
34 (interactive "P")
35 (calc-slow-wrapper
bf77c646 36 (calc-binary-op ":" 'calcFunc-fdiv arg 1)))
136211a9
EZ
37
38
39(defun calc-fraction (arg)
40 (interactive "P")
41 (calc-slow-wrapper
42 (let ((func (if (calc-is-hyperbolic) 'calcFunc-frac 'calcFunc-pfrac)))
43 (if (eq arg 0)
44 (calc-enter-result 2 "frac" (list func
45 (calc-top-n 2)
46 (calc-top-n 1)))
47 (calc-enter-result 1 "frac" (list func
48 (calc-top-n 1)
bf77c646 49 (prefix-numeric-value (or arg 0))))))))
136211a9
EZ
50
51
52(defun calc-over-notation (fmt)
97d83988 53 (interactive "sFraction separator: ")
136211a9
EZ
54 (calc-wrapper
55 (if (string-match "\\`\\([^ 0-9][^ 0-9]?\\)[0-9]*\\'" fmt)
56 (let ((n nil))
57 (if (/= (match-end 0) (match-end 1))
d227f871 58 (setq n (string-to-number (substring fmt (match-end 1)))
136211a9
EZ
59 fmt (math-match-substring fmt 1)))
60 (if (eq n 0) (error "Bad denominator"))
61 (calc-change-mode 'calc-frac-format (list fmt n) t))
12f1951a 62 (error "Bad fraction separator format"))))
136211a9
EZ
63
64(defun calc-slash-notation (n)
65 (interactive "P")
66 (calc-wrapper
bf77c646 67 (calc-change-mode 'calc-frac-format (if n '("//" nil) '("/" nil)) t)))
136211a9
EZ
68
69
70(defun calc-frac-mode (n)
71 (interactive "P")
72 (calc-wrapper
73 (calc-change-mode 'calc-prefer-frac n nil t)
74 (message (if calc-prefer-frac
12f1951a
CW
75 "Integer division will now generate fractions"
76 "Integer division will now generate floating-point results"))))
136211a9
EZ
77
78
79;;;; Fractions.
80
81;;; Build a normalized fraction. [R I I]
82;;; (This could probably be implemented more efficiently than using
83;;; the plain gcd algorithm.)
84(defun math-make-frac (num den)
85 (if (Math-integer-negp den)
86 (setq num (math-neg num)
87 den (math-neg den)))
88 (let ((gcd (math-gcd num den)))
89 (if (eq gcd 1)
90 (if (eq den 1)
91 num
92 (list 'frac num den))
93 (if (equal gcd den)
94 (math-quotient num gcd)
bf77c646 95 (list 'frac (math-quotient num gcd) (math-quotient den gcd))))))
136211a9
EZ
96
97(defun calc-add-fractions (a b)
98 (if (eq (car-safe a) 'frac)
99 (if (eq (car-safe b) 'frac)
100 (math-make-frac (math-add (math-mul (nth 1 a) (nth 2 b))
101 (math-mul (nth 2 a) (nth 1 b)))
102 (math-mul (nth 2 a) (nth 2 b)))
103 (math-make-frac (math-add (nth 1 a)
104 (math-mul (nth 2 a) b))
105 (nth 2 a)))
106 (math-make-frac (math-add (math-mul a (nth 2 b))
107 (nth 1 b))
bf77c646 108 (nth 2 b))))
136211a9
EZ
109
110(defun calc-mul-fractions (a b)
111 (if (eq (car-safe a) 'frac)
112 (if (eq (car-safe b) 'frac)
113 (math-make-frac (math-mul (nth 1 a) (nth 1 b))
114 (math-mul (nth 2 a) (nth 2 b)))
115 (math-make-frac (math-mul (nth 1 a) b)
116 (nth 2 a)))
117 (math-make-frac (math-mul a (nth 1 b))
bf77c646 118 (nth 2 b))))
136211a9
EZ
119
120(defun calc-div-fractions (a b)
121 (if (eq (car-safe a) 'frac)
122 (if (eq (car-safe b) 'frac)
123 (math-make-frac (math-mul (nth 1 a) (nth 2 b))
124 (math-mul (nth 2 a) (nth 1 b)))
125 (math-make-frac (nth 1 a)
126 (math-mul (nth 2 a) b)))
127 (math-make-frac (math-mul a (nth 2 b))
bf77c646 128 (nth 1 b))))
136211a9
EZ
129
130
136211a9
EZ
131;;; Convert a real value to fractional form. [T R I; T R F] [Public]
132(defun calcFunc-frac (a &optional tol)
133 (or tol (setq tol 0))
134 (cond ((Math-ratp a)
135 a)
136 ((memq (car a) '(cplx polar vec hms date sdev intv mod))
137 (cons (car a) (mapcar (function
138 (lambda (x)
139 (calcFunc-frac x tol)))
140 (cdr a))))
141 ((Math-messy-integerp a)
142 (math-trunc a))
143 ((Math-negp a)
144 (math-neg (calcFunc-frac (math-neg a) tol)))
145 ((not (eq (car a) 'float))
146 (if (math-infinitep a)
147 a
148 (if (math-provably-integerp a)
149 a
150 (math-reject-arg a 'numberp))))
151 ((integerp tol)
152 (if (<= tol 0)
153 (setq tol (+ tol calc-internal-prec)))
154 (calcFunc-frac a (list 'float 5
155 (- (+ (math-numdigs (nth 1 a))
156 (nth 2 a))
157 (1+ tol)))))
158 ((not (eq (car tol) 'float))
159 (if (Math-realp tol)
160 (calcFunc-frac a (math-float tol))
161 (math-reject-arg tol 'realp)))
162 ((Math-negp tol)
163 (calcFunc-frac a (math-neg tol)))
164 ((Math-zerop tol)
165 (calcFunc-frac a 0))
166 ((not (math-lessp-float tol '(float 1 0)))
167 (math-trunc a))
168 ((Math-zerop a)
169 0)
170 (t
171 (let ((cfrac (math-continued-fraction a tol))
172 (calc-prefer-frac t))
bf77c646 173 (math-eval-continued-fraction cfrac)))))
136211a9
EZ
174
175(defun math-continued-fraction (a tol)
176 (let ((calc-internal-prec (+ calc-internal-prec 2)))
177 (let ((cfrac nil)
178 (aa a)
179 (calc-prefer-frac nil)
180 int)
181 (while (or (null cfrac)
182 (and (not (Math-zerop aa))
183 (not (math-lessp-float
184 (math-abs
185 (math-sub a
186 (let ((f (math-eval-continued-fraction
187 cfrac)))
188 (math-working "Fractionalize" f)
189 f)))
190 tol))))
191 (setq int (math-trunc aa)
192 aa (math-sub aa int)
193 cfrac (cons int cfrac))
194 (or (Math-zerop aa)
195 (setq aa (math-div 1 aa))))
bf77c646 196 cfrac)))
136211a9
EZ
197
198(defun math-eval-continued-fraction (cf)
199 (let ((n (car cf))
200 (d 1)
201 temp)
202 (while (setq cf (cdr cf))
203 (setq temp (math-add (math-mul (car cf) n) d)
204 d n
205 n temp))
bf77c646 206 (math-div n d)))
136211a9
EZ
207
208
209
210(defun calcFunc-fdiv (a b) ; [R I I] [Public]
211 (if (Math-num-integerp a)
212 (if (Math-num-integerp b)
213 (if (Math-zerop b)
214 (math-reject-arg a "*Division by zero")
215 (math-make-frac (math-trunc a) (math-trunc b)))
216 (math-reject-arg b 'integerp))
bf77c646 217 (math-reject-arg a 'integerp)))
136211a9 218
3ccef44f
JB
219(provide 'calc-frac)
220
cbee283d 221;; arch-tag: 89d65274-0b3b-42d8-aacd-eaf86da5b4ea
bf77c646 222;;; calc-frac.el ends here