> On Jun 24, 2016, at 15:03, Nicola Salmoria via swift-evolution
> <[email protected]> wrote:
>
>
>
> On Fri, Jun 24, 2016 at 11:45 PM, Max Moiseev <[email protected]
> <mailto:[email protected]>> wrote:
>> However, division by 0 isn't an overflow: it's an undefined operation. I
>> find it somewhat surprising that dividedWithOverflow/remainderWithOverflow
>> allow attempting this operation.
> I tried to say that in my previous email. I agree that division by zero is
> NOT the overflow and should probably be handled differently if only for a
> better error message.
>
>>
>> To me, the intuitive semantics of the WithOverflow methods are "perform the
>> operation, and if the result doesn't fit in the given type, return a
>> truncated result and an overflow flag". This is not what happens when
>> dividing by 0, because the result simply doesn't exist.
>>
>> I think I would prefer if rhs != 0 was documented as an explicit
>> precondition of the division and remainder operations, and
>> dividedWithOverflow/remainderWithOverflow trapped because of precondition
>> failure.
> That’s exactly how it is implemented in the prototype now.
>
> Now I'm confused. Isn't this line of the gyb returning .overflow when the
> divisor is 0?
> https://github.com/apple/swift/blob/master/test/Prototypes/Integers.swift.gyb#L793
>
> <https://github.com/apple/swift/blob/master/test/Prototypes/Integers.swift.gyb#L793>
>
> Also, the current version of Swift doesn't trap either:
> let z = 0
> print(Int.divideWithOverflow(1, z)) // (0, true)
> print(Int.remainderWithOverflow(1, z)) // (0, true)
>
> interestingly, you need to put 0 in a variable otherwise the compiler rejects
> the lines.
Huh. My bad for not checking. Yeah, I don't think dividing by zero and dividing
INT_MIN by -1 are the same kind of overflow.
Jordan
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution