Questions tagged [clamp]

Constrains a value between a lower and upper bound. Use this tag when working with a clamp() function in the languages that support it. Not to be confused with CLAMP Toolkit (Clinical Language Annotation, Modeling, and Processing).

In computer terms, to clamp a value is to make sure that it lies between some maximum and minimum values. If it’s greater than the max value, then it is replaced by the max, etc.

Clamp documentation links based on native language support:

.NET -> https://learn.microsoft.com/en-us/dotnet/api/system.math.clamp?view=netcore-3.1

C++ -> https://en.cppreference.com/w/cpp/algorithm/clamp

CSS -> MDN ; CSS Values

R -> (raster package) https://www.rdocumentation.org/packages/raster/versions/3.3-13/topics/clamp

Ruby -> (Comparable package) https://ruby-doc.org/core-2.7.0/Comparable.html#method-i-clamp

Rust -> https://docs.rs/num/0.2.1/num/fn.clamp.html

Many languages, such as C, Haskell, Java, JavaScript, PHP, Python, and Raku (Perl 6) do not have a native clamp implementation. Workarounds, custom implementations, or native feature extensions for a given language can typically be found by searching for "clamp in " here on Stack Overflow.


Not to be confused with the CLAMP Toolkit (Clinical Language Annotation, Modeling, and Processing).

140 questions
11
votes
5 answers

is there a function in C or C++ to do "saturation" on an integer

I am doing some 3D graphics and I have an open ocean. For this ocean, I have a matrix representing the sea state (i.e. wave heights) for a particular rectangular subsection of the sea. The rest of the ocean is flat. My problem is that my controlled…
lmirosevic
  • 15,787
  • 13
  • 70
  • 116
11
votes
4 answers

Why is there no clamp function in math.h

math.h goes to the trouble of providing min and max, but not a clamp function. I would have thought that as they are all usually similar implementation-wise they would all appear in the same library. Is there any particular reason that math.h does…
Pharap
  • 3,826
  • 5
  • 37
  • 51
10
votes
5 answers

Extending Generic Integer Types in Swift

So I'm trying to extend Swift's integer types with a few convenient functions that I use a lot, however I'm not clear on which protocols I should be extending. As an example, let's say I want to implement a function for clamping a value (if it's…
Haravikk
  • 3,109
  • 1
  • 33
  • 46
7
votes
3 answers

Is there a 'clamp' method/sub for ranges/Num etc in Raku (i.e. Perl6)?

Is there a 'clamp' or equivalent method or sub in Perl6? eg my $range= (1.0 .. 9.9) my $val=15.3; my $clamped=$range.clamp($val); # $clamped would be 9.9 $val= -1.3; $clamped=$range.clamp($val); # $clamped would be 1.0
drclaw
  • 2,463
  • 9
  • 23
7
votes
1 answer

std::clamp - detect if function return value is binded to const T&

It is better not to bind std::clamp return value to const ref, if one of its min or max parameters are rvalues. Typical realization of std::clamp (very simplified): template constexpr const T& clamp(const T& value, const T& min, const T&…
vladon
  • 8,158
  • 2
  • 47
  • 91
6
votes
1 answer

css clamp function with auto not working as min, value, max

I have am using css clamp() to adjust height of my div, but it doesn't work as expected. .container{ height: clamp(200px, auto, 400px); } but works well when .container{ min-height: 200px; height: auto; max-height: 400px; } from…
Abraham
  • 12,140
  • 4
  • 56
  • 92
6
votes
2 answers

pow for Integral Values

I need a version of pow for integers. I have 2 problems that I need to solve with pow: If the result is larger than my integral type I need to clamp to numeric_limits::max() I need to be able to deal with 41.99999 rounding up to 42, rather than…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
5
votes
2 answers

Cast type with range limit

Is there an elegant way to cast a bigger datatype to a smaller one without causing the result to overflow? E.g. casting 260 to uint8_t should result in 255 instead of 4. A possible solution would be: #include #include inline…
5
votes
1 answer

How does clamp() differ from setting width, max-width, & min-width?

I recently realized that you can use the CSS function clamp() in combination with width to set a "max width" and "min width". I am wondering what the fundamental difference is when using width + clamp() over min-width, width, max-width. In other…
Pat Murray
  • 4,125
  • 7
  • 28
  • 33
5
votes
1 answer

Line clamp in React using clamp.js causing Object error in IE11

Goal: Have browser render an overflow-text ellipsis look responsively when task description is over two lines for it's container, not when it isn't. Click here for a screenshot of desired outcome. Here is my React component below, the clamp we are…
Tay Hess
  • 563
  • 5
  • 11
5
votes
1 answer

Uint8ClampedList in Dart

I am curently playing with Dart and especially dart:typed_data. I stumbled across a class where I have no idea what its purpose/speciality is. I speak of Uint8ClampedList. The difference to the Uint8List in the documentation is the sentence Indexed…
jcklie
  • 4,054
  • 3
  • 24
  • 42
5
votes
1 answer

How to properly clamp beckmann distribution

I am trying to implement a Microfacet BRDF shading model (similar to the Cook-Torrance model) and I am having some trouble with the Beckmann Distribution defined in this paper: https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf Where M is…
user1855952
  • 1,515
  • 5
  • 26
  • 55
4
votes
3 answers

Can you use negative values in the CSS clamp() function?

I would love to implement fluid letter-spacing using the clamp function in CSS. But as i like my letter spacing tight, i would need to have three negative values in the formula and tried now for hours to figure this one out. Do I have to switch the…
4
votes
1 answer

Safari 14 - min(), max(), clamp() not working with vw and px values

I have a container where I want to set its font-size so it reflects on its children. I'm using the following to support font sizes for different screen sizes: .hero { font-size: clamp(4px, 1vw, 10px); } The CSS above works fine for Google…
RivasCVA
  • 91
  • 2
  • 8
4
votes
1 answer

How to apply bounds on a variable when performing optimisation in Pytorch?

I am trying to use Pytorch for non-convex optimisation, trying to maximise my objective (so minimise in SGD). I would like to bound my dependent variable x > 0, and also have the sum of my x values be less than 1000. I think I have the penalty…
Capeboom
  • 425
  • 1
  • 5
  • 13
1
2
3
9 10