On Sun, Jul 05, 2020 at 12:18:54PM +0900, Stephen J. Turnbull wrote:
The problem with making this a builtin is that I don't think that "clamp" is an unlikely identifier.
Doesn't matter -- we can shadow builtins, it's just a name, not a keyword. Lots of code in the wild uses str, len, id, chr, etc as variable names. I think more importantly, clamp is not important enough to be a builtin. But I wouldn't object to it being builtin if others think it is (and you can convince the Steering Council).
Which suggests the question: Is there a commonly used equivalent for complex numbers?
Naturally :-) Complex numbers represent points on a plane; it is very common in graphical toolkits to need to clamp an object to within some window or other region of the plane, so that you don't (e.g.) drag your object outside of the document, or position it somewhere off screen where it is impossible for the user to click on. (There was, or is, an annoying bug in OpenOffice that would occasionally reposition the coordinates of objects to some ludicrous position way off screen where they couldn't be reached.) Now admittedly screen and window coordinates are generally integer values, but the concept applies to floating point coordinates too: if you have a need to clamp a point to within a rectangular region, applying clamp() to the real and imaginary components will do the job.
and I'd like to toss a possible `coerce`
Here my issue is that for me the *target* of a coercion should be a "single thing", which could be a type, but might also be a scalar. It is true that type theorists consider x in Reals and y in [0,1] to be different types, so "y = coerce(x) # to unit interval" could match that concept, but somehow that doesn't work for me. That may just be me, the majority of native speakers may disagree.
No, I agree. In computing, coerce nearly always means to coerce to a type, not to coerce to some range of values. There are two standard terms for this function: clamp and clip, depending on whether you view the operation as squashing the value into a range or cutting off the bits that don't fit. I prefer clamp but could live with clip. -- Steven