summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
authorPeter Stephenson <pws@zsh.org>2015-01-12 16:38:00 +0000
committerPeter Stephenson <pws@zsh.org>2015-01-12 16:38:00 +0000
commit5f4325a0a41987a92cee8b64a76e5b0d5e831f60 (patch)
tree8d66f0afe363b606c5841aff3c0c8869c2da4c4d /Src
parent626650f20e5c01fa6554da2a73dc5338a2523842 (diff)
downloadzsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar.gz
zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar.xz
zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.zip
Propagate float/integer type in arithmetic assignment.
Add test.

Mention this and also floating point mod change in README.
Diffstat (limited to 'Src')
-rw-r--r--Src/math.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/Src/math.c b/Src/math.c
index 6d096e0df..db42d0f7c 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -880,6 +880,8 @@ getcvar(char *s)
 static mnumber
 setmathvar(struct mathvalue *mvp, mnumber v)
 {
+    Param pm;
+
     if (mvp->pval) {
 	/*
 	 * This value may have been hanging around for a while.
@@ -909,7 +911,32 @@ setmathvar(struct mathvalue *mvp, mnumber v)
     if (noeval)
 	return v;
     untokenize(mvp->lval);
-    setnparam(mvp->lval, v);
+    pm = setnparam(mvp->lval, v);
+    if (pm) {
+	/*
+	 * If we are performing an assignment, we return the
+	 * number with the same type as the parameter we are
+	 * assigning to, in the spirit of the way assignments
+	 * in C work.  Note this was a change to long-standing
+	 * zsh behaviour.
+	 */
+	switch (PM_TYPE(pm->node.flags)) {
+	case PM_INTEGER:
+	    if (v.type != MN_INTEGER) {
+		v.u.l = (zlong)v.u.d;
+		v.type = MN_INTEGER;
+	    }
+	    break;
+
+	case PM_EFLOAT:
+	case PM_FFLOAT:
+	    if (v.type != MN_FLOAT) {
+		v.u.d = (double)v.u.l;
+		v.type = MN_FLOAT;
+	    }
+	    break;
+	}
+    }
     return v;
 }