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.
5 ;; This file is part of GNU Emacs.
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.
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.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-frac () nil)
32 (defun calc-fdiv (arg)
35 (calc-binary-op ":" 'calcFunc-fdiv arg 1)))
38 (defun calc-fraction (arg)
41 (let ((func (if (calc-is-hyperbolic) 'calcFunc-frac 'calcFunc-pfrac)))
43 (calc-enter-result 2 "frac" (list func
46 (calc-enter-result 1 "frac" (list func
48 (prefix-numeric-value (or arg 0))))))))
51 (defun calc-over-notation (fmt)
52 (interactive "sFraction separator (:, ::, /, //, :/): ")
54 (if (string-match "\\`\\([^ 0-9][^ 0-9]?\\)[0-9]*\\'" fmt)
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."))))
63 (defun calc-slash-notation (n)
66 (calc-change-mode 'calc-frac-format (if n '("//" nil) '("/" nil)) t)))
69 (defun calc-frac-mode (n)
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."))))
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)
90 (let ((gcd (math-gcd num den)))
96 (math-quotient num gcd)
97 (list 'frac (math-quotient num gcd) (math-quotient den gcd))))))
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))
108 (math-make-frac (math-add (math-mul a (nth 2 b))
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)
119 (math-make-frac (math-mul a (nth 1 b))
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))
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))
140 ((memq (car a) '(cplx polar vec hms date sdev intv mod))
141 (cons (car a) (mapcar (function
143 (calcFunc-frac x tol)))
145 ((Math-messy-integerp a)
148 (math-neg (calcFunc-frac (math-neg a) tol)))
149 ((not (eq (car a) 'float))
150 (if (math-infinitep a)
152 (if (math-provably-integerp a)
154 (math-reject-arg a 'numberp))))
157 (setq tol (+ tol calc-internal-prec)))
158 (calcFunc-frac a (list 'float 5
159 (- (+ (math-numdigs (nth 1 a))
162 ((not (eq (car tol) 'float))
164 (calcFunc-frac a (math-float tol))
165 (math-reject-arg tol 'realp)))
167 (calcFunc-frac a (math-neg tol)))
170 ((not (math-lessp-float tol '(float 1 0)))
175 (let ((cfrac (math-continued-fraction a tol))
176 (calc-prefer-frac t))
177 (math-eval-continued-fraction cfrac)))))
179 (defun math-continued-fraction (a tol)
180 (let ((calc-internal-prec (+ calc-internal-prec 2)))
183 (calc-prefer-frac nil)
185 (while (or (null cfrac)
186 (and (not (Math-zerop aa))
187 (not (math-lessp-float
190 (let ((f (math-eval-continued-fraction
192 (math-working "Fractionalize" f)
195 (setq int (math-trunc aa)
197 cfrac (cons int cfrac))
199 (setq aa (math-div 1 aa))))
202 (defun math-eval-continued-fraction (cf)
206 (while (setq cf (cdr cf))
207 (setq temp (math-add (math-mul (car cf) n) d)
214 (defun calcFunc-fdiv (a b) ; [R I I] [Public]
215 (if (Math-num-integerp a)
216 (if (Math-num-integerp 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)))
223 ;;; calc-frac.el ends here