From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19858 invoked by alias); 14 Jan 2015 15:48:56 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 34280 Received: (qmail 26471 invoked from network); 14 Jan 2015 15:48:53 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_PASS autolearn=ham version=3.3.2 X-AuditID: cbfec7f5-b7fc86d0000066b7-ee-54b68d88fe12 Date: Wed, 14 Jan 2015 15:38:42 +0000 From: Peter Stephenson To: zsh-workers@zsh.org Subject: Re: Math expression evaluation error? Message-id: <20150114153842.7b48aa93@pwslap01u.europe.root.pri> In-reply-to: <20150114150258.GA25519@ypig.lip.ens-lyon.fr> References: <54B03024.1030309@gmail.com> <20150109201552.1304eafe@ntlworld.com> <54B04ADA.9050102@gmail.com> <20150109224034.294d4fd6@ntlworld.com> <20150114150258.GA25519@ypig.lip.ens-lyon.fr> Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrKLMWRmVeSWpSXmKPExsVy+t/xy7odvdtCDP7N5bA42PyQyYHRY9XB D0wBjFFcNimpOZllqUX6dglcGc/+HWAuWCtasWHTedYGxg7BLkZODgkBE4kvf5exQdhiEhfu rQeyuTiEBJYySnyd2QOWEBJYwiSxc4EsRGIbo8SyLd9Zuxg5OFgEVCV6PxWD1LAJGEpM3TSb EcQWERCXOLv2PAuILSygJ7H5fReYzStgLzFr6m6wGk4BK4mtZyewQ8w8yiix6/58JpAEv4C+ xNW/n5ggLrKXmHnlDCNEs6DEj8n3wAYxC2hJbN7WxAphy0tsXvOWGeJQdYkbd3ezT2AUmoWk ZRaSlllIWhYwMq9iFE0tTS4oTkrPNdIrTswtLs1L10vOz93ECAnarzsYlx6zOsQowMGoxMPr cGRriBBrYllxZe4hRgkOZiUR3qfl20KEeFMSK6tSi/Lji0pzUosPMTJxcEo1MIYwSqZ9+xmX t/SZ10ZxhlDbY5OPxWo8cg+ut5RWlf5j/+2sz7aNahJJUas+G14uPdUc+qxYuP7d7L16YQlr JySvqHkfNu/I85JpyWut1CtNedQWhmY9YfgvpxbmErV350zPyA3fk0uKkx+zPeBZVb/uq9aM iSwyj56lhFy9elI+77x+zI+QJCWW4oxEQy3mouJEALlNjhM4AgAA On Wed, 14 Jan 2015 16:02:58 +0100 Vincent Lefevre wrote: > FORCE_FLOAT doesn't > force a floating-point evaluation; it just converts *constants* to > floating-point. See the differences: > > ypig% setopt FORCE_FLOAT > ypig% integer a=1 b=2 > ypig% echo $((1/2)) > 0.5 > ypig% echo $((a/b)) > 0 > > I'm wondering whether this is the expected behavior, though. I wouldn't have thought so --- I would guess when forcing floating point calculations people would expect this to happen everywhere, despite what the documentation for the option says. This shouldn't be problematic if it's already working for constants since variables appear in just the same contexts. diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo index 076aaf4..8a0222c 100644 --- a/Doc/Zsh/options.yo +++ b/Doc/Zsh/options.yo @@ -496,9 +496,10 @@ pindex(NOFORCEFLOAT) cindex(floating point, forcing use of) cindex(forcing use of floating point) item(tt(FORCE_FLOAT))( -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. +Constants in arithmetic evaluation will be treated as +floating point even without the use of a decimal point; the +values of integer variables will be converted to floating point when +used in arithmetic expressions. Integers in any base will be converted. ) pindex(GLOB) pindex(NO_GLOB) diff --git a/Src/math.c b/Src/math.c index db42d0f..c047725 100644 --- a/Src/math.c +++ b/Src/math.c @@ -336,16 +336,27 @@ enum prec_type { static mnumber getmathparam(struct mathvalue *mptr) { + mnumber result; if (!mptr->pval) { char *s = mptr->lval; mptr->pval = (Value)zhalloc(sizeof(struct value)); if (!getvalue(mptr->pval, &s, 1)) { mptr->pval = NULL; + if (isset(FORCEFLOAT)) { + result.type = MN_FLOAT; + result.u.d = 0.0; + return result; + } return zero_mnumber; } } - return getnumvalue(mptr->pval); + result = getnumvalue(mptr->pval); + if (isset(FORCEFLOAT) && result.type == MN_INTEGER) { + result.type = MN_FLOAT; + result.u.d = (double)result.u.l; + } + return result; } static mnumber diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst index 8e0730d..ea87af2 100644 --- a/Test/C01arith.ztst +++ b/Test/C01arith.ztst @@ -308,3 +308,13 @@ >2 >2 # It's hard to test for integer to float. + + integer ff1=3 ff2=4 + print $(( ff1/ff2 )) + setopt force_float + print $(( ff1/ff2 )) + unsetopt force_float +0:Variables are forced to floating point where necessary +# 0.75 is exactly representable, don't expect rounding error. +>0 +>0.75