Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Floating point modulus



On Sun, 11 Jan 2015 18:46:32 -0800
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Jan 11,  8:25pm, Peter Stephenson wrote:
> }
> } I'll leave that to you, but instead of an explicit rounding you could do
> } basically the same calculation but assigned to a variable declared as an
> } integer and output that.
> 
> Hm.  This seems like a bug:
> 
> % integer rnd
> % print -- $(( rnd = ((29.1 % 13.0 * 10) + 0.5) ))
> 31.500000000000014
> 
> Shouldn't the result of the $(( ... )) be an integer because rnd has been
> declared as an integer?

The result above is how I always thought it worked, actually.  The type
of the result isn't necessarily the type of the variable assigned to;
there's never been any code to propagate types back from assignments,
unlike in C.

Whether or not it should be is another question.  Looking at the doc,
with its references to C, you'd probably be entitled to expect assignment
types propagated the way they do in C.

In particular:

% integer foo
% (( foo )); print $?
1                                     # foo is zero, status is non-zero
% (( foo = 3.5/5.5 )); print $?
0                                     # foo is zero but
                                      # status is also zero as
				      # result is non zero
% (( foo )); print $?
1                                     # different result

You might think the status at least went with the assignment.

> I had to do this instead:

That's what I was expecting you to do.

pws



Messages sorted by: Reverse Date, Date, Thread, Author