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

Re: Math expression evaluation error?



12.01.2015, 12:18, "Vincent Lefevre" <vincent@xxxxxxxxxx>:
> On 2015-01-10 21:10:16 -0800, Ray Andrews wrote:
>>  Sure.  That's the best argument for leaving it alone and of course it is
>>  perfectly sound.  I'd go the other way, liking friendliness more, but
>>  I respect your decision. And I could be quite wrong about 1/2 + 1/2 = 0,
>>  strange as it looks to me, rounding down could be vital to some code
>>  somewhere.
>
> It's only a notation problem. Unfortunately / was chosen (historically,
> not by zsh) as the symbol for integer division. The shell expression
> 1/2 + 1/2 just means:
>
>   integer_div(1,2) + integer_div(1,2)
>
> where integer_div is the integer division function, which gives 0.
> This is perfectly correct math.

It is not integer division. 1.0/2 and 1/2.0 will give the same result: 0.5. If you want to see language with operator for integer division check out Python-3: it has / for always-floating-point-unless-overridden and // for always-integral-unless-overridden (note: *not* integer (i.e. int type), having float on either side of // will result in floating-point integral value). Though as usual you can do anything you like in your own class, including `rm -rf ~` when trying to use division.

I would rather call it “type-specific division”. And I think that Python-3 way is least surprising and most convenient variant. Though you should not use // for dividing non-integral floats: 1.5//0.1 == 14.0 (rounding+decimal-binary conversion issues I guess).

>
> --
> Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
> Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)



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