Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? None of the standard operators convert between intish and doubleish types; even Math.floor returns a double, and its result can't be coerced to an 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;
}
like image 660
ZachB Avatar asked May 21 '13 22:05

ZachB


1 Answers

Vyacheslav Egorov (twitter:@mraleph) says: use ~~ to coerce to an int. Special validation case: http://asmjs.org/spec/latest/#unaryexpression

a = ~~floor(n); // success!
like image 170
ZachB Avatar answered Oct 22 '22 02:10

ZachB