<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="">For any given concrete type, it’s pretty straightforward to map [0, .max] to [0,1) — note that this is a bit different from what you seem to have been doing originally, mapping e.g. [0, 2**52) to [0, 1):</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(unitRange s: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">UInt64</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">(s >> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">UInt64</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">63</span><span style="font-variant-ligatures: no-common-ligatures" class=""> - </span><span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">significandBitCount</span><span style="font-variant-ligatures: no-common-ligatures" class="">)) * .</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ulpOfOne</span><span style="font-variant-ligatures: no-common-ligatures" class="">/</span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">2</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div></div><div class=""><br class=""></div>Making this generic over integer types is painful without the SE-0104 integer protocols, however.<div class=""><br class=""></div><div class="">– Steve<br class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 26, 2016, at 8:13 PM, Jens Persson <<a href="mailto:jens@bitcycle.com" class="">jens@bitcycle.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Ah, right! Thanks again.<div class="">How would you make all integer type (UIntN, IntN) convertible/mappable from their respective [.min, .max] range to Double/Float unit range [0, 1)?</div><div class="">/Jens</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Aug 27, 2016 at 2:06 AM, Stephen Canon <span dir="ltr" class=""><<a href="mailto:scanon@apple.com" target="_blank" class="">scanon@apple.com</a>></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=""><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><div class=""><div class="h5"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 26, 2016, at 8:00 PM, Jens Persson <<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>> wrote:</div><br class=""><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 BinaryFloatingPointWithBitPatt<wbr class="">ern: BinaryFloatingPoint {</div><div class=""> init(bitPattern: RawSignificand)</div><div class=""> var bitPattern: RawSignificand { get }</div><div class="">}</div><div class=""><br class=""></div><div class="">extension Float: BinaryFloatingPointWithBitPatt<wbr class="">ern { }</div><div class="">extension Double: BinaryFloatingPointWithBitPatt<wbr class="">ern { }</div><div class=""><br class=""></div><div class="">extension BinaryFloatingPointWithBitPatt<wbr class="">ern {</div><div class=""> init(<wbr class="">unitRangeFromRawSignificand s: RawSignificand) {</div><div class=""> self = Self(bitPattern: Self(1).bitPattern | s) - 1</div><div class=""> }</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 << 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=""><<a href="mailto:scanon@apple.com" target="_blank" class="">scanon@apple.com</a>></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=""> </span><span style="color:#36568a" class="">init</span><span class="">(unitRangeFromRawSignifica<wbr class="">nd 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=""> </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=""> }</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). 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=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 26, 2016, at 7:47 PM, Stephen Canon via swift-dev <<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>> 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=""> </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=""> </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=""> </span><span class="">Float</span><span class="">: </span><span class="">BinaryFloatin<wbr class="">gPointWithBitPattern</span><span 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:rgb(54,86,138)" class="">extension</span><span class=""> </span><span class="">Double</span><span class="">: </span><span class="">BinaryFloati<wbr class="">ngPointWithBitPattern</span><span class=""> { }</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=""> </span><span style="color:#36568a" class="">init</span><span class="">(unitRangeFromRawSignifica<wbr class="">nd s: RawSignificand) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class=""> </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=""> }</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 <<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class="">Where does your RawSignificand input come from? 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 <<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>> 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 (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 <<a href="mailto:scanon@apple.com" target="_blank" class="">scanon@apple.com</a>> wrote<wbr class="">:<br class="">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'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 class=""><br class="">It sounds like maybe you actually want to be operating directly on bitPatterns, rather than the abstract fields of the types.<br class=""><br class="">– Steve<br class=""><br class=""><blockquote type="cite" class="">On Aug 26, 2016, at 7:21 PM, Jens Persson <<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>> 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 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 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 class=""><br class="">/Jens<br class=""><br class=""><br class="">On Sat, Aug 27, 2016 at 1:15 AM, Jens Persson <<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>> wr<wbr class="">ote:<br class="">Reason for asking is that I have this:<br class=""><br class="">extension Double {<br class=""> init(unitRangeFromRawSignifica<wbr class="">nd s: RawSignificand) {<br class=""> let bitPattern = s | (1023 << 52)<br class=""> self = unsafeBitCast(bitPattern, to: Double.self) - 1.0<br class=""> }<br class="">}<br class="">extension Float {<br class=""> init(unitRangeFromRawSignifica<wbr class="">nd s: RawSignificand) {<br class=""> let bitPattern = s | (127 << 23)<br class=""> self = unsafeBitCast(bitPattern, to: Float.self) - 1.0<br class=""> }<br class="">}<br class=""><br class="">But they would be better as:<br class="">extension BinaryFloatingPoint {<br class=""> init(unitRangeFromRawSignifica<wbr class="">nd s: RawSignificand) {<br class=""> ... problems here, have to try casting things into RawSignificand's type ...<br class=""> }<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 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 <<a href="mailto:scanon@apple.com" target="_blank" class="">scanon@apple.com</a>> wrote<wbr class="">:<br class="">> On Aug 26, 2016, at 6:06 PM, Jens Persson via swift-dev <<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>> wrote:<br class="">><br class="">> I can understand why<br class="">> Double.RawSignificand is UInt64<br class="">> and<br class="">> Float.RawSignificand is UInt32<br class="">><br class="">> But I can't understand why both<br class="">> Double.RawExponent<br class="">> and<br class="">> Float.RawExponent<br class="">> should be UInt.<br class="">><br class="">> 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? 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 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/mailma<wbr class="">n/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/mailma<wbr class="">n/listinfo/swift-dev</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>