Yup. On Wed, Oct 18, 2017 at 07:28 Ole Begemann <o...@oleb.net> wrote:
> On Tue, Oct 17, 2017, at 20:46, Xiaodi Wu via swift-evolution wrote: > > On Tue, Oct 17, 2017 at 12:54 Jonathan Hull <jh...@gbis.com> wrote: > > Why was elementsEqual created? Isn’t it meant to check equality of two > sequences in a generic context where == isn’t available? > > > No no no no no no no no. That’s precisely why the name is misleading. > elementsEqual is *not* simply a mixed-type version of ==. Remember that == > as implemented on a concrete sequence type *has no obligation* to use > elementwise comparison using the element’s implementation of ==. This is > not merely theoretical: [Float].== does *not* do an elementwise comparison > using Float.==. By contrast, you are guaranteed a true elementwise > comparison with elementsEqual regardless of how equivalence is defined for > the sequence. > > > Really? I would have thought an elementwise comparison is exactly what > [Float].== would do. > > I tested this and these are the (very surprising, at least to me) results: > > let numbers: [Float] = [1, .nan] > let copy = numbers > let sameNumbers: [Float] = [1, .nan] > > numbers == copy // (1) true (!) > numbers == sameNumbers // (2) false (!) > numbers.elementsEqual(copy) // (3) false (ok) > numbers.elementsEqual(sameNumbers) // (4) false (ok) > > So [Float].== returns a different result depending on whether you compare > two arrays that share the same buffer (1) or two arrays with the same > contents (2) when the array contains NaN. The reason is that Array.== > immediately returns true if two arrays share the same buffer > <https://github.com/apple/swift/blob/master/stdlib/public/core/Arrays.swift.gyb#L2194-L2197> > . > > Even knowing that comparing floats is problematic in the context of NaN > (and in general), I find this very surprising. It also seems to clash with > Michael's > idea > <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20171016/040544.html> > that two substitutable sequences should return true for ==. > >
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution