在asm.js中转换int [ish]和double [ish]

Zac*_*chB 7 javascript firefox asm.js

如果我需要,比如在asm.js模块中找到数字的整数部分和小数部分,我该怎么办?没有标准运算符在intish和doubleish类型之间进行转换; 甚至Math.floor返回一个double,其结果也不能强制转换为int.

var floor = stdlib.Math.floor;

function(n) {
    n = +n;
    var a = 0;
    a = floor(n)|0; // fails: "Operands to bitwise ops must be intish"
    var b = 0.0;
    b = +(n-a); // would fail if compiler got to here
    return;
}
Run Code Online (Sandbox Code Playgroud)

Zac*_*chB 10

Vyacheslav Egorov(推特:@mraleph)说:~~用来胁迫int.特殊验证案例:http://asmjs.org/spec/latest/#unaryexpression

a = ~~floor(n); // success!
Run Code Online (Sandbox Code Playgroud)

  • 请注意,FF(夜间通道)的最新版本需要`~~ + floor(n)`,或者有时`~~ + floor(n)| 0`. (2认同)