Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Math expression evaluation error?
13.01.2015, 19:01, "Vincent Lefevre" <vincent@xxxxxxxxxx>:
> On 2015-01-12 19:18:22 +0300, ZyX wrote:
>> 12.01.2015, 12:18, "Vincent Lefevre" <vincent@xxxxxxxxxx>:
>>> 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.
>
> In POSIX, it is always an integer division.
What?!
1. How POSIX is related? Zsh is not a POSIX shell and it is not emulation mode that is being discussed here.
2. If this standard is correct: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tagtcjh_15 then it references ISO C standard. The ISO C standard (C99) says the following for multiplicative operators:
> 6.5.5 Multiplicative operators
…
> 3 The usual arithmetic conversions are performed on the operands.
> 2 Each of the operands shall have arithmetic type. The operands of the % operator shall
> have integer type.
…
> 5 The result of the / operator is the quotient from the division of the first operand by the
> second; the result of the % operator is the remainder. In both operations, if the value of
> the second operand is zero, the behavior is undefined.
> 6 When integers are divided …
Where do you see a requirement for `/` to be integer division?
3. POSIX (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04) says that “Only signed long integer arithmetic is required.” And the section referenced there talks about signed long or double. Signed long integer arithmetic is *required* to be in there, but signed long integer arithmetics is *not required* to be the *only* arithmetics present in POSIX shell.
So no, `/` has nothing to do with integer division, except that the referenced C standard expects well-defined integer division in case two integers are divided. If you have another POSIX standard or another interpretation please show it here.
>> 1.0/2 and 1/2.0 will give the same result: 0.5.
>
> because 1.0 and 2.0 don't have an integer type. Therefore the result is
> unspecified by POSIX, and zsh chooses to do a floating-point division.
>> 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
>
> For Python, but not for zsh, unfortunately. But this choice comes
> from POSIX historical behavior (and perhaps earlier).
>> I would rather call it “type-specific division”.
>
> In zsh. In POSIX, it is *always* an integer division. There is no
> floating-point there.
>
> An alternative choice zsh could have done is to return an error
> if the / operator is used in a floating-point context, and use a
> different operator for the floating-point division. However the
> use of / for integer division could still be surprising.
>
> --
> 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