JavaScript中Math.imul()函数的用途是什么?

Math.imul()

与其他乘法函数不同,Math.imul()函数返回两个参数的类似C的32位乘法结果。它在Emscripten等项目中的应用非常高。 

语法

var product = Math.imul(a, b);

该方法采用两个数字并给出其乘法值。

示例1

在下面的示例中,两个正常整数作为方法Math.Imul()的参数给出,并且所获得的结果显示为输出中所示。

<html>
<body>
<script>
   document.write(Math.imul(3, 4));
   document.write("</br>");
   document.write(Math.imul(-3, 4));
</script>
</body>
</html>

输出结果

12
-12


示例2

在下面的示例中,将类似c的32位值作为方法Math.Imul()的参数给出,并显示所获得的结果,如输出所示

<html>
<body>
<script>
   document.write(Math.imul(0xffffffff, 4));
   document.write("</br>");
   document.write(Math.imul(0xfffffffe, 4));
</script>
</body>
</html>

输出结果

-4
-8