5 ms·
Thanks for this. I have one nit. You show `q = 1598029824 - u/2;` as being identical to `q = 0x5F400000 - u >> 1;`, but every language I know of uses a differ
by recursive 6y ago
Thanks for this. I have one nit. You show `q = 1598029824 - u/2;` as being identical to `q = 0x5F400000 - u >> 1;`, but every language I know of uses a different order of operations, giving different results. It might be clearest to provide parentheses in the second case.
- usmannk 6y agoInterestingly enough it seems to do the shift first in Swift
- TwoBit 6y agoAll well-known programming languages will do the shift first.
- thehobgoblin 6y agoI looked at about 20 different "popular" languages and I believe that only 2 (Go and Swift) of them have shifts at a higher precedence than addition. I think I'm missing a reference/joke =/
- hemdawgz 6y agoOr this might just be another confident but baseless HN commenter assertion.
- copascetic 6y agoC, C++, Java, and Rust all have shift as lower precedence than addition/subtraction. Edit: and Javascript, Python.
- recursive 6y agoYou can add C# as well.
- beached_whale 6y agoUse brackets to be explicit about your expected order of operations.
- timhh 6y agoOops! I added brackets, thanks!