javascript - Converting between int[ish] and double[ish] in asm.js - Stack Overflow

If I need to, say, find the the integer part and the fractional part of a number within an asm.js modul

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 piler got to here
    return;
}

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 piler got to here
    return;
}
Share Improve this question asked May 21, 2013 at 22:25 ZachBZachB 15.5k5 gold badges66 silver badges93 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745177311a4615250.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信