是否有类似于Math的库支持JavaScript BigInt?

是否有类似于Math的库支持JavaScript BigInt?,javascript,bigint,Javascript,Bigint,我正在尝试使用数学库的一些函数,例如(pow、floor等)。然而,当我尝试使用像这样的大整数时 let x = Math.pow(100n, 100n); 我明白了 TypeError:无法将BigInt值转换为数字 当然我可以自己实现,比如 const BigMath ={ pow(num, pow){ let total; for(let i = 0; i < pow; i++){ if(!total) total = num; else

我正在尝试使用数学库的一些函数,例如(pow、floor等)。然而,当我尝试使用像这样的大整数时

let x = Math.pow(100n, 100n);
我明白了

TypeError:无法将BigInt值转换为数字

当然我可以自己实现,比如

const BigMath ={
  pow(num, pow){
    let total;
    for(let i = 0; i < pow; i++){
      if(!total) total = num;
      else total = total * num;
    }
    return total;
  }
} 
let x = BigMath.pow(100n, 100n);
const BigMath={
战力(num,pow){
让总;
for(设i=0;i
但是我不想回去重新实现所有的函数。特别是从我的实现来看,它应该能够处理它,没有问题


那么我如何处理数学。*和BigInt?

BigInts是一种不同的数据类型,它有自己的方式,你不能让compare不在表达式中使用它们

typeof 123;  // 'number'
typeof 123n; // 'bigint'
数字不能表示超出安全整数限制的bigint,这就是为什么不允许在bigint和数字之间混合运算(比较运算符==,)的原因,因为任何隐式转换都可能丢失信息

所以,您可以做的是添加隐式强制转换

Number( Math.pow( Number(100n) , Number(100n) ) ); // 1.0000000000000005e+200
对于
pow()
您只需使用
**
运算符:

Math.pow(2175)
//4.789048565205903e+52
2**175
//4.789048565205903e+52
2n**175n
//47890485652059026823698344598447161988085597568237568n
floor()
像大多数
Math
函数一样,与整数无关

事实上,只有5个数学函数与整数相关:

  • Math.abs()
  • Math.max()
  • Math.min()
  • Math.pow()
  • 数学符号()
其他函数与实数有关:

  • 三角函数(
    cos
    acos
    sin
    asin
    tan
    atan
    atan2
  • 双曲函数(
    cosh
    acosh
    sinh
    asinh
    tanh
    atanh
  • 根(
    sqrt
    cbrt
    hypot
  • 圆形(
    Round
    ceil
    floor
    trunc
  • 对数(
    exp
    expm1
    log
    log10
    log1p
    log2
  • 随机(
    Random
  • 位(
    clz32
    fround
    imul
因此,这里是
BigInt
Math
等价物:

const bigMath={
abs(x){
返回x<0n?-x:x
},
符号(x){
如果(x==0n)返回0n
返回x<0n?-1n:1n
},
功率(基准、指数){
返回基**指数
},
最小值(值,…值){
for(设i=0;i值)值=值[i]
返回值
},
}

当它转换成数字时,这一轮不是吗?是的,它会,javascript中没有内置的方法来
pow
BigInts如果这是你的观点,那么已经存在一个允许它的方法。