Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Math expression evaluation error?



On Fri, 9 Jan 2015 22:40:34 +0000
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> +Users should beware that, in common with many other programming
> +languages but not software designed for calculation, the evaluation of
> +an expression in zsh is taken a term at a time and promotion of integers
> +to floating point does not occur in terms only containing integers.  A
> +typical result of this is that a division such as tt(6/8) is truncated,
> +in this being rounded down to 0.  The tt(FORCE_FLOAT) shell option can
> +be used in scripts or functions where floating point evaluation is
> +required throughout.

Before this drifts out of my mind, here's a similar warning for zcalc.

Assuming anybody actually bothers to read the documentation...

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 5043243..00ede52 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -3227,8 +3227,18 @@ A reasonably powerful calculator based on zsh's arithmetic evaluation
 facility.  The syntax is similar to that of formulae in most programming
 languages; see
 ifzman(the section `Arithmetic Evaluation' in zmanref(zshmisc))\
-ifnzman(noderef(Arithmetic Evaluation)) for details.  The mathematical
-library tt(zsh/mathfunc) will be loaded if it is available; see
+ifnzman(noderef(Arithmetic Evaluation)) for details.
+
+Non-programmers should note that, as in many other programming
+languages, expressions involving only integers (whether constants
+without a `tt(.)', variables containing such constants as strings, or
+variables declared to be integers) are by default evaluated using
+integer arithmetic, which is not how an ordinary desk calculator
+operates.  To force floating point operation, pass the option tt(-f);
+see further notes below.
+
+The mathematical library tt(zsh/mathfunc) will be loaded if it is
+available; see
 ifzman(the section `The zsh/mathfunc Module' in zmanref(zshmodules))\
 ifnzman(noderef(The zsh/mathfunc Module)).  The mathematical functions
 correspond to the raw system libraries, so trigonometric functions are

pws



Messages sorted by: Reverse Date, Date, Thread, Author