<div dir="ltr">The above is just a useful init for Double and Float. For example I use it for creating random Double&#39;s and Float&#39;s in the unit range [0, 1) from 64 bit prng like xorshift128Plus or xoroshiro128Plus.<div><br></div><div>It would also be used when trying to write something like this:<div><br></div><div>Double.init(unitRangeMapped value: Int8)</div><div>Double.init(unitRangeMapped value: UInt8)<br></div><div><div>Double.init(unitRangeMapped value: Int16)</div><div>Double.init(unitRangeMapped value: UInt16)</div></div><div>...</div><div><div>Float.init(unitRangeMapped value: Int8)</div><div>Float.init(unitRangeMapped value: UInt8)<br></div><div><div>Float.init(unitRangeMapped value: Int16)</div><div>Float.init(unitRangeMapped value: UInt16)</div></div></div><div>...</div><div><br></div><div>Or perhaps that could be better written the other way around, as instance methods for all IntegerTypes, result in Float and Double:</div><div><br></div><div>let f: Float = UInt8(123).mappedToUnitRange</div><div>let d: Double = Int8(-123).mappedToUnitRange</div><div><br></div><div>in any case, it would be nice if I could write these as one method (or computed property) in a protocol extension.</div><div><br></div><div>/Jens</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Aug 27, 2016 at 1:38 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"><div>Where does your RawSignificand input come from?  Is that really the type that you want?</div><div><br></div><div>I don’t think you really need very much boilerplate at all here.</div><div><div class="h5"><br><div><blockquote type="cite"><div>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:</div><br><div><div dir="ltr">I understand.<div>It&#39;s just very tempting to try and use the new static computed properties for eg 23 and 52 etc.</div><div>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).</div><div>/Jens</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Aug 27, 2016 at 1:25 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"><div>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.</div><div><br></div><div>It sounds like maybe you actually want to be operating directly on bitPatterns, rather than the abstract fields of the types.</div><div><br></div><div>– Steve</div><div><div><br><div><blockquote type="cite"><div>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:</div><br><div><div dir="ltr">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.<div><br></div><div>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).</div><div><br></div><div>/Jens</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Aug 27, 2016 at 1:15 AM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.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 dir="ltr">Reason for asking is that I have this:<div><br><div><div>extension Double {</div><div>    init(unitRangeFromRawSignifica<wbr>nd s: RawSignificand) {</div><div>        let bitPattern = s | (1023 &lt;&lt; 52)</div><div>        self = unsafeBitCast(bitPattern, to: Double.self) - 1.0</div><div>    }</div><div>}</div><div>extension Float {</div><div>    init(unitRangeFromRawSignifica<wbr>nd s: RawSignificand) {</div><div>        let bitPattern = s | (127 &lt;&lt; 23)</div><div>        self = unsafeBitCast(bitPattern, to: Float.self) - 1.0</div><div>    }</div><div>}</div></div><div><br></div><div>But they would be better as:</div><div>extension BinaryFloatingPoint {</div><div>    init(unitRangeFromRawSignifica<wbr>nd s: RawSignificand) {</div><div>        ... problems here, have to try casting things into RawSignificand&#39;s type ...</div><div>    }</div><div>}</div><div><br></div></div><div>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.)</div><span><font color="#888888"><div><br></div><div>/Jens</div><div><br></div></font></span></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Aug 27, 2016 at 1:02 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"><span>&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>
</span>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</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></blockquote></div><br></div>