Why does OR 0 round numbers in Javascript?

前端 未结 3 1735
天命终不由人
天命终不由人 2021-01-21 02:06

I\'m under the impression that the Number type in Javascript stores any number, integer or float, according to the IEEE floating point standard. If so, then why does bitwise OR-

3条回答
  •  不思量自难忘°
    2021-01-21 02:23

    At language level there is only floats, and temporary integers for bitwise operators.

    Per spec, a float is turned into a 32-bit integer by doing the abstract operation:

    var n = (sign(number) * floor(abs(number))) % pow(2, 32);
    if( n >= pow( 2, 31 ) ) {
         return n - pow( 2, 32 );
    }
    else {
         return n;
    }
    

    I must emphasize that this operation is abstract and certainly not done in the above fashion by any engine so don't draw any performance considerations from it. (This goes for any other spec operation too)

提交回复
热议问题