Last active
October 3, 2015 04:37
-
-
Save hogart/2392803 to your computer and use it in GitHub Desktop.
quantize teturn nearest bigger to `value` number, which is multiple of `quant`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return nearest bigger (by default, or lesser if the third parameter is true) to `value` number, which is multiple of `quant` | |
* @throws {Error} 'Illegal quantazing value' | |
* @param {Number} value | |
* @param {Number} quant must not be zero | |
* @param {Boolean} [toLower=false] return bigger rather than lesser nearest number | |
* @return {Number} | |
*/ | |
function quantize(value, quant, toLower) { | |
if (quant === 0) { | |
throw new Error('Illegal quantazing value') | |
} | |
var operation = Math[toLower ? 'floor' : 'ceil']; | |
return operation( value / quant ) * quant; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment