* calc/calc.el (calc-ensure-consistent-units): New variable.
authorJay Belanger <jay.p.belanger@gmail.com>
Sat, 19 May 2012 03:00:48 +0000 (22:00 -0500)
committerJay Belanger <jay.p.belanger@gmail.com>
Sat, 19 May 2012 03:00:48 +0000 (22:00 -0500)
* calc/calc-units.el (math-consistent-units-p, math-check-unit-consistency):
  New functions.
  (calc-quick-units, calc-convert-units): Use `math-check-unit-consistency' when
  `calc-ensure-consistent-units' is non-nil.
  (calc-extract-units): Fix typo.

* doc/misc/calc.texi
  (Basic Operations on Units, Customizing Calc):
  Mention `calc-ensure-consistent-units'.

doc/misc/ChangeLog
doc/misc/calc.texi
lisp/ChangeLog
lisp/calc/calc-units.el
lisp/calc/calc.el

index c7dd1d3..420fde3 100644 (file)
@@ -1,3 +1,9 @@
+2012-05-19  Jay Belanger  <jay.p.belanger@gmail.com>
+
+       * doc/misc/calc.texi
+       (Basic Operations on Units, Customizing Calc):
+       Mention `calc-ensure-consistent-units'.
+
 2012-05-14  Andreas Schwab  <schwab@linux-m68k.org>
 
        * cc-mode.texi: Avoid space before macro in 4th argument of cross
index 48252ce..98b5c44 100644 (file)
@@ -27778,6 +27778,11 @@ acres per meter-second.)  Remainder units are expressed in terms of
 ``fundamental'' units like @samp{m} and @samp{s}, regardless of the
 input units.
 
+If you want to disallow using inconsistent units, you can set the customizable variable
+@code{calc-ensure-consistent-units} to @code{t} (@pxref{Customizing Calc}).  In this case, 
+if you request units which are inconsistent with the original units, you will be warned about
+it and no conversion will occur.
+
 One special exception is that if you specify a single unit name, and
 a compatible unit appears somewhere in the units expression, then
 that compatible unit will be converted to the new unit and the
@@ -35591,6 +35596,19 @@ as @samp{a/(b*c)}. If @code{calc-multiplication-has-precedence} is
 of @code{calc-multiplication-has-precedence} is @code{t}.
 @end defvar
 
+@defvar calc-ensure-consistent-units
+When converting units, the variable @code{calc-ensure-consistent-units} 
+determines whether or not the target units need to be consistent with the 
+original units.  If @code{calc-ensure-consistent-units} is @code{nil}, then
+the target units don't need to have the same dimensions as the original units; 
+for example, converting @samp{100 ft/s} to @samp{m} will produce @samp{30.48 m/s}.   
+If  @code{calc-ensure-consistent-units} is non-@code{nil}, then the target units 
+need to have the same dimensions as the original units; for example, converting 
+@samp{100 ft/s} to @samp{m} will result in an error, since @samp{ft/s} and @samp{m} 
+have different dimensions. The default value of @code{calc-ensure-consistent-units} 
+is @code{nil}.
+@end defvar
+
 @defvar calc-undo-length
 The variable @code{calc-undo-length} determines the number of undo
 steps that Calc will keep track of when @code{calc-quit} is called.
index ed24a1e..9177803 100644 (file)
@@ -1,3 +1,13 @@
+2012-05-19  Jay Belanger  <jay.p.belanger@gmail.com>
+
+       * calc/calc.el (calc-ensure-consistent-units): New variable.
+
+       * calc/calc-units.el (math-consistent-units-p, math-check-unit-consistency):
+       New functions.
+       (calc-quick-units, calc-convert-units): Use `math-check-unit-consistency' when
+       `calc-ensure-consistent-units' is non-nil.
+       (calc-extract-units): Fix typo.
+
 2012-05-18  Stefan Monnier  <monnier@iro.umontreal.ca>
 
        * vc/vc-bzr.el (vc-bzr-state-heuristic): Save match-data around sha1.
index 9d25830..f4100f3 100644 (file)
@@ -370,8 +370,11 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
      (unless (< pos (length units))
        (error "Unit number %d not defined" pos))
      (if (math-units-in-expr-p expr nil)
-        (calc-enter-result 1 (format "cun%d" num)
-                           (math-convert-units expr (nth pos units)))
+         (progn
+           (if calc-ensure-consistent-units
+               (math-check-unit-consistency expr units))
+           (calc-enter-result 1 (format "cun%d" num)
+                              (math-convert-units expr (nth pos units))))
        (calc-enter-result 1 (format "*un%d" num)
                          (math-simplify-units
                           (math-mul expr (nth pos units))))))))
@@ -477,6 +480,8 @@ If EXPR is nil, return nil."
      (setq units (math-read-expr new-units))
      (when (eq (car-safe units) 'error)
        (error "Bad format in units expression: %s" (nth 2 units)))
+     (if calc-ensure-consistent-units
+         (math-check-unit-consistency expr units))
      (math-put-default-units units)
      (let ((unew (math-units-in-expr-p units t))
           (std (and (eq (car-safe units) 'var)
@@ -560,7 +565,7 @@ If EXPR is nil, return nil."
 (defun calc-extract-units ()
   (interactive)
   (calc-slow-wrapper
-   (calc-enter-result 1 "rmun" (math-simplify-units
+   (calc-enter-result 1 "exun" (math-simplify-units
                                (math-extract-units (calc-top-n 1))))))
 
 ;; The variables calc-num-units and calc-den-units are local to
@@ -914,6 +919,17 @@ If EXPR is nil, return nil."
           (math-single-units-in-expr-p (nth 1 expr))))
        (t 'wrong)))
 
+(defun math-consistent-units-p (expr1 expr2)
+  "Non-nil if EXPR1 and EXPR2 have consistent units."
+  (math-numberp (math-get-units (list '/ expr1 expr2))))
+
+(defun math-check-unit-consistency (expr units)
+  "Give an error if EXPR and UNITS do not have consistent units."
+  (unless  (math-consistent-units-p expr units)
+    (error "New units (%s) are inconsistent with current units (%s)"
+           (math-format-value units)
+           (math-format-value (math-get-units expr)))))
+
 (defun math-check-unit-name (v)
   (and (eq (car-safe v) 'var)
        (or (assq (nth 1 v) (or math-units-table (math-build-units-table)))
index c7cea5f..4d64209 100644 (file)
@@ -418,6 +418,13 @@ in normal mode."
   :group 'calc
   :type 'boolean)
 
+(defcustom calc-ensure-consistent-units
+  nil
+  "If non-nil, make sure new units are consistent with current units
+when converting units."
+  :group 'calc
+  :type 'boolean)
+
 (defcustom calc-undo-length
   100
   "The number of undo steps that will be preserved when Calc is quit."