<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Aug 4, 2016 at 6:20 AM, 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div id=":1f2" class="">Think about why you're <span class="">clamping</span>. It's unlikely to be used just once, for<br>
a single calculation. You're likely to be <span class="">clamping</span> a whole series of<br>
values, with a fixed lower and upper bounds. The bounds are unlikely to<br>
be known at compile-time, but they aren't going to change from <span class="">clamping</span><br>
to <span class="">clamping</span>. Something like this:<br>
<br>
lower, upper = get_bounds()<br>
for x in values():<br>
    y = some_calculation(x)<br>
    y = <span class="">clamp</span>(y, lower, upper)<br>
    do_something_with(y)<br>
<br>
<br>
is the most likely use-case, I think.<br></div></blockquote><div><br></div><div>I was curious about what the likely cases are in many cases, so I ran a quick sample from a professional project I am working on, and found the following results:</div><div><br></div><div>clamping to [0, 1]: 50 instances, almost always dealing with percentages<br></div><div>lower is 0: 44 instances, almost all were clamping an index to list bounds, though a few outliers existed<br></div><div>lower is 1: 4 instances, 3 were clamping a 1-based index, the other was some safety code to ensure a computed wait time falls within certain bounds to avoid both stalling and spamming</div><div><div>both values were constant, but with no real specific values: 11 instances (two of these are kinda 0,1 limits, but a log is being done for volume calculations, so 0 is invalid, but the number is very close to 0)</div></div><div>one value was constant, with some arbitrary limit and the other was computed: 0</div><div>both values were computed: 20 instances (many instances have the clamping pulled from data, which is generally constant but can be changed easier than code)</div><div><br></div><div>Any given call to clamp was put into the first of the categories it matched. "computed" is fairly general, it includes cases where the value is user-input with no actual math done.</div><div><br></div><div>As would be expected, all cases were using computed value as the input, only the min/max were ever constant.</div><div><br></div><div>The project in this case is a video game's game logic code, written in C#. None of the shaders or engine code is included. There may be additional clamping using min/max combinations, rather than the provided clamp helpers that were not included, however the search did find two instances, where they were commented as being clamps, which were included.</div><div><br></div><div>Basically all of the cases will repeat fairly often, either every frame, move, or level. Most are not in loops outside of the frame/game loop.</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 id=":1f2" class="">
If lower happens to be greater than upper, that's clearly a mistake. Its<br>
better to get an exception immediately, rather than run through a<br>
million calculations and only then discover that you've ended up with a<br>
million NANs. It's okay if you get a few NANs, that simply indicates<br>
that one of your x values was a NAN, or a calculation produced a NAN.<br>
But if *every* calculation produces a NAN, well, that's a sign of<br>
breakage. Hence, better to raise straight away.</div></blockquote></div><div class="gmail_extra"><br></div><div class="gmail_extra">I personally don't have much opinion on NAN behaviour in general - I don't think I've ever actually used them in any of my code, and the few cases they show up, it is due to a bug or corrupted data that I want caught early.</div><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">Chris</div></div>
</div></div>