comparison lisp/calc/calc-math.el @ 41271:fcd507927105

Change all toplevel `setq' forms to `defvar' forms, and move them before their first use. Use `when', `unless'. Remove trailing periods from error forms. Add description and headers suggested by Emacs Lisp coding conventions.
author Colin Walters <walters@gnu.org>
date Mon, 19 Nov 2001 07:43:43 +0000
parents 4549dec29728
children 593f7009284d
comparison
equal deleted inserted replaced
41270:711f18abaf57 41271:fcd507927105
1 ;; Calculator for GNU Emacs, part II [calc-math.el] 1 ;;; calc-math.el --- mathematical functions for Calc
2
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com. 4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
4 7
5 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
6 9
7 ;; GNU Emacs is distributed in the hope that it will be useful, 10 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor 11 ;; but WITHOUT ANY WARRANTY. No author or distributor
17 ;; supposed to have been given to you along with GNU Emacs so you 20 ;; 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 21 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice 22 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies. 23 ;; and this notice must be preserved on all copies.
21 24
22 25 ;;; Commentary:
26
27 ;;; Code:
23 28
24 ;; This file is autoloaded from calc-ext.el. 29 ;; This file is autoloaded from calc-ext.el.
25 (require 'calc-ext) 30 (require 'calc-ext)
26 31
27 (require 'calc-macs) 32 (require 'calc-macs)
234 (defun calc-degrees-mode (arg) 239 (defun calc-degrees-mode (arg)
235 (interactive "p") 240 (interactive "p")
236 (cond ((= arg 1) 241 (cond ((= arg 1)
237 (calc-wrapper 242 (calc-wrapper
238 (calc-change-mode 'calc-angle-mode 'deg) 243 (calc-change-mode 'calc-angle-mode 'deg)
239 (message "Angles measured in degrees."))) 244 (message "Angles measured in degrees")))
240 ((= arg 2) (calc-radians-mode)) 245 ((= arg 2) (calc-radians-mode))
241 ((= arg 3) (calc-hms-mode)) 246 ((= arg 3) (calc-hms-mode))
242 (t (error "Prefix argument out of range")))) 247 (t (error "Prefix argument out of range"))))
243 248
244 (defun calc-radians-mode () 249 (defun calc-radians-mode ()
245 (interactive) 250 (interactive)
246 (calc-wrapper 251 (calc-wrapper
247 (calc-change-mode 'calc-angle-mode 'rad) 252 (calc-change-mode 'calc-angle-mode 'rad)
248 (message "Angles measured in radians."))) 253 (message "Angles measured in radians")))
249 254
250 255
251 ;;; Compute the integer square-root floor(sqrt(A)). A > 0. [I I] [Public] 256 ;;; Compute the integer square-root floor(sqrt(A)). A > 0. [I I] [Public]
252 ;;; This method takes advantage of the fact that Newton's method starting 257 ;;; This method takes advantage of the fact that Newton's method starting
253 ;;; with an overestimate always works, even using truncating integer division! 258 ;;; with an overestimate always works, even using truncating integer division!
410 (and inf 415 (and inf
411 (math-mul (math-sqrt (math-infinite-dir a inf)) inf))) 416 (math-mul (math-sqrt (math-infinite-dir a inf)) inf)))
412 (progn 417 (progn
413 (calc-record-why 'numberp a) 418 (calc-record-why 'numberp a)
414 (list 'calcFunc-sqrt a)))) 419 (list 'calcFunc-sqrt a))))
415 (defalias calcFunc-sqrt 'math-sqrt) 420 (defalias 'calcFunc-sqrt 'math-sqrt)
416 421
417 (defun math-infinite-dir (a &optional inf) 422 (defun math-infinite-dir (a &optional inf)
418 (or inf (setq inf (math-infinitep a))) 423 (or inf (setq inf (math-infinitep a)))
419 (math-normalize (math-expr-subst a inf 1))) 424 (math-normalize (math-expr-subst a inf 1)))
420 425
530 (math-from-hms b 'deg))) 535 (math-from-hms b 'deg)))
531 (math-to-hms (math-hypot (math-from-hms a 'deg) b)))) 536 (math-to-hms (math-hypot (math-from-hms a 'deg) b))))
532 ((eq (car-safe b) 'hms) 537 ((eq (car-safe b) 'hms)
533 (math-to-hms (math-hypot a (math-from-hms b 'deg)))) 538 (math-to-hms (math-hypot a (math-from-hms b 'deg))))
534 (t nil))) 539 (t nil)))
535 (defalias calcFunc-hypot 'math-hypot) 540 (defalias 'calcFunc-hypot 'math-hypot)
536 541
537 (defun calcFunc-sqr (x) 542 (defun calcFunc-sqr (x)
538 (math-pow x 2)) 543 (math-pow x 2))
539 544
540 545
1322 (setq pow next 1327 (setq pow next
1323 sum (+ sum n)))) 1328 sum (+ sum n))))
1324 (cons (equal pow x) sum))) 1329 (cons (equal pow x) sum)))
1325 1330
1326 1331
1332 (defvar math-log-base-cache nil)
1327 (defun math-log-base-raw (b) ; [N N] 1333 (defun math-log-base-raw (b) ; [N N]
1328 (if (not (and (equal (car math-log-base-cache) b) 1334 (if (not (and (equal (car math-log-base-cache) b)
1329 (eq (nth 1 math-log-base-cache) calc-internal-prec))) 1335 (eq (nth 1 math-log-base-cache) calc-internal-prec)))
1330 (setq math-log-base-cache (list b calc-internal-prec 1336 (setq math-log-base-cache (list b calc-internal-prec
1331 (math-ln-raw (math-float b))))) 1337 (math-ln-raw (math-float b)))))
1332 (nth 2 math-log-base-cache)) 1338 (nth 2 math-log-base-cache))
1333 (setq math-log-base-cache nil)
1334 1339
1335 (defun calcFunc-lnp1 (x) ; [N N] [Public] 1340 (defun calcFunc-lnp1 (x) ; [N N] [Public]
1336 (cond ((Math-equal-int x -1) 1341 (cond ((Math-equal-int x -1)
1337 (if calc-infinite-mode 1342 (if calc-infinite-mode
1338 '(neg (var inf var-inf)) 1343 '(neg (var inf var-inf))