Live data from Hacker News

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

twitter.com

61–70 of 291 posts

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

#61
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)

That doesn't really help. "Max" to enforce a "lower bound" is briefly halting.

I agree.

But "min(-, constant_x)" should be thought of as "at most constant_x" and similarly for max. Maybe there's a way to make it more expressive.

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

#62

This is a great example of how haskell makes these things obvious ;) clamped = num `max` min' `min` max'

no idea what's going on there, I know some of those variables are actually functions but the whole thing is unreadable unless you have experience in haskell imo

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

#63

  This is the TXR Lisp interactive listener of TXR 242.
  Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for  cheatsheet.
  1> (clamp 1 10 -1)
  1
  2> (clamp 1 10 15)
  10
  3> (clamp 1 10 5)
  5
Added on August 13, 2015 by commit f2e197dcd31d737bf23816107343f67e2bf6dd8e

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

#64

Python also doesn't have a built-in clamp function (I think there might be one in numpy), I usually use sorted((floor, x, ceiling))[1] Throw it in a function with some asserts if you're worried about x not being a number and giving you weird results.

There is indeed one in numpy : https://numpy.org/doc/stable/reference/generated/numpy.clip....

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

#65
post #7

Stage 1 ECMAScript proposal to add Math.clamp (among others): https://github.com/rwaldron/proposal-math-extensions But it looks dead: https://github.com/rwaldron/proposal-math-extensions/issues/... As someone mentioned in the thread, nested ternary is easier to interpret: (a > max ? max : (a < min ? min : a))

To each his own but I disagree. Nested ternary's are hard to read and understand, and modifying them (by future devs) is tricky and error prone.

[deleted]

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

#66
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)

I prefer "ceiling" and "floor", but yes, agreed.

Those also happen to be fairly common names for operations (rounding up or down to nearest integer).

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

#67
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)

That doesn't really help. "Max" to enforce a "lower bound" is briefly halting.

I would prefer it if the methods in java.lang.Math had been called "larger" and "smaller", instead of "min" and "max".

I sometimes mix up "min" as "take the minimum" rather than "take the larger given this minimum".

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

#68
post #30
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)

I think this was posted purely for the limerick quality.

  There was a young coder whose hacks
  His manager often claimed lacked
  The requisite clarity
  For to clamp vars would he:
  Math.min(Math.max(number, min), max);

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

#69

Earlier quoted context omitted.

Great. But I have been programming since about 1985.

I’ve been programming for living since 2000, but I don’t think that’s relevant. No reason not to use what’s available in standard libraries of whatever language you’re writing. For example, C++ on AMD64 is very likely to compile std::clamp into 2 instructions, minsd and maxsd. I’m not so sure about nested ternaries mentioned elsewhere in the comments.

There are very good reasons to avoid std:: stuff.

And if you don't already know that in your soul, I will appear to be a genuine crackpot, and the reasons not to use std::* will still exist.

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

#70

Earlier quoted context omitted.

That will give you the median value. What OP wants is the value `a` clamped within `min` and `max`.

Can you give an example where median([a, min, max]) is not equal to clamp(a, min, max), given max >= min?

If a max, if I'm reading it right.
Post reply on HN