1 ;;; calc-frac.el --- fraction functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 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/>.
27 ;; This file is autoloaded from calc-ext.el.
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-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)
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"))))
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
)
87 (let ((gcd (math-gcd num 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
))
105 (math-make-frac (math-add (math-mul a
(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
)
116 (math-make-frac (math-mul a
(nth 1 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
))
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))
135 ((memq (car a
) '(cplx polar vec hms date sdev intv mod
))
136 (cons (car a
) (mapcar (function
138 (calcFunc-frac x tol
)))
140 ((Math-messy-integerp 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
))))
152 (setq tol
(+ tol calc-internal-prec
)))
153 (calcFunc-frac a
(list 'float
5
154 (- (+ (math-numdigs (nth 1 a
))
157 ((not (eq (car tol
) 'float
))
159 (calcFunc-frac a
(math-float tol
))
160 (math-reject-arg tol
'realp
)))
162 (calcFunc-frac a
(math-neg tol
)))
165 ((not (math-lessp-float tol
'(float 1 0)))
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)))
178 (calc-prefer-frac nil
)
180 (while (or (null cfrac
)
181 (and (not (Math-zerop aa
))
182 (not (math-lessp-float
185 (let ((f (math-eval-continued-fraction
187 (math-working "Fractionalize" f
)
190 (setq int
(math-trunc aa
)
192 cfrac
(cons int cfrac
))
194 (setq aa
(math-div 1 aa
))))
197 (defun math-eval-continued-fraction (cf)
201 (while (setq cf
(cdr cf
))
202 (setq temp
(math-add (math-mul (car cf
) n
) d
)
207 (defun calcFunc-fdiv (a b
) ; [R I I] [Public]
209 ((Math-num-integerp a
)
211 ((Math-num-integerp 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
)
222 ((Math-num-integerp 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
))))
236 ;;; calc-frac.el ends here