TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

One does not simply calculate the absolute value

172 点作者 mot2ba将近 4 年前

16 条评论

MontyCarloHall将近 4 年前
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 未加载
Sharlin将近 4 年前
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 未加载
andi999将近 4 年前
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 未加载
athrowaway3z将近 4 年前
<p><pre><code> Wouldn&#x27;t max( x,-x ) cover everything ?</code></pre>
评论 #28268918 未加载
评论 #28268892 未加载
评论 #28268912 未加载
im3w1l将近 4 年前
<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 未加载
amelius将近 4 年前
&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-me将近 4 年前
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>
Koshkin将近 4 年前
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.
teddyh将近 4 年前
Does Java not have signbit()?
评论 #28273764 未加载
beervirus将近 4 年前
Does this correctly handle all the different NaN values, weird corner cases, etc. properly?
评论 #28268646 未加载
评论 #28269151 未加载
BlissWaves将近 4 年前
Alterantively one can use Math.abs().
评论 #28272382 未加载
togaen将近 4 年前
Easy solution: switch to C++, use std::copysign
评论 #28270942 未加载
wly_cdgr将近 4 年前
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 未加载
literallyaduck将近 4 年前
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 未加载
kurthr将近 4 年前
And don&#x27;t forget complex numbers or higher dimensionality.
评论 #28270960 未加载
da_chicken将近 4 年前
&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 未加载