Math.imul()

Math.imul() 静态方法返回两个参数的类似 C 32 位乘法的结果。

¥The Math.imul() static method returns the result of the C-like 32-bit multiplication of the two parameters.

Try it

语法

¥Syntax

js
Math.imul(a, b)

参数

¥Parameters

a

第一个号码。

b

第二个数字。

返回值

¥Return value

给定参数的类 C 32 位乘法的结果。

¥The result of the C-like 32-bit multiplication of the given arguments.

描述

¥Description

Math.imul() 允许使用类似 C 语义的 32 位整数乘法。此功能对于 恩脚本 这样的项目很有用。

¥Math.imul() allows for 32-bit integer multiplication with C-like semantics. This feature is useful for projects like Emscripten.

因为 imul()Math 的静态方法,所以你始终将其用作 Math.imul(),而不是用作你创建的 Math 对象的方法(Math 不是构造函数)。

¥Because imul() is a static method of Math, you always use it as Math.imul(), rather than as a method of a Math object you created (Math is not a constructor).

如果你在 imul() 中使用普通的 JavaScript 浮点数,你将会遇到性能下降的情况。这是因为从浮点转换为整数进行乘法,然后将相乘的整数转换回浮点的成本很高。然而,对于 asm.js,它允许 JIT 优化器更自信地在 JavaScript 中使用整数,将内部存储为整数的两个数字(这只能在 asm.js 中实现)与 imul() 相乘可能会提高性能。

¥If you use normal JavaScript floating point numbers in imul(), you will experience a degrade in performance. This is because of the costly conversion from a floating point to an integer for multiplication, and then converting the multiplied integer back into a floating point. However, with asm.js, which allows JIT-optimizers to more confidently use integers in JavaScript, multiplying two numbers stored internally as integers (which is only possible with asm.js) with imul() could be potentially more performant.

示例

¥Examples

使用 Math.imul()

¥Using Math.imul()

js
Math.imul(2, 4); // 8
Math.imul(-1, 8); // -8
Math.imul(-2, -2); // 4
Math.imul(0xffffffff, 5); // -5
Math.imul(0xfffffffe, 5); // -10

规范

Specification
ECMAScript Language Specification
# sec-math.imul

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also