It’s interesting how fairly complex bit twiddling hacks are well-known for integers, but even very simple but useful bit twiddles for floats (like this one) are obscure enough to warrant their own article.<p>Of course, the most famous bit manipulation of floats is the fast inverse square root trick [0]. The bits of a float as an integer literal are proportional to its log2 value (plus a constant), which is handy for computing a reciprocal square root in logspace, which when converted back to a float gets exponentiated, yielding the answer (after a couple more refinement steps). If you actually take the time to manually hammer out the bitwise manipulations, the fast inverse square root is quite simple, but it is nonetheless regarded as black magic, likely due to the totally non-explanatory comments in the Quake source code.<p>Funnily enough, directly following the fast inverse square root in the Quake source code is a function to compute absolute values by zeroing the float sign bit [1], just as described in the article.<p>[0] <a href="https://en.wikipedia.org/wiki/Fast_inverse_square_root#Aliasing_to_an_integer_as_an_approximate_logarithm" rel="nofollow">https://en.wikipedia.org/wiki/Fast_inverse_square_root#Alias...</a><p>[1] <a href="https://github.com/id-Software/Quake-III-Arena/blob/dbe4ddb10315479fc00086f08e25d968b4b43c49/code/game/q_math.c#L574-L578" rel="nofollow">https://github.com/id-Software/Quake-III-Arena/blob/dbe4ddb1...</a>
Computing the absolute value of a signed (fixed-size, two's complement) integer is not trivial either, because there is no positive i such that `abs(INT_MIN) == i`. Some languages like Java just give you `abs(INT_MIN) == INT_MIN` because:<p><pre><code> -100…000 = ~100…000 + 1 = 011…111 + 1 = 100…000
</code></pre>
where ~ is the bitwise complement. Even worse, in C and C++ specifically, because overflow of signed integers is undefined behavior, so is `abs(INT_MIN)`! To be able to represent the absolute value, you need to either return the corresponding unsigned type (if your language has them), promote to a wider type, or return a tuple `(carry_bit, result)`.
<p><pre><code> > return Double.longBitsToDouble(
> Double.doubleToRawLongBits(value) & 0x7fffffffffffffffL);
</code></pre>
Technically, this will change NaN values into different ones.
> After such interviews, there will be rumors about your company.<p>It makes no sense to bring this up in interview questions. Just about every programmer knows that IEEE 754 contains some quirks. If you want to know the boring details, just look them up in the manual.
PC processors have bitwise instructions processing floats.<p>When I need absolute value, I usually do it like that in C++:<p><pre><code> return _mm_andnot_pd( _mm_set1_pd( -0.0 ), vec );</code></pre>
In programming, the result of a comparison of floating point numbers in general and a comparison with zero in particular is problem (and scale) dependent. Some values that are close to zero are actually zero, within a certain margin of error (the “epsilon”). I wish computers directly operated on intervals instead of single points.
Just convert to string then replace the "-" char with empty, then you can convert to the type you want. Sure performance suffers, but it gets you home by quitting time. Don't actually do this.
> <i>For example, if you divide 1.0 by +0.0 and -0.0, you get completely different answers: +Infinity and -Infinity.</i><p>This must be a Java-ism. In most languages I've used you get a division by zero error or NaN.