[swift-evolution] [Proposal draft] Enhanced floating-point protocols
Joe Groff
jgroff at apple.com
Fri Apr 15 12:15:24 CDT 2016
The floating-point API design looks great. However, I'm concerned about providing a generic Arithmetic protocol:
> On Apr 14, 2016, at 4:55 PM, Stephen Canon via swift-evolution <swift-evolution at swift.org> wrote:
>
> A new protocol, Arithmetic, is introduced that provides the most basic operations (add, subtract, multiply and divide) as well as Equatable and IntegerLiteralConvertible, and is conformed to by both integer and floating- point types.
>
> There has been some resistance to adding such a protocol, owing to differences in behavior between floating point and integer arithmetic. While these differences make it difficult to write correct generic code that operates on all "arithmetic" types, it is nonetheless convenient to provide a single protocol that guarantees the availability of these basic operations. It is intended that "number-like" types should provide these APIs.
There are many other things we could do because they're "convenient", but we don't because they're wrong or mislead users into design cul-de-sacs. For example, we could provide integer indexing into strings, which would certainly be convenient, but we don't do that because it would lead to misguided accidentally-quadratic algorithms all over the place. This feels like a similar accommodation—while convenient, it makes it too easy for users to write naive real-number-arithmetic code and apply it blindly to numeric representations with very different error and overflow behavior. By including "divides" in the protocol, you're also implying a common abstraction over two *completely different* operations—integer quotient and floating-point division don't share many properties other than unfortunately sharing an operator in C.
-Joe
More information about the swift-evolution
mailing list