UNPKG

971 BPlain TextView Raw
1import { each, isNil } from '@antv/util';
2// 求以a为次幂,结果为b的基数,如 x^^a = b;求x
3// 虽然数学上 b 不支持负数,但是这里需要支持 负数
4export function calBase(a: number, b: number) {
5 const e = Math.E;
6 let value;
7 if (b >= 0) {
8 value = Math.pow(e, Math.log(b) / a); // 使用换底公式求底
9 } else {
10 value = Math.pow(e, Math.log(-b) / a) * -1; // 使用换底公式求底
11 }
12 return value;
13}
14
15export function log(a: number, b: number) {
16 if (a === 1) {
17 return 1;
18 }
19 return Math.log(b) / Math.log(a);
20}
21
22export function getLogPositiveMin(values, base, max?: number) {
23 if (isNil(max)) {
24 max = Math.max.apply(null, values);
25 }
26 let positiveMin = max;
27 each(values, (value) => {
28 if (value > 0 && value < positiveMin) {
29 positiveMin = value;
30 }
31 });
32 if (positiveMin === max) {
33 positiveMin = max / base;
34 }
35 if (positiveMin > 1) {
36 positiveMin = 1;
37 }
38 return positiveMin;
39}