<div dir="ltr"><div>On Tue, Jan 16, 2018 at 6:31 PM, Xiaodi Wu <span dir="ltr"><<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>></span> wrote:<br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span class="gmail-">On Tue, Jan 16, 2018 at 4:30 PM, Nevin Brackett-Rozinsky <span dir="ltr"><<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank">nevin.brackettrozinsky@gmail.<wbr>com</a>></span> wrote:<br></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:1px solid 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_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_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters<T: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (inches: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-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_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">2.54</span> * inches<span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s5" style="color:rgb(0,132,0)">// Error</span></font></p><p class="gmail-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_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><div>We should not ask nor expect people to constrain their generic algorithms to BinaryFloatingPoint unless they are working with the radix.<br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid 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:1px solid 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_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_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters<T: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (inches: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p>
<p class="gmail-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_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> (<span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">254</span> / <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">100</span>) * inches</font></p>
<p class="gmail-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><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><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid 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:1px solid 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't overflow until 2048 bits. The following compiles just fine:</div><span class="gmail-"><div><br></div><div>func moles<T : FloatingPoint>(particles: T) -> T {<br></div></span><div> let N_A: T = 602_214_085_774_000_000_000_<wbr>000</div><div> return particles / N_A</div><span class="gmail-"><div>}</div></span></div></div></div></blockquote><div><br></div><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><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid 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:1px solid 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_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_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> moles <T: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (particles: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-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_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span></span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">let</span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> avogadroNumber: </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> = </span>6_022_140_857<span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> * </span>100_000_000_000_000</font></p><p class="gmail-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_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> particles / avogadroNumber</font></p><p class="gmail-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_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<T : FloatingPoint & LosslessStringConvertible>(<wbr>particles: T) -> T {</div><div> let N_A = T("6.02214085774e+23")!</div><div> return particles / N_A</div><span class="gmail-"><div>}</div></span></div></div></div></blockquote><div><br></div><div>…or I could write “T: FloatingPoint & ExpressibleByFloatLiteral”. I could even make a typealias for that. But I shouldn’t have to.</div><div><br></div><div>Nevin</div></div></div></div>