[swift-evolution] [Review] SE-0104: Protocol-oriented integers
Jordan Rose
jordan_rose at apple.com
Thu Jun 23 17:53:40 CDT 2016
> On Jun 23, 2016, at 14:38, Nicola Salmoria via swift-evolution <swift-evolution at swift.org> wrote:
>
> Max Moiseev via swift-evolution <swift-evolution at ...> writes:
>
>>> For FixedWidthInteger#dividedWithOverflow/remainderWithOverflow, under
> what situations would
>> you have an overflow? I could only come up with something like
> Int.min.dividedWithOverflow(-1).
>> If you look at the prototype here:
>>
> https://github.com/apple/swift/blob/master/test/Prototypes
> /Integers.swift.gyb#L789
>> there is
>> exactly the check that you’ve mentioned, but for all signed integers.
> Besides, it is very convenient to
>> have all the arithmetic operations be implemented the same way, even if
> there were no real overflows for division.
>
> I agree with this for the four basic operations, but not for the remainder
> operation.
>
> By definition, the remainder is always strictly smaller (in absolute value)
> than the divisor, so even if the division itself overflows, the remainder
> must be representable, so technically it never overflow.
>
> In the only actual case where the division overflow, that is Int.min / -1,
> the remainder is simply 0.
>
> For these reasons, I think it would make sense to explicitly request that
> the remainder operation never traps, and remove the overflow variants.
…except when taking a remainder dividing by 0, right?
Jordan
More information about the swift-evolution
mailing list