How to clamp an integer to some range?
This is pretty clear, actually. Many folks learn it quickly. You can use a comment to help them.
new_index = max(0, min(new_index, len(mylist)-1))
Fastest Way To Clamp an Integer
As easy as
var normalized = Math.Min(50, Math.Max(0, value));
As of performance:
public static int Max(int val1, int val2) {
return (val1>=val2)?val1:val2;
}
That's how it's implemented in .NET, so it's unlikely you can implement it even better.
How to limit a number to be within a specified range? (Python)
def clamp(n, minn, maxn):
return max(min(maxn, n), minn)
or functionally equivalent:
clamp = lambda n, minn, maxn: max(min(maxn, n), minn)
now, you use:
n = clamp(n, 7, 42)
or make it perfectly clear:
n = minn if n < minn else maxn if n > maxn else n
even clearer:
def clamp(n, minn, maxn):
if n < minn:
return minn
elif n > maxn:
return maxn
else:
return n
Clamping floating numbers in Python?
There's no such function, but
max(min(my_value, max_value), min_value)
will do the trick.
What's the most elegant way to cap a number to a segment?
The way you do it is pretty standard. You can define a utility clamp
function:
/**
* Returns a number whose value is limited to the given range.
*
* Example: limit the output of this computation to between 0 and 255
* (x * 255).clamp(0, 255)
*
* @param {Number} min The lower boundary of the output range
* @param {Number} max The upper boundary of the output range
* @returns A number in the range [min, max]
* @type Number
*/
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
(Although extending language built-ins is generally frowned upon)
How can I safely add and clamp a signed integer in C?
You're right to worry about overflow. The obvious techniques for checking for overflow detect it after it's happened, which is of course too late, if there's any undefined behavior involved.
The standard technique is to rearrange the test. Rather than saying:
if(X + Delta > MAX) { whoops! it overflowed; }
subtract Delta
from both sides so you have
if(X > MAX - Delta) { whoops! it overflowed; }
Of course you also have to consider the possibility that Delta
is negative. So in your case I believe something like this will do it:
#define MAX (1<<30)
void UpdateX(signed int Delta) {
if(Delta >= 0) X = (X <= MAX - Delta) ? X + Delta : MAX;
else X = (X >= -MAX - Delta) ? X + Delta : -MAX;
}
See also How to check for signed integer overflow in C without undefined behaviour?
See also Question 20.6b in the C FAQ list.
Most Pythonic way to fit a variable to a range?
You can use the min and max functions:
result = min(max_value, max(min_value, result))
Standard way to clamp a number between two values in Swift
Swift 4/5
Extension of Comparable/Strideable
similar to ClosedRange.clamped(to:_) -> ClosedRange
from standard Swift library.
extension Comparable {
func clamped(to limits: ClosedRange<Self>) -> Self {
return min(max(self, limits.lowerBound), limits.upperBound)
}
}
#if swift(<5.1)
extension Strideable where Stride: SignedInteger {
func clamped(to limits: CountableClosedRange<Self>) -> Self {
return min(max(self, limits.lowerBound), limits.upperBound)
}
}
#endif
Usage:
15.clamped(to: 0...10) // returns 10
3.0.clamped(to: 0.0...10.0) // returns 3.0
"a".clamped(to: "g"..."y") // returns "g"
// this also works (thanks to Strideable extension)
let range: CountableClosedRange<Int> = 0...10
15.clamped(to: range) // returns 10
Hacks for clamping integer to 0-255 and doubles to 0.0-1.0?
This is a trick I use for clamping an int to a 0 to 255 range:
/**
* Clamps the input to a 0 to 255 range.
* @param v any int value
* @return {@code v < 0 ? 0 : v > 255 ? 255 : v}
*/
public static int clampTo8Bit(int v) {
// if out of range
if ((v & ~0xFF) != 0) {
// invert sign bit, shift to fill, then mask (generates 0 or 255)
v = ((~v) >> 31) & 0xFF;
}
return v;
}
That still has one branch, but a handy thing about it is that you can test whether any of several ints are out of range in one go by ORing them together, which makes things faster in the common case that all of them are in range. For example:
/** Packs four 8-bit values into a 32-bit value, with clamping. */
public static int ARGBclamped(int a, int r, int g, int b) {
if (((a | r | g | b) & ~0xFF) != 0) {
a = clampTo8Bit(a);
r = clampTo8Bit(r);
g = clampTo8Bit(g);
b = clampTo8Bit(b);
}
return (a << 24) + (r << 16) + (g << 8) + (b << 0);
}
Related Topics
How Include Static Files to Setuptools - Python Package
Does a File Object Automatically Close When Its Reference Count Hits Zero
Simple Argparse Example Wanted: 1 Argument, 3 Results
How to Dynamically Compose an or Query Filter in Django
Binary Representation of Float in Python (Bits Not Hex)
Getting Computer's Utc Offset in Python
Is It Safe to Replace a Self Object by Another Object of the Same Type in a Method
Format Floats with Standard JSON Module
Python Enum, When and Where to Use
Making a Countdown Timer with Python and Tkinter
How to Convert Columns into One Datetime Column in Pandas
How to Convert a Given Ordinal Number (From Excel) to a Date
How to Remove Packages Installed with Python's Easy_Install
Django Db Settings 'Improperly Configured' Error
Jupyter Notebook with Python 3.8 - Notimplementederror
Inserting Line at Specified Position of a Text File
Unsupported Operand Type(S) for +: 'Int' and 'Str'
When and How to Use the Builtin Function Property() in Python