Why doesn’t `NumericalArithmeticType` exist in Swift?

Asked on-list today: Why are there two mutually exclusive protocols for number types instead of a single unifying `NumericalArithmeticType`?

Jordan Rose answered:

This comes up often enough that we should probably write it somewhere, but we don’t have this protocol for the simple reason that there are very few algorithms that are correct for both integers and floating-point numbers. Even something as simple as “average” needs to be handled differently.


  • I’ve often wondered this actually. I ended up creating my own protocol for handling numbers, but I only ever use it when I really don’t need to handle precision in any significant way (which usually means using Playgrounds for simple algorithms). In general, I’m fully aware it’s a bad idea for production code.

    • Not necessarily bad for production code unless that’s what your production deems important. I have made the same “number” protocol, and It’s fine for games. I don’t care if it’s float, cgfloat, double, etc. I just need to calculate 2.2*2.2