<div dir="ltr">Something to keep in mind:<div><br></div><div>the math module is written in C, and will remain that way for the time being (see recent discussion on, I think, this list and also the discussion when we added math.isclose()</div><div><br></div><div>which means it will be for floats only.</div><div><br></div><div>My first thought is that not every one line function needs to be in the standard library. However, as this thread shows, there are some complications to be considered, so maybe it does make sense to have them hashed out.</div><div><br></div><div>Regarding NaN:</div><br>In [4]: nan = float('nan')<br><br>In [6]: nan > 5<br><br>Out[6]: False<br><br>In [7]: 5 > nan<br><br>Out[7]: False<div><br></div><div>This follows the IEEE spec -- so the only correct result from</div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">clip(x, float('nan'))</font> is NaN.</div><div><br></div>Steven D'Aprano wrote:<div><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">I don't care too much whether the parameters are mandatory or have<br></span><span style="font-size:12.8px">defaults, so long as it is *possible* to pass something for the lower<br></span><span style="font-size:12.8px">and upper bounds which mean "unbounded". </span></blockquote><div><br></div><div>I think the point was that if one of the liimts in unbounded, then you can jsut use min or max...</div><div><br></div><div>though I think I agree -- you may have code where the limits are sometimes unbounded, and sometimes not -- nice to have a way to have only one code path.</div><div><br></div><div> <span style="font-size:12.8px">(1) Explicitly pass -INFINITY or +INFINITY as needed; </span></div><div><span style="font-size:12.8px">but which</span><br></div><div><br></div><div>that's it then.</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"><span style="font-size:12.8px">infinity, float or Decimal? If you pass the wrong one, you may have to<br></span><span style="font-size:12.8px">pay the cost of converting your values to float/Decimal, which could end<br></span><span style="font-size:12.8px">up expensive if you have a lot of them.</span></blockquote><div><br></div><div>well, as above, if it's in the math module, it's only float.... you could add one ot the Decimal module, too, I suppose.</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"><span style="font-size:12.8px">(2) Pass a NAN as the bounds. With my implementation, that actually<br></span><span style="font-size:12.8px">works! But it's a surprising accident of implementation, it feels wrong<br></span><span style="font-size:12.8px">and looks weird,</span></blockquote><div><br></div><div>and violates IEEE754 -- don't do that.</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"><span style="font-size:12.8px">(3) Use some special Infimum and Supremum objects which are smaller<br></span><span style="font-size:12.8px">than, and greater than, every other value. But we don't have such<br></span><span style="font-size:12.8px">objects, so you'd need to create your own.</span><br style="font-size:12.8px"><span style="font-size:12.8px"></span></blockquote><div><br></div><div><br></div><div>that's what <span class="">float</span><span class="">(</span><span class="">'inf'</span><span class="">) already is -- let's use them.</span></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"><span style="font-size:12.8px">(4) Use None as a placeholder for "no limit". That's my preferred<br></span><span style="font-size:12.8px">option.</span></blockquote><div><br></div><div>reasonable enough -- and would make the API a bit easier -- both for matching different types, and because there is no literal or pre-existing object for Inf.</div><div><br></div><div>-Chris </div><div><div><br></div><div><br></div><div><br></div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 31, 2016 at 7:47 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Sun, Jul 31, 2016 at 09:38:44PM +0200, Victor Stinner wrote:<br>
> I dislike this API. What's the point of calling clamp(x)? clamp(b, a) is<br>
> min(a, b) and clamp(a, max_val=b) is just max(a, b).<br>
<br>
</span>You have that the wrong way around. If you supply a lower-bounds, you<br>
must take the max(), not the min(). If you supply a upper-bounds, you<br>
take the min(), not the max(). It's easy to get wrong.<br>
<span class=""><br>
<br>
> My point is that all parameters must be mandatory.<br>
<br>
</span>I don't care too much whether the parameters are mandatory or have<br>
defaults, so long as it is *possible* to pass something for the lower<br>
and upper bounds which mean "unbounded". There are four obvious<br>
alternatives (well three obvious ones and one surprising one):<br>
<br>
(1) Explicitly pass -INFINITY or +INFINITY as needed; but which<br>
infinity, float or Decimal? If you pass the wrong one, you may have to<br>
pay the cost of converting your values to float/Decimal, which could end<br>
up expensive if you have a lot of them.<br>
<br>
(2) Pass a NAN as the bounds. With my implementation, that actually<br>
works! But it's a surprising accident of implementation, it feels wrong<br>
and looks weird, and again, it may require converting the values to<br>
float/Decimal.<br>
<br>
(3) Use some special Infimum and Supremum objects which are smaller<br>
than, and greater than, every other value. But we don't have such<br>
objects, so you'd need to create your own.<br>
<br>
(4) Use None as a placeholder for "no limit". That's my preferred<br>
option.<br>
<br>
Of course, even if None is accepted as "no limit", the caller can still<br>
explicitly provide an infinity if they prefer.<br>
<br>
As I said, I don't particularly care whether the lower and upper bounds<br>
have default values. But I think it is useful and elegant to accept None<br>
(as well as infinity) to mean "no limit".<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Steve<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R (206) 526-6959 voice<br>7600 Sand Point Way NE (206) 526-6329 fax<br>Seattle, WA 98115 (206) 526-6317 main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div>