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

Mathf.Clamp Function distorts the object size

I'm making the SpaceShooter game in Unity. Here is the function. void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3…
Arif Khan
  • 97
  • 1
  • 1
  • 8
0
votes
1 answer

React Clamp.js does not get applied when content loaded asynchronously

I'm using react clamp https://www.npmjs.com/package/react-clamp which uses clamp.js behind the scenes. this.props.events.map((event,i) => { return
The is a really long event name
}) This…
theUnicycleGuy
  • 145
  • 2
  • 11
0
votes
1 answer

Unity 3d, Mathf.Clamp automatically returning values

I am developing some logic and realized that clamp function not clamping rotation properly. It is always returning max rotation that I have put in the limit. Then i made another simple logic to check what is wrong. I came to know that the, Clamp…
Bilal Mirza
  • 233
  • 1
  • 3
  • 14
0
votes
1 answer

How does this nested ternary expression work?

I was making a clamp() function in php, and decided to go for a nested ternary expression just to try it. In the end, I settled with this (working) function : function clamp($value, $min, $max){ return $value<$min ? $min :…
Estecka
  • 388
  • 1
  • 15
0
votes
2 answers

Clamp gameobject to within borders while slowly following another along x and y

I am trying to make an air hockey game and I need to clamp the AI into a specific area whilst it follows the puck around the board when it's in its half. I thought the following code would work but I'm getting some weird behaviour from the paddle…
JonHerbert
  • 647
  • 1
  • 8
  • 23
0
votes
0 answers

Bug with clamp.js - a.lastChild is null

I am trying to use clamp.js but I get a bug right away. This is my code test
omega
  • 40,311
  • 81
  • 251
  • 474
0
votes
2 answers

Clamp player thrust influence

How to: clamp influence of thrust, while allowing other forces to be infinite. Example: A rocket can thrust in its rotated direction. Only explosions can push it past its top-speed. I'm looking for a theory more than code. Any help would be…
John Moore
  • 91
  • 1
  • 7
0
votes
2 answers

Gameobject does not clamp but rather jitters

I am trying to clamp the value of y for my game object to be 4 and -4 but it keeps jumping to the ymax and ymin. and the only reason i can think of is because of the last line code. i am only clamping the y values because the x and z values are not…
Iven P.
  • 25
  • 2
0
votes
1 answer

opencl- image object clamp to edge

I have two different kernels for my sobel operator. One uses buffer object and the other one uses image object. In my opinion, these two kernels should make the same result, but they're not. These two codes handle edges using (clamp to edge) Where…
eclipse0922
  • 158
  • 2
  • 15
0
votes
3 answers

Slice List of floats by value in Python

I have a list of several thousand floats that I want to be able to slice by min and max values. E.G. using: flist = [1.9842, 9.8713, 5.4325, 7.6855, 2.3493, 3.3333] (my actual list is 400,000 floats long, but the above is a working example) I want…
Xodarap777
  • 1,358
  • 4
  • 19
  • 42
0
votes
0 answers

PhysicsBody Velocity Clamp?

In my app I am simply moving one of the sprites I created in SpriteBuilder like so: [_sprite.physicsBody applyImpulse:ccp(95.0f, 0.0f)]; However, if I continuously call this, it will make the sprite go much much faster than I like. Is there a way…
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
0
votes
2 answers

Any builtin clamp method in the C++ library?

I would like to know if there is some builtin clamp method which can clamp a value between a range, say between (0,1)? clamp(a) = a if a is in (0,1) a < 0 a = 0 a > 1 a = 1
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
0
votes
1 answer

Clamp presentable value

I'm sitting here and trying to resolve an issue I have, with alternating between 0 and 1 of a variable. Basicly, I want to do something like this: float distance = 32f; float maxDistance = 32f; float clampedDistance =…
Kevin Jensen Petersen
  • 423
  • 2
  • 22
  • 43
-1
votes
2 answers

How to make a padding start at a max value when full screen but shrink gradually to minimum value as browser width gets smaller?

I've been using padding-left: clamp( 60px, calc( value - value ), 186px) but can't seem to figure out how to shrink the paddding properly when i shrink the width of my browser
MandoMando
  • 19
  • 1
  • 2
-1
votes
1 answer

How do you make the height of an element the same as the width of an element which was calculated using clamp in CSS?

I have an image element with a width of clamp(100px, 160px, 27vw);. I want to make the height of the element the same as its width. I tried using the attribute value of auto but it did not work. Is there any way to achieve it? Below is a snippet of…
iamaprogrammer
  • 115
  • 3
  • 10
1 2 3
9
10