diff options
| author | Peter Stephenson <pws@zsh.org> | 2015-01-12 16:38:00 +0000 |
|---|---|---|
| committer | Peter Stephenson <pws@zsh.org> | 2015-01-12 16:38:00 +0000 |
| commit | 5f4325a0a41987a92cee8b64a76e5b0d5e831f60 (patch) | |
| tree | 8d66f0afe363b606c5841aff3c0c8869c2da4c4d /Src/math.c | |
| parent | 34253: warn in zcalc doc about integer arithmetic. (diff) | |
| download | zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar.gz zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar.bz2 zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar.lz zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar.xz zsh-5f4325a0a41987a92cee8b64a76e5b0d5e831f60.tar.zst 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/math.c')
| -rw-r--r-- | Src/math.c | 29 |
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; } |
