Assume limited precision when rounding

This commit is contained in:
Tim Schaub
2021-12-09 13:22:29 -05:00
parent 01c81373b3
commit 78bf0a3679
4 changed files with 179 additions and 12 deletions
+44
View File
@@ -204,3 +204,47 @@ export function modulo(a, b) {
export function lerp(a, b, x) {
return a + x * (b - a);
}
/**
* Returns a number with a limited number of decimal digits.
* @param {number} n The input number.
* @param {number} decimals The maximum number of decimal digits.
* @return {number} The input number with a limited number of decimal digits.
*/
export function toFixed(n, decimals) {
const factor = Math.pow(10, decimals);
return Math.round(n * factor) / factor;
}
/**
* Rounds a number to the nearest integer value considering only the given number
* of decimal digits (with rounding on the final digit).
* @param {number} n The input number.
* @param {number} decimals The maximum number of decimal digits.
* @return {number} The nearest integer.
*/
export function round(n, decimals) {
return Math.round(toFixed(n, decimals));
}
/**
* Rounds a number to the next smaller integer considering only the given number
* of decimal digits (with rounding on the final digit).
* @param {number} n The input number.
* @param {number} decimals The maximum number of decimal digits.
* @return {number} The next smaller integer.
*/
export function floor(n, decimals) {
return Math.floor(toFixed(n, decimals));
}
/**
* Rounds a number to the next bigger integer considering only the given number
* of decimal digits (with rounding on the final digit).
* @param {number} n The input number.
* @param {number} decimals The maximum number of decimal digits.
* @return {number} The next bigger integer.
*/
export function ceil(n, decimals) {
return Math.ceil(toFixed(n, decimals));
}