collect-system/packages/client/src/tools/number.ts

77 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-12-30 08:26:40 +08:00
/**
*
*
*
* 1.0.0 -
*
* 使
*
* -
* -
* -
*/
/**
*
*
*
*
*
* @param n -
* @param max -
* @returns
*
*
* O(1)
* O(1)
*/
export function upperBound(n: number, max: number) {
return n > max ? max : n;
}
/**
*
*
*
*
*
* @param n -
* @param min -
* @returns
*
*
* O(1)
* O(1)
*/
export function lowerBound(n: number, min: number) {
return n < min ? min : n;
}
/**
*
*
*
*
*
*
* lowerBound和upperBound实现双重限制
*
* @param n -
* @param min -
* @param max -
* @returns
*
* 使
* bound(5, 0, 10) // 返回5
* bound(-1, 0, 10) // 返回0
* bound(11, 0, 10) // 返回10
*
*
* O(1)
* O(1)
*/
export function bound(n: number, min: number, max: number) {
// 先应用下界限制,再应用上界限制
return upperBound(lowerBound(n, min), max);
}