Files
JiboSDK/node_modules/mumath/normalize.js
2026-03-22 03:21:45 +02:00

36 lines
765 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Get rid of float remainder
*
* @module mumath/normalize
*/
'use strict';
var almost = require('almost-equal');
module.exports = function(value, eps) {
//ignore ints
var rem = value%1;
if (!rem) return value;
if (eps == null) eps = Number.EPSILON || almost.FLT_EPSILON;
//pick numbers neighbour, which is way shorter, like 0.4999999999999998 → 0.5
//O(20)
var range = 5;
var len = (rem+'').length;
for (var i = 1; i < range; i+=.5) {
var left = rem - eps*i,
right = rem + eps*i;
var leftStr = left+'', rightStr = right + '';
if (len - leftStr.length > 2) return value - eps*i;
if (len - rightStr.length > 2) return value + eps*i;
// if (leftStr[2] != rightStr[2])
}
return value;
};