[swift-evolution] Fix or remove Swift.min and Swift.max?

Pyry Jahkola pyry.jahkola at iki.fi
Sun Jul 3 16:28:22 CDT 2016


+1, and bumping this topic.

The background — which I'm sure Jens is aware of — is that IEEE-754 floating point numbers can't truly conform to Comparable. (The problematic case being that neither of `x < .nan`, `x == .nan`, or `x > .nan` can be `true`.)

But given that the NaN-abolishing semantics of `fmin` is quite useful, and since we also want that Double and Float remain conforming to Comparable (albeit brokenly), could we consider fixing this issue by moving `min(_:_:)` and `max(_:_:)` into Comparable?

The Comparable protocol would become:

    public protocol Comparable : Equatable {
      func < (lhs: Self, rhs: Self) -> Bool
      func <= (lhs: Self, rhs: Self) -> Bool
      func >= (lhs: Self, rhs: Self) -> Bool
      func > (lhs: Self, rhs: Self) -> Bool
      static func minimum(lhs: Self, rhs: Self) -> Self
      static func maximum(lhs: Self, rhs: Self) -> Self
    }

with default implementations added for the new static functions, and custom implementations for FloatingPoint types. The `Swift.min` and `Swift.max` functions would then forward their logic to `Comparable.minimum` and `Comparable.maximum`.

— Pyry

> On 29 Jun 2016, Jens Persson wrote:
> 
> Hi all!
> 
> Swift.min (and Swift.max) propagates nan or not depending on the order of its args:
> 
> Swift.min(1.0, .nan) // 1.0
> Swift.min(.nan, 1.0) // nan (!)
> 
> Double.minimum(1.0, .nan) // 1.0
> Double.minimum(.nan, 1.0) // 1.0
> 
> fmin(1.0, .nan) // 1.0
> fmin(.nan, 1.0) // 1.0
> 
> The new static minimum and maximum funcs on FloatingPoint in Swift 3 shows the expected behaviour (ie the same as fmin, fmax and IEEE-754), so what should happen with Swift.min and Swift.max?
> 
> Fix, remove or perhaps something else?
> 
> https://bugs.swift.org/browse/SR-1011 <https://bugs.swift.org/browse/SR-1011>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160704/69a0a881/attachment.html>


More information about the swift-evolution mailing list