[英]Converting between int[ish] and double[ish] in asm.js
If I need to, say, find the the integer part and the fractional part of a number within an asm.js module, how do I do it? 如果我需要,比如在asm.js模块中找到数字的整数部分和小数部分,我该怎么办? None of the standard operators convert between intish and doubleish types;
没有标准运算符在intish和doubleish类型之间进行转换; even Math.floor returns a double, and its result can't be coerced to an int.
甚至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) says: use ~~
to coerce to an int. Vyacheslav Egorov(推特:@mraleph)说:用
~~
来强制转换为int。 Special validation case: http://asmjs.org/spec/latest/#unaryexpression 特殊验证案例: http : //asmjs.org/spec/latest/#unaryexpression
a = ~~floor(n); // success!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.