Live data from Hacker News

Math.min(Math.max(num, min), max)

twitter.com

221–230 of 291 posts

Re: Math.min(Math.max(num, min), max)

#221
post #5

Earlier quoted context omitted.

Or to make sure it's crystal clear what's going on: function clamp(num, min, max) { if (num > max) return max; if (num

Speaking only to JS is there any reason to write it any other way outside of being clever or as a lambda for singular use? I definitely prefer this version. (Assuming any necessary runtime checks are included for a given project)

There are many reasons to forego readability, especially when writing a library: performance, compatibility, requirements, interpreter/compiler optimizations or even cyclomatic complexity.

In lodash's case it might even be all of the above, although I can't speak for the intentions of the authors since there are no comments to guide readers through the process.

Note GP's link points to what looks like the v3 branch. Check out the latest implementation of clamp, with a few less if statements, and what looks like a NaN check using strict equality if you want your mind blown. https://github.com/lodash/lodash/blob/86a852fe763935bb64c125...

Re: Math.min(Math.max(num, min), max)

#222

Earlier quoted context omitted.

This is why computers are slow.

I was curious how slow this would be, and here is what JavaScriptCore made of this code: function clamp(n, min, max) { return [min, n, max].sort((a, b) => a - b)[1]; } for (var i = 0; i However, I was pretty disappointed when it seemed to be calling sort each time :( Perhaps I profiled it incorrectly? jsc's profiling data shows that it never hit FTL and nothing ever got inlined. The bytecode for DFG and Baseline is i…

[deleted]

Re: Math.min(Math.max(num, min), max)

#224
Kotlin implementation [1]:

    public fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int {
        if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.")
        if (this  maximumValue) return maximumValue
        return this
    }
[1] https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/c...

Re: Math.min(Math.max(num, min), max)

#227
post #118

Earlier quoted context omitted.

Both min and max should return NaN, if any of their parameters is NaN. Sorting can be defined where to place the NaNs (head/tail) but it's largely irrelevant in this case as simply the substitution won't be permitted by any compiler. NaN is part of IEEE754 but of course it's not a 'real' number (integer numbers don't have NaNs) Edit: you can consider NaN (and to a degree both infinities) as an exception, once it occu…

> Both min and max should return NaN, if any of their parameters is NaN. That's not a given, though. IEEE 754-2008 defined min and max as returning the non-NaN parameter. They have been removed in IEEE 754-2019 though.

The reference to IEEE 754 is made later on, mostly to answer the question posted. I meant regular functions in C alike languages - Math.min/max - java/javascript, fmin/fmax - C++. They do the "right" thing to propagate the NaN

Re: Math.min(Math.max(num, min), max)

#228
post #206
post #37

in k: xs: 1 2 3 4 5 max: 4 min: 2 max& min| xs 2 2 3 4 4 works for scalar, vector, matrix

K made me realize that if you use the numbers 0 and 1 for booleans, `min()` is `&&` and `max()` is `||`. love that!

same here, illuminating a-ha moment!

Re: Math.min(Math.max(num, min), max)

#229

Earlier quoted context omitted.

C++17 does have it... but it doesn't compile optimally. It compiles to something based on comparisons instead of the floating-point-specific operations. I tried this on a number of compiler combinations and didn't see anything that would emit min/max instructions for `std::clamp `. https://www.godbolt.org/z/MKqTvE

Your two functions are not equivalent. std::fmax handles NaN, but std::clamp does not.

Depends on how the comparisons are ordered. Some of the orderings I've seen in here do respect NaN by virtue of `x > upper_bound` comparing false if either x or upper_bound are NaN.

Re: Math.min(Math.max(num, min), max)

#230
post #28

I find that the fact that the functions min and max have the same name as the variables min and max increases cognitive load which makes it harder to think about it. I find the following easier to read : Math.min(Math.max(num, lower_bound), upper_bound)

It’s usually nicer to make a helper function:

  const clamp = (x, low, high) =>
    Math.max(low, Math.min(x, high));
Then it can be easily used later via a descriptive name. e.g.

  color_component = clamp(color_component, 0, 255);
Post reply on HN