<div dir="ltr">On Tue, Jan 16, 2018 at 6:20 PM, Nevin Brackett-Rozinsky <span dir="ltr">&lt;<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank">nevin.brackettrozinsky@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span class="gmail-"><div>On Tue, Jan 16, 2018 at 6:31 PM, Xiaodi Wu <span dir="ltr">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br></div></span><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span class="gmail-m_859043527983772557gmail-">On Tue, Jan 16, 2018 at 4:30 PM, Nevin Brackett-Rozinsky <span dir="ltr">&lt;<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank">nevin.brackettrozinsky@gmail.<wbr>com</a>&gt;</span> wrote:<br></span><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-m_859043527983772557gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">The thing that is “broken” here is generic programming. If I constrain something to FloatingPoint, I cannot use a float literal in calculations with it:</div><div class="gmail_quote"><br></div><div class="gmail_quote">







<p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters&lt;T: <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (inches: <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">2.54</span> * inches<span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s5" style="color:rgb(0,132,0)">// Error</span></font></p><p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)">









</p><p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1">}</font></p></div></div></div></blockquote><div><br></div></span><div>Why not constrain it to `BinaryFloatingPoint`? What other types are you trying to use with this function?</div></div></div></div></blockquote><div><br></div></span><div>We should not ask nor expect people to constrain their generic algorithms to BinaryFloatingPoint unless they are working with the radix.</div></div></div></div></blockquote><div><br></div><div>On the contrary, I think the &quot;currency&quot; floating-point protocol should be `BinaryFloatingPoint`. Having worked with `FloatingPoint` rather extensively (or, attempted to at least), I reiterate that I find it to be a distinctly unhelpful protocol in terms of enabling useful generic algorithms; if we were to do anything, I&#39;d advocate for its complete removal before ABI stability sets in. For compatibility we would supply a deprecated `typealias FloatingPoint = BinaryFloatingPoint`.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-m_859043527983772557gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"> so that eg. a Rational type could be used. And that gives a hint as to the workaround:</div><div class="gmail_quote"><br></div><div class="gmail_quote">







<p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters&lt;T: <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (inches: <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p>
<p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> (<span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">254</span> / <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">100</span>) * inches</font></p>
<p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1">}</font></p></div></div></div></blockquote><div><br></div></span><div>Yes, you *could* do that.</div></div></div></div></blockquote><div><br></div></span><div>And it seems I *will* be doing that, as long as such a workaround is necessary. Though it does appear to have the unfortunate cost of an extra division operation.</div></div></div></div></blockquote><div><br></div><div>There should be no runtime cost when specialized.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-m_859043527983772557gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"></div><div class="gmail_quote">That only works for numbers which don’t overflow the integer literals though.</div></div></div></blockquote><div><br></div></span><div>Integer literals don&#39;t overflow until 2048 bits. The following compiles just fine:</div><span class="gmail-m_859043527983772557gmail-"><div><br></div><div>func moles&lt;T : FloatingPoint&gt;(particles: T) -&gt; T {<br></div></span><div>  let N_A: T = 602_214_085_774_000_000_000_00<wbr>0</div><div>  return particles / N_A</div><span class="gmail-m_859043527983772557gmail-"><div>}</div></span></div></div></div></blockquote><div><br></div></span><div><div>When I write that in a playground it shows N_A as 1.67866967797794e+18.</div><div><br></div><div>(Also, you appear to have mistakenly concatenated the standard uncertainty in the last 2 digits, “74”, onto the accepted value for the constant.)</div></div></div></div></div></blockquote><div><br></div><div>Yes, those are typos; I was typing freehand into an email. Please adjust the number and value of digits as necessary.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-m_859043527983772557gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">If we want a really large or small value then we have to split it in pieces:</div><div class="gmail_quote"><br></div><div class="gmail_quote">







<p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> moles &lt;T: <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (particles: <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p2" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(39,42,216);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">    </span></span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">let</span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> avogadroNumber: </span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> = </span>6_022_140_857<span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> * </span>100_000_000_000_000</font></p><p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> particles / avogadroNumber</font></p><p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)">










</p><p class="gmail-m_859043527983772557gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1">}</font></p></div><div class="gmail_quote"><br></div><div class="gmail_quote">It would be much nicer to write “let avogadroNumber: T = 6.022140857e23”.</div></div></div></blockquote><div><br></div></span><div>You could write:</div><div><br></div><div>func moles&lt;T : FloatingPoint &amp; LosslessStringConvertible&gt;(par<wbr>ticles: T) -&gt; T {</div><div>  let N_A = T(&quot;6.02214085774e+23&quot;)!</div><div>  return particles / N_A</div><span class="gmail-m_859043527983772557gmail-"><div>}</div></span></div></div></div></blockquote><div><br></div></span><div>…or I could write “T: FloatingPoint &amp; ExpressibleByFloatLiteral”. I could even make a typealias for that. But I shouldn’t have to.</div></div></div></div></blockquote><div><br></div><div>Sure, you could do that too. Your solution here is actually probably the best, in that if a user wishes to conform a decimal type to ExpressibleByFloatLiteral, your extensions would also work for that type. I&#39;ve already explained why the semantics of ExpressibleByFloatLiteral are such that not every FloatingPoint type could conform.</div><div><br></div></div></div></div>