max (BigInt)
返回数组中最大的 BigInt。
typescript
const largest = max(numbers);INFO
此函数仅可从 es-toolkit/bigint 获得,以避免与其他数字类型的类似函数发生潜在冲突。
用法
max(nums)
当您想从多个 BigInt 中取出最大值时,请使用 max。Math.max 完全无法接受 BigInt,所以只能这样比较它们。
typescript
import { max } from 'es-toolkit/bigint';
const largest = max([1n, 5n, 3n]);
console.log(largest); // 5n
// 对负数同样适用
console.log(max([-5n, -1n, -3n])); // -1n由于 BigInt 是精确比较的,那些用 number 会舍入成同一个值的数字仍然可以区分。
typescript
import { max } from 'es-toolkit/bigint';
// 作为 `number`,这两个值都是 9007199254740992
console.log(max([9007199254740992n, 9007199254740993n])); // 9007199254740993n没有任何 BigInt 可以表示“没有最大值”,因为 BigInt 既没有 NaN 也没有 -Infinity,所以空数组会抛出错误,而不是返回一个占位值。
typescript
import { max } from 'es-toolkit/bigint';
max([]); // RangeError: Cannot find the maximum of an empty array.参数
nums(readonly bigint[]): 要搜索的BigInt数组。
返回值
(bigint): 返回数组中最大的 BigInt。
错误
如果数组为空,则抛出 RangeError。

