TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

One does not simply calculate the absolute value

172 pointsby mot2baalmost 4 years ago

16 comments

MontyCarloHallalmost 4 years ago
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:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fast_inverse_square_root#Aliasing_to_an_integer_as_an_approximate_logarithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fast_inverse_square_root#Alias...</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;id-Software&#x2F;Quake-III-Arena&#x2F;blob&#x2F;dbe4ddb10315479fc00086f08e25d968b4b43c49&#x2F;code&#x2F;game&#x2F;q_math.c#L574-L578" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;id-Software&#x2F;Quake-III-Arena&#x2F;blob&#x2F;dbe4ddb1...</a>
评论 #28270822 未加载
评论 #28270456 未加载
评论 #28270370 未加载
评论 #28269918 未加载
Sharlinalmost 4 years ago
Computing the absolute value of a signed (fixed-size, two&#x27;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)`.
评论 #28272645 未加载
andi999almost 4 years ago
Does anybody know a good reference on the minus 0 thing? How do you end up with it? Is 4-4=+0.0 and -4 +4=-0.0? Or is it different?
评论 #28269407 未加载
评论 #28269427 未加载
评论 #28269474 未加载
评论 #28269460 未加载
athrowaway3zalmost 4 years ago
<p><pre><code> Wouldn&#x27;t max( x,-x ) cover everything ?</code></pre>
评论 #28268918 未加载
评论 #28268892 未加载
评论 #28268912 未加载
im3w1lalmost 4 years ago
<p><pre><code> &gt; return Double.longBitsToDouble( &gt; Double.doubleToRawLongBits(value) &amp; 0x7fffffffffffffffL); </code></pre> Technically, this will change NaN values into different ones.
评论 #28270044 未加载
评论 #28269564 未加载
ameliusalmost 4 years ago
&gt; 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.
评论 #28272981 未加载
Const-mealmost 4 years ago
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>
Koshkinalmost 4 years ago
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.
teddyhalmost 4 years ago
Does Java not have signbit()?
评论 #28273764 未加载
beervirusalmost 4 years ago
Does this correctly handle all the different NaN values, weird corner cases, etc. properly?
评论 #28268646 未加载
评论 #28269151 未加载
BlissWavesalmost 4 years ago
Alterantively one can use Math.abs().
评论 #28272382 未加载
togaenalmost 4 years ago
Easy solution: switch to C++, use std::copysign
评论 #28270942 未加载
wly_cdgralmost 4 years ago
What is this insane person doing when you can just<p>if (value &lt; 0) return -value; else if (value &gt; 0) return value; else return +0.0
评论 #28271264 未加载
评论 #28271204 未加载
literallyaduckalmost 4 years ago
Just convert to string then replace the &quot;-&quot; char with empty, then you can convert to the type you want. Sure performance suffers, but it gets you home by quitting time. Don&#x27;t actually do this.
评论 #28270982 未加载
评论 #28269467 未加载
kurthralmost 4 years ago
And don&#x27;t forget complex numbers or higher dimensionality.
评论 #28270960 未加载
da_chickenalmost 4 years ago
&gt; <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&#x27;ve used you get a division by zero error or NaN.
评论 #28268510 未加载
评论 #28268438 未加载
评论 #28268656 未加载
评论 #28269562 未加载
评论 #28268372 未加载
评论 #28268442 未加载
评论 #28269146 未加载