From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2404 invoked by alias); 11 Jan 2015 07:10:24 -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: 34230 Received: (qmail 26651 invoked from network); 11 Jan 2015 07:10:20 -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=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=Kc1larcG c=1 sm=1 tr=0 a=FT8er97JFeGWzr5TCOCO5w==:117 a=kj9zAlcOel0A:10 a=q2GGsy2AAAAA:8 a=oR5dmqMzAAAA:8 a=-9mUelKeXuEA:10 a=YNv0rlydsVwA:10 a=vxi1udumlat59xxQRDQA:9 a=509mIObRmhL5v1Hz:21 a=mKkUNTr5VUORiuUR:21 a=CjuIK1q_8ugA:10 From: Bart Schaefer Message-id: <150110231017.ZM24021@torch.brasslantern.com> Date: Sat, 10 Jan 2015 23:10:17 -0800 In-reply-to: <54B20E23.8090900@eastlink.ca> Comments: In reply to Ray Andrews "Re: PATCH: hist: remove wrong NULL terminator" (Jan 10, 9:46pm) References: <1420807419-9270-1-git-send-email-mikachu@gmail.com> <54B013C5.6090307@eastlink.ca> <54B04A7A.1010402@eastlink.ca> <20150109223028.6e003bff@ntlworld.com> <54B066C5.3010008@eastlink.ca> <54B0D893.4080202@eastlink.ca> <510FB8E2-EA0C-4582-BD31-527E9755F0FB@larryv.me> <54B1ACA3.1050001@eastlink.ca> <150110175849.ZM21774@torch.brasslantern.com> <54B20E23.8090900@eastlink.ca> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Subject: Floating point modulus MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Jan 10, 9:46pm, Ray Andrews wrote: } } Speaking of testing, I can't get modulus to work nohow. } } $ echo $(( 2.5 )); } 2.5 } } $ echo $(( 2.5 % 2 )); } 0 } } ... If I'm doing something stupid there ... Zsh implements the C modulus operator (%) directly, and it's defined only on integers. If you try that in C it won't even compile: error: invalid operands to binary % Consequently the operands are silently converted to integers, instead of the shell throwing an error. The expectation is that you will know that zsh math is C math (same as for why things are integer by default) and that you must do: $ zmodload zsh/mathfunc $ echo $(( fmod(2.5, 2) )) 0.5 The following patch would instead silently substitute the fmod() call when using the % operator on a float. I won't commit it without some further discussion from the group. With more effort it could be made to do this only when FORCE_FLOAT is in effect, though I'm not sure how many such special-cases we want to manage. diff --git a/Src/math.c b/Src/math.c index 438a170..6d096e0 100644 --- a/Src/math.c +++ b/Src/math.c @@ -288,11 +288,11 @@ static int type[TOKCOUNT] = { /* 0 */ LR, LR|OP_OP|OP_OPF, RL, RL, RL|OP_OP|OP_OPF, /* 5 */ RL|OP_OP|OP_OPF, RL, RL, LR|OP_A2IO, LR|OP_A2IO, -/* 10 */ LR|OP_A2IO, LR|OP_A2, LR|OP_A2, LR|OP_A2IO, LR|OP_A2, +/* 10 */ LR|OP_A2IO, LR|OP_A2, LR|OP_A2, LR|OP_A2, LR|OP_A2, /* 15 */ LR|OP_A2, LR|OP_A2IO, LR|OP_A2IO, LR|OP_A2IR, LR|OP_A2IR, /* 20 */ LR|OP_A2IR, LR|OP_A2IR, LR|OP_A2IR, LR|OP_A2IR, BOOL|OP_A2IO, /* 25 */ BOOL|OP_A2IO, LR|OP_A2IO, RL|OP_OP, RL|OP_OP, RL|OP_E2, -/* 30 */ RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2IO, +/* 30 */ RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2, /* 35 */ RL|OP_E2IO, RL|OP_E2IO, RL|OP_E2IO, RL|OP_E2IO, RL|OP_E2IO, /* 40 */ BOOL|OP_E2IO, BOOL|OP_E2IO, RL|OP_A2IO, RL|OP_A2, RL|OP_OP, /* 45 */ RL, RL, LR|OP_OPF, LR|OP_OPF, RL|OP_A2, @@ -1133,7 +1133,9 @@ op(int what) * Any integer mod -1 is the same as any integer mod 1 * i.e. zero. */ - if (b.u.l == -1) + if (c.type == MN_FLOAT) + c.u.d = fmod(a.u.d, b.u.d); + else if (b.u.l == -1) c.u.l = 0; else c.u.l = a.u.l % b.u.l;