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