<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Note that with the bug fixed, the result will still not be 1.nextDown, because the size of an ulp changes at 1; the values you produce will be space .ulpOfOne apart, but 1.nextDown is 1 - ulpOfOne/2.</div><div class=""><br class=""></div><div class="">– Steve</div><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 26, 2016, at 8:00 PM, Jens Persson &lt;<a href="mailto:jens@bitcycle.com" class="">jens@bitcycle.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Thanks, but there seem to be something not working the same as in my original code, here is a quick test of your code:<div class=""><br class=""><div class=""><div class="">protocol BinaryFloatingPointWithBitPattern: BinaryFloatingPoint {</div><div class="">&nbsp; &nbsp; init(bitPattern: RawSignificand)</div><div class="">&nbsp; &nbsp; var bitPattern: RawSignificand { get }</div><div class="">}</div><div class=""><br class=""></div><div class="">extension Float: BinaryFloatingPointWithBitPattern { }</div><div class="">extension Double: BinaryFloatingPointWithBitPattern { }</div><div class=""><br class=""></div><div class="">extension BinaryFloatingPointWithBitPattern {</div><div class="">&nbsp; &nbsp; init(unitRangeFromRawSignificand s: RawSignificand) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; self = Self(bitPattern: Self(1).bitPattern | s) - 1</div><div class="">&nbsp; &nbsp; }</div><div class="">}</div><div class=""><br class=""></div><div class="">typealias T = Double</div><div class="">// typealias T = Float</div><div class=""><br class=""></div><div class="">let allSignificantBitsSet = T.RawSignificand((1 &lt;&lt; T.significandBitCount) - 1)</div><div class="">print("bits set in signigicant:", String(allSignificantBitsSet, radix: 2).characters.count) // 52</div><div class="">let a = T.init(bitPattern: 0)</div><div class="">let b = T.init(bitPattern: allSignificantBitsSet)</div><div class="">print(a) // 0.0, correct.</div><div class="">print(b) // 2.2250738585072e-308. Wrong, this should be (1.0).nextDown.</div></div><div class=""><br class=""></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Aug 27, 2016 at 1:57 AM, Stephen Canon <span dir="ltr" class="">&lt;<a href="mailto:scanon@apple.com" target="_blank" class="">scanon@apple.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><span class=""><div class="">If BinaryFloatingPoint had init(_: RawSignificand), you could also just write:</div><div class=""><span style="font-family:Menlo;font-size:11px;color:rgb(54,86,138)" class=""><br class=""></span></div><div class=""><span style="font-family:Menlo;font-size:11px;color:rgb(54,86,138)" class="">extension</span><span style="font-family:Menlo;font-size:11px" class=""> </span><span style="color:rgb(195,89,0);font-family:Menlo;font-size:11px" class="">BinaryFloatingPoint</span><span style="font-family:Menlo;font-size:11px" class=""> {</span></div></span><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#36568a" class="">init</span><span class="">(<wbr class="">unitRangeFromRawSignificand s: </span><span style="color:#c35900" class="">RawSignificand</span><span class="">) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#36568a" class="">self</span><span class=""> = </span><span style="color:#c35900" class="">Self</span><span class="">(s) * .</span><span style="color:#587ea8" class="">ulpOfOne</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;line-height:normal" class="">(this is why I ask if RawSignificand is really the type you want; if you use some concrete integer type this will work).&nbsp; But once we have all the new integer protocol conformances, we’ll have a generic init from any integer type (this was already reviewed for FloatingPoint, but isn’t implementable without the Integer support), which will also make this possible.</div><div class=""><div class="h5"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 26, 2016, at 7:47 PM, Stephen Canon via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>&gt; wrote:</div><br class=""><div class=""><div style="word-wrap:break-word" class="">Assuming RawSignificand really is the type you want, I think this does what you’re looking for?<br class=""><br class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#36568a" class="">protocol</span><span class=""> BinaryFloatingPointWithBitPatt<wbr class="">ern: </span><span style="color:#c35900" class="">BinaryFloatingPoint</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#36568a" class="">init</span><span class="">(bitPattern: RawSignificand)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#36568a" class="">var</span><span class=""> bitPattern: RawSignificand { </span><span style="color:#36568a" class="">get</span><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(195,89,0)" class=""><span style="color:rgb(54,86,138)" class="">extension</span><span class="">&nbsp;</span><span class="">Float</span><span class="">:&nbsp;</span><span class="">BinaryFloatin<wbr class="">gPointWithBitPattern</span><span class="">&nbsp;{ }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(195,89,0)" class=""><span style="color:rgb(54,86,138)" class="">extension</span><span class="">&nbsp;</span><span class="">Double</span><span class="">:&nbsp;</span><span class="">BinaryFloati<wbr class="">ngPointWithBitPattern</span><span class="">&nbsp;{ }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(195,89,0)" class=""><span class=""><br class=""></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(195,89,0)" class=""><span style="color:#36568a" class="">extension</span><span class=""> </span><span class="">BinaryFloatingPointWithBitPatt<wbr class="">ern</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#36568a" class="">init</span><span class="">(<wbr class="">unitRangeFromRawSignificand s: RawSignificand) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#36568a" class="">self</span><span class=""> = </span><span style="color:#c35900" class="">Self</span><span class="">(bitPattern: </span><span style="color:#c35900" class="">Self</span><span class="">(</span><span style="color:#36568a" class="">1</span><span class="">).</span><span style="color:#587ea8" class="">bitPattern</span><span class=""> </span><span style="color:#587ea8" class="">|</span><span class=""> s) </span><span style="color:#587ea8" class="">-</span><span class=""> </span><span style="color:#36568a" class="">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><br class=""><blockquote type="cite" class="">On Aug 26, 2016, at 7:38 PM, Stephen Canon via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>&gt; wrote:<br class=""><br class="">Where does your RawSignificand input come from?&nbsp; Is that really the type that you want?<br class=""><br class="">I don’t think you really need very much boilerplate at all here.<br class=""><br class=""><blockquote type="cite" class="">On Aug 26, 2016, at 7:30 PM, Jens Persson &lt;<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>&gt; wrote:<br class=""><br class="">I understand.<br class="">It's just very tempting to try and use the new static computed properties for eg 23 and 52 etc.<br class="">I guess I'll just have to write a lot of boilerplate, or perhaps a protocol that is just implemented by Double and Float&nbsp;(that will be very similar to BinaryFloatingPoint in a lot of ways).<br class="">/Jens<br class=""><br class="">On Sat, Aug 27, 2016 at 1:25 AM, Stephen Canon&nbsp;&lt;<a href="mailto:scanon@apple.com" target="_blank" class="">scanon@apple.com</a>&gt;&nbsp;<wbr class="">wrote:<br class="">This doesn’t really scale up very well, though.&nbsp; BinaryFloatingPoint needs to also be able to model e.g. Float2048 or&nbsp;similar; we generally don't want to require that RawExponent to be the same type as RawSignificand (which I think is&nbsp;what you’re really suggesting), because in typical bignum usage significands are much larger than exponents.<br class=""><br class="">It sounds like maybe you actually want to be operating directly on bitPatterns, rather than the abstract fields of the&nbsp;types.<br class=""><br class="">– Steve<br class=""><br class=""><blockquote type="cite" class="">On Aug 26, 2016, at 7:21 PM, Jens Persson &lt;<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>&gt; wrote:<br class=""><br class="">Oh, to more directly answer your question: I don't like having to create a UInt (UInt64) value when all my bit&nbsp;manipulaton code happens in UInt32 (for Float) for example.<br class=""><br class="">The most probable context for using these computed properties and types of BinaryFloatingPoint is one in which&nbsp;specific fixed width types really matters a lot (look at the name of the protocol and the properties and assocated&nbsp;types we are talking about).<br class=""><br class="">/Jens<br class=""><br class=""><br class="">On Sat, Aug 27, 2016 at 1:15 AM, Jens Persson&nbsp;&lt;<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>&gt;&nbsp;<wbr class="">wrote:<br class="">Reason for asking is that I have this:<br class=""><br class="">extension Double {<br class="">&nbsp; &nbsp; init(<wbr class="">unitRangeFromRawSignificand s: RawSignificand) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; let bitPattern = s | (1023 &lt;&lt; 52)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; self = unsafeBitCast(bitPattern, to: Double.self) - 1.0<br class="">&nbsp; &nbsp; }<br class="">}<br class="">extension Float {<br class="">&nbsp; &nbsp; init(<wbr class="">unitRangeFromRawSignificand s: RawSignificand) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; let bitPattern = s | (127 &lt;&lt; 23)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; self = unsafeBitCast(bitPattern, to: Float.self) - 1.0<br class="">&nbsp; &nbsp; }<br class="">}<br class=""><br class="">But they would be better as:<br class="">extension BinaryFloatingPoint {<br class="">&nbsp; &nbsp; init(<wbr class="">unitRangeFromRawSignificand s: RawSignificand) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; ... problems here, have to try casting things into RawSignificand's type ...<br class="">&nbsp; &nbsp; }<br class="">}<br class=""><br class="">Please have a go at that and perhaps you see what I mean or you will come up with a nice solution that I have&nbsp;missed. (Speed is very important btw.)<br class=""><br class="">/Jens<br class=""><br class=""><br class="">On Sat, Aug 27, 2016 at 1:02 AM, Stephen Canon&nbsp;&lt;<a href="mailto:scanon@apple.com" target="_blank" class="">scanon@apple.com</a>&gt;&nbsp;<wbr class="">wrote:<br class="">&gt; On Aug 26, 2016, at 6:06 PM, Jens Persson via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>&gt; wrote:<br class="">&gt;<br class="">&gt; I can understand why<br class="">&gt; Double.RawSignificand is UInt64<br class="">&gt; and<br class="">&gt; Float.RawSignificand is UInt32<br class="">&gt;<br class="">&gt; But I can't understand why both<br class="">&gt; Double.RawExponent<br class="">&gt; and<br class="">&gt; Float.RawExponent<br class="">&gt; should be UInt.<br class="">&gt;<br class="">&gt; Why aren't they also just UInt64 and UInt32, resp.?<br class=""><br class="">Let me flip the question: why would they be UInt64 and UInt32?&nbsp; Absent a reason to prefer a specific fixed-with type, Swift integers should generally default to being [U]Int (and ideally Int, but RawExponent is&nbsp;Unsigned).<br class=""><br class="">– Steve<br class=""><br class=""><br class=""></blockquote><br class=""><br class=""></blockquote><br class="">______________________________<wbr class="">_________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-dev</a><br class=""></blockquote><br class=""></div>______________________________<wbr class="">_________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-dev</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>