@antv/f2
Version:
Charts for mobile visualization.
65 lines (64 loc) • 1.72 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.calBase = calBase;
exports.getLogPositiveMin = getLogPositiveMin;
exports.log = log;
exports.precisionAdd = precisionAdd;
var _util = require("@antv/util");
// 求以a为次幂,结果为b的基数,如 x^^a = b;求x
// 虽然数学上 b 不支持负数,但是这里需要支持 负数
function calBase(a, b) {
var e = Math.E;
var value;
if (b >= 0) {
value = Math.pow(e, Math.log(b) / a); // 使用换底公式求底
} else {
value = Math.pow(e, Math.log(-b) / a) * -1; // 使用换底公式求底
}
return value;
}
function log(a, b) {
if (a === 1) {
return 1;
}
return Math.log(b) / Math.log(a);
}
function getLogPositiveMin(values, base, max) {
if ((0, _util.isNil)(max)) {
max = Math.max.apply(null, values);
}
var positiveMin = max;
(0, _util.each)(values, function (value) {
if (value > 0 && value < positiveMin) {
positiveMin = value;
}
});
if (positiveMin === max) {
positiveMin = max / base;
}
if (positiveMin > 1) {
positiveMin = 1;
}
return positiveMin;
}
function digitLength(num) {
// Get digit length of e
var eSplit = num.toString().split(/[eE]/);
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
/**
* 高精度加法,解决 0.1 + 0.2 !== 0.3 的经典问题
*
* @param num1 加数
* @param num2 被加数
* @return {number} 返回值
*/
function precisionAdd(num1, num2) {
var num1Digits = digitLength(num1);
var num2Digits = digitLength(num2);
var baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
return (num1 * baseNum + num2 * baseNum) / baseNum;
}
;