2013-05-21 23 views
7

如果需要在asm.js模块中查找整数部分和数字的小数部分,我该怎么做? standard operators在intish和doubleish之间都没有转换;即使Math.floor返回一个double,其结果也不能强制为int。asm.js中的int [ish]和double [ish]之间的转换

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; 
} 

回答

10

维亚切斯拉夫·叶戈罗夫(叽叽喳喳:@mraleph)说:使用~~强迫为int。特殊情况下的验证:http://asmjs.org/spec/latest/#unaryexpression

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

注意近期建立FF(夜间通道)的要求'~~ +地板(N)',或者有时'~~ +地板(N)| 0'。 – ZachB