<div dir="ltr">Great, thank you Steve!<div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 5, 2017 at 5:04 PM, Steve (Numerics) 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>Hi Jens —</div><div><br></div><div>BinaryFloatingPoint is very deliberately limited to the arithmetic operations required by IEEE 754. This is the minimal set of operations that a binary floating point type should provide, but it&#39;s already a really large implementation burden for someone who wants to implement their own conforming type.</div><div><br></div><div>I agree that there should eventually be either a refinement or orthogonal protocol[s] with the semantics “type implements [a subset of] the standard math functions”, but those shouldn’t get bolted onto BinaryFloatingPoint—<wbr>implementing these functions for an arbitrary BinaryFloatingPoint type is highly non-trivial, and would make the implementation burden for a new floating point type unreasonably high. This is also out-of-scope for the current phase of Swift evolution.</div><div><br></div><div>In the short term for your immediate problem at hand, I’ve been doing something like:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">import</span><span style="font-variant-ligatures:no-common-ligatures"> Darwin</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(195,89,0)"><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">protocol</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> Math: </span><span style="font-variant-ligatures:no-common-ligatures">BinaryFloatingPoint</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _exp() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Self</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _log() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Self</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _sin() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Self</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _cos() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Self</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(195,89,0)"><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">extension</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures">Double</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">: </span><span style="font-variant-ligatures:no-common-ligatures">Math</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _exp() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Double</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">exp</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _log() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Double</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">log</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _sin() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Double</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">sin</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _cos() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Double</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">cos</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(54,86,138)"><span style="font-variant-ligatures:no-common-ligatures">extension</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Float</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Math</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _exp() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Float</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">exp</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _log() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Float</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">log</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _sin() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Float</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">sin</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> _cos() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Float</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">cos</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">self</span><span style="font-variant-ligatures:no-common-ligatures">) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> exp&lt;T: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Math</span><span style="font-variant-ligatures:no-common-ligatures">&gt;(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">_</span><span style="font-variant-ligatures:no-common-ligatures"> x: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> x.</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">_exp</span><span style="font-variant-ligatures:no-common-ligatures">() }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> log&lt;T: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Math</span><span style="font-variant-ligatures:no-common-ligatures">&gt;(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">_</span><span style="font-variant-ligatures:no-common-ligatures"> x: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> x.</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">_log</span><span style="font-variant-ligatures:no-common-ligatures">() }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> sin&lt;T: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Math</span><span style="font-variant-ligatures:no-common-ligatures">&gt;(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">_</span><span style="font-variant-ligatures:no-common-ligatures"> x: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> x.</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">_sin</span><span style="font-variant-ligatures:no-common-ligatures">() }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> cos&lt;T: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Math</span><span style="font-variant-ligatures:no-common-ligatures">&gt;(</span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">_</span><span style="font-variant-ligatures:no-common-ligatures"> x: </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">T</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">return</span><span style="font-variant-ligatures:no-common-ligatures"> x.</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">_cos</span><span style="font-variant-ligatures:no-common-ligatures">() }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(54,86,138)"><span style="font-variant-ligatures:no-common-ligatures">extension</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Math</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> {</span></div><span class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#36568a">func</span><span style="font-variant-ligatures:no-common-ligatures"> sigmoid() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#c35900">Self</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(54,86,138)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">    </span><span style="font-variant-ligatures:no-common-ligatures">return</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures">1.0</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> / (</span><span style="font-variant-ligatures:no-common-ligatures">1.0</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> + </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">exp</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">-</span><span style="font-variant-ligatures:no-common-ligatures">self</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">))</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div></span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(54,86,138)"><span style="font-variant-ligatures:no-common-ligatures">let</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> x = </span><span style="font-variant-ligatures:no-common-ligatures">1.0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(88,126,168)"><span style="font-variant-ligatures:no-common-ligatures">x</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">.</span><span style="font-variant-ligatures:no-common-ligatures">sigmoid</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">()</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures;color:#000000"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures;color:#000000">Someone else might have a more clever solution.</span></div><div><span style="font-variant-ligatures:no-common-ligatures;color:#000000">– Steve</span></div><div><br></div><div><blockquote type="cite"><div><div class="h5"><div>On Jan 5, 2017, at 8:22 AM, Jens Persson via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_-1911139565851502168Apple-interchange-newline"></div></div><div><div><div class="h5"><div dir="ltr"><div>The code below doesn&#39;t compile since there is no exponential function (exp) that works on all FloatingPoint or BinaryFloatingPoint types, also no protocol seems to define the power function or the constant e, although they do define for example: basic arithmetic operators, squareRoot() and pi.<br></div><div><br></div><div>extension BinaryFloatingPoint {</div><div>    func sigmoid() -&gt; Self {</div><div>        return 1.0 / (1.0 + exp(-self))</div><div>    }</div><div>}</div><div><br></div><div>I could, but don&#39;t want to write two free funcs sigmoid(Float) -&gt; Float and sigmoid(Double) -&gt; Double, because I need to use x.sigmoid() in several places where x is of a generic type (a BinaryFloatingPoint).<br></div><div><br></div><div>More generally: I would have the same problem if I needed eg sin or cos. Are there any particular reason why, given a generic BinaryFloatingPoint, I cannot use sin or cos, while I can use pi and squareRoot()? It does seem a bit arbitrary.</div><div><br></div><div>Any ideas on how to implement a sigmoid() that works for all BinaryFloatingPoint types?</div><div><br></div><div>/Jens</div></div></div></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></blockquote></div><br></div>