* [patch] Round to zero not down
@ 2015-09-29 5:06 Matthew Martin
0 siblings, 0 replies; only message in thread
From: Matthew Martin @ 2015-09-29 5:06 UTC (permalink / raw)
To: zsh-workers
The docs claim that various operations round down; however,
% a=-5;((b=a/2.0));echo $b
-2
% echo $((~ -5/2.0))
2 # Actually rounds down
% echo $(((-5/2.0) & -1))
-2
% echo $(((-5/2.0) | 0))
-2
% echo $(((-5/2.0) ^ 0))
-2
% echo $(((-5/2.0) % 0))
-2.5 # Doesn't round at all
% echo $(((-5/2.0) >> 1))
-1 # The -2.5 is rounded to -2
% echo $(((-6/2.0) >> 1))
-2 # But -1,5 (the result of the shift) is rounded down
% echo $(((-5/2.0) << 1))
-4
% echo $((-6/8))
0
The diff below changes the docs to reflect this behavior.
diff --git a/Doc/Zsh/arith.yo b/Doc/Zsh/arith.yo
index 1dcd18c..17f466f 100644
--- a/Doc/Zsh/arith.yo
+++ b/Doc/Zsh/arith.yo
@@ -215,7 +215,7 @@ findex(integer, use of)
Arithmetic evaluation is performed on the value of each
assignment to a named parameter declared integer
in this manner. Assigning a floating point number to an integer results in
-rounding down to the next integer.
+rounding towards zero.
cindex(parameters, floating point)
cindex(floating point parameters)
@@ -230,16 +230,16 @@ format.
Promotion of integer to floating point values is performed where
necessary. In addition, if any operator which requires an integer
-(`tt(~)', `tt(&)', `tt(|)', `tt(^)', `tt(%)', `tt(<<)', `tt(>>)' and their
-equivalents with assignment) is given a floating point argument, it will be
-silently rounded down to the next integer.
+(`tt(&)', `tt(|)', `tt(^)', `tt(<<)', `tt(>>)' and their equivalents with
+assignment) is given a floating point argument, it will be silently rounded
+towards zero except for `tt(~)' which rounds down.
Users should beware that, in common with many other programming
languages but not software designed for calculation, the evaluation of
an expression in zsh is taken a term at a time and promotion of integers
to floating point does not occur in terms only containing integers. A
typical result of this is that a division such as tt(6/8) is truncated,
-in this being rounded down to 0. The tt(FORCE_FLOAT) shell option can
+in this being rounded towards 0. The tt(FORCE_FLOAT) shell option can
be used in scripts or functions where floating point evaluation is
required throughout.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2015-09-29 5:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-29 5:06 [patch] Round to zero not down Matthew Martin
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).