36 lines
779 B
TypeScript
36 lines
779 B
TypeScript
![]() |
export function equaln(v1: number, v2: number, fuzz = 1e-5)
|
||
|
{
|
||
|
return Math.abs(v1 - v2) <= fuzz;
|
||
|
}
|
||
|
|
||
|
export function FixIndex(index: number, arr: Array<any> | number)
|
||
|
{
|
||
|
let count = (arr instanceof Array) ? arr.length : arr;
|
||
|
if (index < 0)
|
||
|
return count + index;
|
||
|
else if (index >= count)
|
||
|
return index - count;
|
||
|
else
|
||
|
return index;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param compart 如果t2大于t1那么返回t2
|
||
|
* @returns 索引
|
||
|
*/
|
||
|
export function Max<T>(arr: T[], compart: (t1: T, t2: T) => boolean): number
|
||
|
{
|
||
|
let best: T = arr[0];
|
||
|
let bestIndex = 0;
|
||
|
for (let i = 1; i < arr.length; i++)
|
||
|
{
|
||
|
let t1 = arr[i];
|
||
|
if (compart(best, t1))
|
||
|
{
|
||
|
best = t1;
|
||
|
bestIndex = i;
|
||
|
}
|
||
|
}
|
||
|
return bestIndex;
|
||
|
}
|