Why is Math.pow() (sometimes) not equal to ** in JavaScript?

后端 未结 1 349
谎友^
谎友^ 2021-01-30 06:05

I\'ve just discovered the ECMAScript 7 feature a**b as an alternative for Math.pow(a,b) (MDN Reference) and came across a discussion in that post, in w

相关标签:
1条回答
  • 2021-01-30 06:44

    99**99 is evaluated at compile time ("constant folding"), and the compiler's pow routine is different from the runtime one. When evaluating ** at run time, results are identical with Math.pow — no wonder since ** is actually compiled to a Math.pow call:

    console.log(99**99);           // 3.697296376497268e+197
    a = 99, b = 99;
    console.log(a**b);             // 3.697296376497263e+197
    console.log(Math.pow(99, 99)); // 3.697296376497263e+197

    Actually

    9999=369729637649726772657187905628805440595668764281741102430259972423552570455277523421410650010128232727940978889548326540119429996769494359451621570193644014418071060667659301384999779999159200499899

    so the first result is a better approximation, still such a discrepancy between constant- and dynamic expressions shouldn't take place.

    This behavior looks like a bug in V8. It has been reported and will hopefully get fixed soon.

    0 讨论(0)
提交回复
热议问题