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

Re: Math expression evaluation error?

Thanks for the detailed explanation. I suggest that the an additional line be added to the manual, which would say something like:

" Caution: The evaluation of an expression is taken a term at a time and if there are mixed terms , say integer only and float terms , the integer only ones will not be promoted to real values and the result of the whole expression would be an error to common standards. One can use the -f option or the FORCE_FLOAT environment variable to change this behavior."

I apologize if this is in the manual already.


On 2015年01月09日 12:15, Peter Stephenson wrote:
On Fri, 09 Jan 2015 11:46:44 -0800
jdh <dhenman@xxxxxxxxx> wrote:
I believe I read that math expression evaluation will convert all
calculations in an expression if "any" value in that expression is a
real value.
This isn't the case and wherever this came from needs correcting.
Please instead read the documentation under ARITHMETIC EVALUATION in the
zshmisc manual for what actually happens and let us if know if you think
that is inadequate.  The actually phrasing is "Promotion of integer to
floating point values is performed where necessary".  It also refers
above that to C --- there's an underlying assumption here that the user
has some idea of the operation of C-like languages, which might of
course not be the case.  "Where necessary" is in general where an
operator has a floating point number as one argument and an integer as
another; a C programmer would recognise the use of the word "promotion"
as implying this, a general user probably not.  So the wording could
probably be improved.  Brief and to the point suggestions on this from
anyone are welcome (long rambles on why anyone doesn't understand it
may be invigorating for the rambler but are less useful to us as
developers :-)).

See also the following shell option:

        Constants in arithmetic evaluation will be treated  as  floating
        point  even without the use of a decimal point.  Integers in any
        base will be converted.

This is most useful in zcalc, which actually has an option for this.

        If the option `-f' is set, all numbers are treated  as  floating
        point,  hence for example the expression `3/4' evaluates to 0.75
        rather than 0.  Options must appear in separate words.


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