zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: <zsh-workers@zsh.org>
Subject: Floating point modulus
Date: Sat, 10 Jan 2015 23:10:17 -0800	[thread overview]
Message-ID: <150110231017.ZM24021@torch.brasslantern.com> (raw)
In-Reply-To: <54B20E23.8090900@eastlink.ca>

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;


  reply	other threads:[~2015-01-11  7:10 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-09 12:43 PATCH: hist: remove wrong NULL terminator Mikael Magnusson
2015-01-09 17:45 ` Ray Andrews
2015-01-09 18:33   ` Lawrence Velázquez
2015-01-09 18:36     ` Lawrence Velázquez
2015-01-09 19:38   ` Mikael Magnusson
2015-01-09 21:39     ` Ray Andrews
2015-01-09 22:30       ` Peter Stephenson
2015-01-09 23:39         ` Ray Andrews
2015-01-10  0:39           ` Bart Schaefer
2015-01-10  7:45             ` Ray Andrews
2015-01-10 22:04               ` Lawrence Velázquez
2015-01-10 22:50                 ` Ray Andrews
2015-01-11  1:58                   ` Bart Schaefer
2015-01-11  5:46                     ` Ray Andrews
2015-01-11  7:10                       ` Bart Schaefer [this message]
2015-01-11 17:33                         ` Floating point modulus Peter Stephenson
2015-01-11 19:25                           ` Bart Schaefer
2015-01-11 19:25                         ` Ray Andrews
2015-01-11 20:00                           ` Bart Schaefer
2015-01-11 20:58                             ` Ray Andrews
2015-01-11 21:34                               ` Peter Stephenson
2015-01-12  0:18                                 ` Ray Andrews
2015-01-12 10:03                                   ` Peter Stephenson
2015-01-11 19:36                         ` Bart Schaefer
2015-01-11 20:01                           ` Peter Stephenson
2015-01-11 20:04                             ` Bart Schaefer
2015-01-11 20:25                               ` Peter Stephenson
2015-01-12  0:02                                 ` Ray Andrews
2015-01-12  2:23                                   ` Bart Schaefer
2015-01-12  2:46                                 ` Bart Schaefer
2015-01-12  9:56                                   ` Peter Stephenson
2015-01-12 13:49                                     ` Peter Stephenson
2015-01-12 16:35                                     ` Bart Schaefer
2015-01-12 16:45                                       ` Peter Stephenson
2015-01-12 11:36                           ` Vincent Lefevre
2015-01-10  7:08 ` PATCH: hist: remove wrong NULL terminator Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=150110231017.ZM24021@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).