1 ;;; calc-frac.el --- fraction functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-fdiv (arg)
38 (calc-binary-op ":" 'calcFunc-fdiv arg
1)))
41 (defun calc-fraction (arg)
44 (let ((func (if (calc-is-hyperbolic) 'calcFunc-frac
'calcFunc-pfrac
)))
46 (calc-enter-result 2 "frac" (list func
49 (calc-enter-result 1 "frac" (list func
51 (prefix-numeric-value (or arg
0))))))))
54 (defun calc-over-notation (fmt)
55 (interactive "sFraction separator: ")
57 (if (string-match "\\`\\([^ 0-9][^ 0-9]?\\)[0-9]*\\'" fmt
)
59 (if (/= (match-end 0) (match-end 1))
60 (setq n
(string-to-number (substring fmt
(match-end 1)))
61 fmt
(math-match-substring fmt
1)))
62 (if (eq n
0) (error "Bad denominator"))
63 (calc-change-mode 'calc-frac-format
(list fmt n
) t
))
64 (error "Bad fraction separator format"))))
66 (defun calc-slash-notation (n)
69 (calc-change-mode 'calc-frac-format
(if n
'("//" nil
) '("/" nil
)) t
)))
72 (defun calc-frac-mode (n)
75 (calc-change-mode 'calc-prefer-frac n nil t
)
76 (message (if calc-prefer-frac
77 "Integer division will now generate fractions"
78 "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
))
133 ;;; Convert a real value to fractional form. [T R I; T R F] [Public]
134 (defun calcFunc-frac (a &optional tol
)
135 (or tol
(setq tol
0))
138 ((memq (car a
) '(cplx polar vec hms date sdev intv mod
))
139 (cons (car a
) (mapcar (function
141 (calcFunc-frac x tol
)))
143 ((Math-messy-integerp a
)
146 (math-neg (calcFunc-frac (math-neg a
) tol
)))
147 ((not (eq (car a
) 'float
))
148 (if (math-infinitep a
)
150 (if (math-provably-integerp a
)
152 (math-reject-arg a
'numberp
))))
155 (setq tol
(+ tol calc-internal-prec
)))
156 (calcFunc-frac a
(list 'float
5
157 (- (+ (math-numdigs (nth 1 a
))
160 ((not (eq (car tol
) 'float
))
162 (calcFunc-frac a
(math-float tol
))
163 (math-reject-arg tol
'realp
)))
165 (calcFunc-frac a
(math-neg tol
)))
168 ((not (math-lessp-float tol
'(float 1 0)))
173 (let ((cfrac (math-continued-fraction a tol
))
174 (calc-prefer-frac t
))
175 (math-eval-continued-fraction cfrac
)))))
177 (defun math-continued-fraction (a tol
)
178 (let ((calc-internal-prec (+ calc-internal-prec
2)))
181 (calc-prefer-frac nil
)
183 (while (or (null cfrac
)
184 (and (not (Math-zerop aa
))
185 (not (math-lessp-float
188 (let ((f (math-eval-continued-fraction
190 (math-working "Fractionalize" f
)
193 (setq int
(math-trunc aa
)
195 cfrac
(cons int cfrac
))
197 (setq aa
(math-div 1 aa
))))
200 (defun math-eval-continued-fraction (cf)
204 (while (setq cf
(cdr cf
))
205 (setq temp
(math-add (math-mul (car cf
) n
) d
)
212 (defun calcFunc-fdiv (a b
) ; [R I I] [Public]
213 (if (Math-num-integerp a
)
214 (if (Math-num-integerp b
)
216 (math-reject-arg a
"*Division by zero")
217 (math-make-frac (math-trunc a
) (math-trunc b
)))
218 (math-reject-arg b
'integerp
))
219 (math-reject-arg a
'integerp
)))
223 ;;; arch-tag: 89d65274-0b3b-42d8-aacd-eaf86da5b4ea
224 ;;; calc-frac.el ends here