News for hackers. Stuff that matters.
Re:Low Level Bit Hacks You Absolutely Must Know by tripletao on Friday May 11, 2018 @10:38PM (#17052208)
Can you give an example of math that would be exact with 32-bit integers, but is inexact in JavaScript? Floating-point math on values that happen to be integers is exact, if an exact answer exists and fits within the mantissa.
Re:Low Level Bit Hacks You Absolutely Must Know by slededit on Friday May 11, 2018 @11:08PM (#17052287)
I think we're approaching this from philosophically different directions. On your end if the programmer controls everything correctly then its safe because doubles have a 53 bit significand.

The problem happens when you treat numbers one way and they are treated differently elsewhere. For example -0 when undergoing a bitwise +/- check (val & (1U<<31)) will appear positive when its negative by definition.

The cast to an integer prior to the bitwise operator looses information. You can use the operators safely if you know there is no information to be lost, but it is not type checked like with C. I will say at least JavaScript guarantees twos compliment. You never know when your favourite compiler will decide to break your code in the name of a faster benchmark - correctness of existing programs be damned. Implementation defined used to mean do something sane, but I digress.

Re:Low Level Bit Hacks You Absolutely Must Know by tripletao on Friday May 11, 2018 @11:53PM (#17052397)
The trick is to let your "double" only take on values that are also valid 32-bit integers, like with lots of gratuitous (x|0) and stuff. That excludes your (indeed problematic) -0. JavaScript's semantics give you enough to implement 32-bit integer math this way pretty easily. The speed may vary dramatically with small changes to the compiler, but correctness won't.
Re:Low Level Bit Hacks You Absolutely Must Know by slededit on Saturday May 12, 2018 @12:21AM (#17052453)
I still stick by my original point that:

((int)dblval) & flag

would require serious justification in a C code review. In JS its all you have. Under those restrictions I'd use it as rarely as possible. But yes you can use it correctly if you try.

Re:Low Level Bit Hacks You Absolutely Must Know by tripletao on Saturday May 12, 2018 @01:15AM (#17052591)
That's certainly true. The only reason to play these games is that JavaScript gives you no better option.