<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 25, 2017, at 19:25, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>> wrote:</div><div class=""><div dir="ltr" class=""><br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class="">I was proposing something different where Float and Int are both Equatable and Substitutable, but neither Equatable nor Substitutable inherit from the other. The former is mathematical and the latter is not (which allows it to deal with NaN payloads, ±0, etc). Generic algorithms care mostly if not completely about mathematics, while generic containers care mostly if not completely about substitutability. They can live alongside each other and get along peacefully/sanely. And if people need to care about both, then at least they have an out.</div></div></blockquote><div class=""><br class=""></div>The issue with this is similar to that in my reply earlier about bitwise comparison of floating-point values. Yes, you can propose some total ordering over floating-point values totally divorced from `==`, but I'm quite certain that people who invoke `sort()` on an array of floating-point values don't simply want *some* deterministic order, but rather an actual increasing order of numeric values.</div></div></div></div></blockquote><div><br class=""></div>Hi Xiaodi,</div><div><br class=""></div><div>Of course, and the implementors of floating point data types would be expected to do just that. It isn’t hard. For example, in C:</div><div><br class=""></div><div><font face="Courier" class="">// Make floats sort reliably and sufficiently reasonably</font></div><div><font face="Courier" class="">bool sortableLessThan(float x, float y) {<br class=""> union {<br class=""> int i;<br class=""> float f;<br class=""> } ux = { .f = x }, uy = { .f = y };<br class=""> int high_bit = ~INT_MAX;<br class=""> int x2 = ux.i >= 0 ? ux.i : (~ux.i | high_bit);<br class=""> int y2 = uy.i >= 0 ? uy.i : (~uy.i | high_bit);<br class=""> return x2 < y2;<br class="">}<br class=""></font><br class=""></div><div>Which the compiler vectorizes down to the following branchless code (which is debatably “reasonable” for sorting):</div><div><br class=""></div><div>c.o`sortableLessThan:<br class="">c.o[0x0] <+0>: vinsertps $0x10, %xmm1, %xmm0, %xmm0 ; xmm0 = xmm0[0],xmm1[0],xmm0[2,3] <br class="">c.o[0x6] <+6>: vmovlps %xmm0, -0x8(%rsp)<br class="">c.o[0xc] <+12>: vpmovsxdq -0x8(%rsp), %xmm0<br class="">c.o[0x13] <+19>: vpcmpeqd %xmm1, %xmm1, %xmm1<br class="">c.o[0x17] <+23>: vpcmpgtq %xmm1, %xmm0, %xmm1<br class="">c.o[0x1c] <+28>: vpor 0x2c(%rip), %xmm0, %xmm2<br class="">c.o[0x24] <+36>: vpxor 0x34(%rip), %xmm2, %xmm2 ; (uint128_t) 0x00007fffffff000000007fffffff0000<br class="">c.o[0x2c] <+44>: vblendvpd %xmm1, %xmm0, %xmm2, %xmm0<br class="">c.o[0x32] <+50>: vmovd %xmm0, %eax<br class="">c.o[0x36] <+54>: vpextrd $0x2, %xmm0, %ecx<br class="">c.o[0x3c] <+60>: cmpl %ecx, %eax<br class="">c.o[0x3e] <+62>: setl %al<br class="">c.o[0x41] <+65>: retq <br class=""><br class=""></div><div><br class=""></div><div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"> Likewise, when someone asks if an array contains a floating-point value (say, `10.0 as Decimal`), they generally want to know if *any* representation of that value exists.</div></div></div></div></blockquote><div><br class=""></div><div>I’ve been thinking about the “contains” API argument a lot today, and I’m of the opinion now that we’re arguing about a problem that doesn’t exist.</div><div><br class=""></div><div>The “contains” question doesn’t make sense because of rounding errors that are inherent in floating point arithmetic. People would need to write “.contains(value: x, plusOrMinus: y)” and there is no way that the generic collection types are going to vend such an API. If anything, the “contains” API needs to be limited to types that conform to (hand waving) some kind of “PreciseValue” protocol (which floating point would not). For the exact same rounding-error reasons, I don’t think floating point types should be hashable either.</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">The point is that _what kind of substitutability_ matters, and the kind that people will expect for floating-point values is the very mathematical substitutability that is supposed to be guaranteed by Equatable, which simply does not accommodate NaN.</div></div></div></div></blockquote></div><div><br class=""></div><div>Given that rounding errors make “contains” and “hashing” impractical to use with floating point types, I don’t see any holes with my (hand waving) “Substitutability” proposal. I could be missing something though. Can you think of anything?</div><div><br class=""></div><div>Dave</div></body></html>