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

时间:2013-05-21 22:25:49

标签: javascript firefox asm.js

如果我需要,比如在asm.js模块中找到数字的整数部分和小数部分,我该怎么办?没有standard operators转换为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;
}

1 个答案:

答案 0 :(得分:10)

Vyacheslav Egorov(推特:@mraleph)说:使用~~来强制转换为int。特殊验证案例:http://asmjs.org/spec/latest/#unaryexpression

a = ~~floor(n); // success!