[swift-evolution] Proposal: Remove % operator for floating-point types

Jacob Bandes-Storch jtbandes at gmail.com
Fri Dec 18 15:19:47 CST 2015


Hi Steve,
I like this idea because it removes a potential "gotcha" / unexpected
behavior, and it will encourage people not to do stuff like this with
floating-point numbers. (Somewhat like the clang analyzer's
security.FloatLoopCounter
<http://clang-analyzer.llvm.org/available_checks.html#security_checkers>
tries to do.) The remainder() behavior makes sense to me.

However, I think it'd be nice for the standard library to continue exposing
this behavior, if possible, rather than requiring the user to "import
Darwin" or "import Glibc". Can these remain as methods on the built-in
floating-point types, perhaps like .mod() and .remainder(), so they can be
used if truly desired?

Jacob

On Fri, Dec 18, 2015 at 1:12 PM, Stephen Canon via swift-evolution <
swift-evolution at swift.org> wrote:

> Hi everybody —
>
> I’d like to propose removing the “%” operator for floating-point types.
>
> *Rationale:*
> While C and C++ do not provide the “%” operator for floating-point types,
> many newer languages do (Java, C#, and Python, to name just a few).
> Superficially this seems reasonable, but there are severe gotchas when % is
> applied to floating-point data, and the results are often extremely
> surprising to unwary users.  C and C++ omitted this operator for good
> reason.  Even if you think you want this operator, it is probably doing the
> wrong thing in subtle ways that will cause trouble for you in the future.
>
> The % operator on integer types satisfies the division algorithm axiom: If
> b is non-zero and q = a/b, r = a%b, then a = q*b + r.  This property does
> not hold for floating-point types, because a/b does not produce an integral
> value.  If it did produce an integral value, it would need to be a bignum
> type of some sort (the integral part of DBL_MAX / DBL_MIN, for example, has
> over 2000 bits or 600 decimal digits).
>
> Even if a bignum type were returned, or if we ignore the loss of the
> division algorithm axiom, % would still be deeply flawed.  Whereas people
> are generally used to modest rounding errors in floating-point arithmetic,
> because % is not continuous small errors are frequently enormously
> magnified with catastrophic results:
>
> (swift) 10.0 % 0.1
>     // r0 : Double = 0.0999999999999995 // What?!
>
> [Explanation: 0.1 cannot be exactly represented in binary floating point;
> the actual value of “0.1”
> is 0.1000000000000000055511151231257827021181583404541015625.  Other than
> that rounding, the entire computation is exact.]
>
> *Proposed Approach:*
> Remove the “%” operator for floating-point types.  The operation is still
> be available via the C standard library fmod( ) function (which should be
> mapped to a Swiftier name, but that’s a separate proposal).
>
> *Alternative Considered:*
> Instead of binding “%” to fmod( ), it could be bound to remainder( ),
> which implements the IEEE 754 remainder operation; this is just like fmod(
> ), except instead of returning the remainder under truncating division, it
> returns the remainder of round-to-nearest division, meaning that if a and b
> are positive, remainder(a,b) is in the range [-b/2, b/2] rather than [0,
> b).  This still has a large discontinuity, but the discontinuity is moved
> away from zero, which makes it much less troublesome (that’s why IEEE 754
> standardized this operation):
>
> (swift) remainder(1, 0.1)
>     // r1 : Double = -0.000000000000000055511151231257827 // Looks like
> normal floating-point rounding
>
> The downside to this alternative is that now % behaves totally differently
> for integer and floating-point data, and of course the division algorithm
> still doesn’t hold.
>
> *Proposal:*
> Remove the % operator for floating-point types in Swift 3.  Add a warning
> in Swift 2.2 that points out the replacement fmod(a, b).
>
> Thanks for your feedback,
> – Steve
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151218/26ff057b/attachment.html>


More information about the swift-evolution mailing list