如果我需要,比如说,在 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;
}
最佳答案
Vyacheslav Egorov (twitter:@mraleph) 说:使用 ~~
强制转换为 int。特殊验证案例:http://asmjs.org/spec/latest/#unaryexpression
a = ~~floor(n); // success!
关于javascript - 在 asm.js 中转换 int[ish] 和 double[ish],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16680533/