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
2
votes
3 answers

Limit a value between min and max using only arithmetic

Is it possible to limit a value in a given range, between min and max, using only arithmetic? That is, + - x / and %? I am not able to use functions such as min, max nor IF-statements. Let's assume I have a range of [1850, 1880], for any values <…
Qliver
  • 31
  • 7
2
votes
2 answers

Mathf.Clamp is resetting my eulerangle to minValue in Unity 3D

Mathf.Clamp is resetting my eulerangle to minValue when it reaches the maxValue instead of constraining it at maxValue. I have tried This : rotateX = Input.GetAxis("Mouse X") * senseX; player.transform.Rotate(player.up,…
Kaustav
  • 33
  • 6
2
votes
1 answer

Fadeout last line of text if more than n lines

We need to show product titles on a maximum of 3 lines, and fade out the 3rd line only if the title breaks onto more than 3 lines. What I tried so far to solve this is having a wrapper with a max-height of 3 lines, the title in it and a…
Raphael Jeger
  • 5,024
  • 13
  • 48
  • 79
2
votes
1 answer

How to clamp GeoJSON data format to terrain in Cesium Sandcastle?

I have terrain view in Cesium Sandcastle and I have loaded roads data in GeoJSON format, they are lines. I want to clamp them on terrain, like this example (in drop-down menu choose "Sample line positions and draw with depth test disabled") ->…
Paula
  • 31
  • 5
2
votes
4 answers

Limit integer to bounds

I'm trying to make sure that int x is greater or equal than 0 but smaller than 1080 (screen size in this case). I came up with this int x = 123; x = std::min(std::max(x, 0), 1080); This seems ugly. Is there a better way to achieve this?
Eric
  • 19,525
  • 19
  • 84
  • 147
2
votes
4 answers

Problem with my clamp macro

I have a problem with my clamp macro, when when my value is over 10 and my high is over 17 it stops working. Any idea? #define CLAMP(value, low, high) (((value)<(low))?(low):(((value)>(high))?(high):(value)))
DogDog
  • 4,820
  • 12
  • 44
  • 66
2
votes
2 answers

Clamp RotateAround is not working on Camera in Unity3D

My code is not working, I am trying to clamp the camera, but it's not working. It's snapping to 45 instantly. How can I clamp the camera? Here is my Code. using UnityEngine; using System.Collections; public class MoveCamera : MonoBehaviour { …
Jamshaid Alam
  • 515
  • 1
  • 9
  • 24
2
votes
1 answer

Is there a Objective-C or C math function to constrain a variable between a Min and a Max?

What I am looking for is a math function that constrains a primitive variable between a minimum and a maximum value in only one single function call, if it exists, in the standard math libraries for Objective-C. I currently use: float…
2
votes
4 answers

x86 assembly - optimization of clamping rax to [ 0 .. limit )

I'm writing a simple assembler procedure, which, naturally, aims to be as quick as possible. However, a certain part, which is located in the most nested loop, doesn't seem 'right' and I believe it is possible to come up with cleverer and quicker…
Rames
  • 918
  • 11
  • 27
2
votes
3 answers

Fastest Way To Clamp an Integer

I am not sure if "clamping" is the correct terminology for this, however I really don't know what else to call it. Suppose we wanted to limit an integer to stay within some arbitrary range, like 0-50. This can be easily achieved by testing the…
Krythic
  • 4,184
  • 5
  • 26
  • 67
2
votes
3 answers

forcing the columns of a matrix within different limits

I have a matrix named l having size 20X3. What I wanted to do was this : Suppose I have this limits: l1_max=20; l1_min=0.5; l2_max=20; l2_min=0.5; mu_max=20; mu_min=0.5; I wanted to force all the elements of the matrix l within the limits. The…
roni
  • 1,443
  • 3
  • 28
  • 49
1
vote
0 answers

Clamp addition evaluated early by Less

I am trying to use clamp for a font-size in my Less file. I set it to be font-size: clamp(3.429rem, -1.870rem + 6.493vw, 9.143rem);. When I build my styles however, I end up with CSS looking like font-size: clamp(3.429rem, 4.623rem, 9.143rem);. Less…
James B. Nall
  • 1,398
  • 3
  • 16
  • 27
1
vote
1 answer

How to clamp/unclamp blocks of text in jQuery or Vanilla JS?

I can't figure out how to do the following in jQuery or Vanilla JS. For the layout below I want to do the following: Compress each text block using multi-clamp library to three (3) lines. When you click on the "Read more" button or on the block…
baslie
  • 55
  • 1
  • 5
1
vote
0 answers

Why do the words of my

tag appear on top of each other despite responsive css setting?

I made this web app which seems to be functioning fine when I check its responsiveness in Chrome's Devtools Responsive modus (see photo 1), but when I open the site on a phone, the words of one of the

elements stack on top of each other when…

1
vote
1 answer

Is it possible to use clamp() inside of scale() in CSS?

Pretty simple question. Just need to know if you can use clamp() inside of scale()? Like this: .element { scale(clamp(100px, 10%, 300px)); } This does not work. How can i do this?
acharb
  • 63
  • 2
  • 7