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.

Evolutionary algorithm to find Quake III's fast inverse square hack with Python

173 pointsby soravuxabout 11 years ago

12 comments

chengsunabout 11 years ago
Řrřola (a famous demoscene coder [1]) found a far better constant than the original (a 2.7-fold accuracy improvement) by using an optimisation algorithm over all three constants present in the original inverse square hack.<p>His algorithm and the result (along with a plot of relative error) can be found on his blog [2].<p>[1]: See, for example, this 256-byte raycaster <a href="https://www.youtube.com/watch?v=R35UuntQQF8" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=R35UuntQQF8</a><p>[2]: <a href="http://rrrola.wz.cz/inv_sqrt.html" rel="nofollow">http:&#x2F;&#x2F;rrrola.wz.cz&#x2F;inv_sqrt.html</a>
评论 #7557592 未加载
评论 #7558911 未加载
Houshalterabout 11 years ago
There is an amazing, easy to use peice of software for doing stuff like this called Eureqa (<a href="http://www.nutonian.com/products/eureqa/" rel="nofollow">http:&#x2F;&#x2F;www.nutonian.com&#x2F;products&#x2F;eureqa&#x2F;</a>). I&#x27;ve used it to find approximations for a lot of different functions.
评论 #7556469 未加载
评论 #7557029 未加载
评论 #7556669 未加载
s-mackeabout 11 years ago
This reminds of another trick on the C64 to multiply two 8 Bit numbers a*b = [ ((a+b)&#x2F;2)^2 - ((a-b)&#x2F;2)^2 ]<p>more or less: one 8 bit additon, one 8 bit subtraction, two table lookups for the squares and one 16 bit subtraction.
评论 #7557292 未加载
anonymousUserabout 11 years ago
Hacker&#x27;s Delight [1][2] is an amazing book dedicated to discussing about all kinds of bit hacking, including fast integer square root, cube root calculation.<p>[1]: <a href="http://www.hackersdelight.org/" rel="nofollow">http:&#x2F;&#x2F;www.hackersdelight.org&#x2F;</a><p>[2]: <a href="http://www.amazon.com/Hackers-Delight-Edition-Henry-Warren/dp/0321842685" rel="nofollow">http:&#x2F;&#x2F;www.amazon.com&#x2F;Hackers-Delight-Edition-Henry-Warren&#x2F;d...</a>
hcarvalhoalvesabout 11 years ago
Now the question is: could you arrive at this algorithm without previous knowledge of what it should converge to?
评论 #7559261 未加载
评论 #7557045 未加载
lelandbateyabout 11 years ago
Is anyone else seeing a perpetual &quot;loading&quot; string where the example code is supposed to be?
评论 #7556535 未加载
EGregabout 11 years ago
Shameless plug some of you might like to read: <a href="http://www.flipcode.com/tpractice/" rel="nofollow">http:&#x2F;&#x2F;www.flipcode.com&#x2F;tpractice&#x2F;</a><p>(I used to be really into game programming when I was in my teens.)
Orangeairabout 11 years ago
Why on earth would they create a named constant for 1.5F, but not for 0x5f3759df?
评论 #7557413 未加载
jherikoabout 11 years ago
With fast native code its more than practical to search the entire space of floating point numbers and guarantee that you find the optimal magic number:<p><a href="http://jheriko-rtw.blogspot.co.uk/2009/04/understanding-and-improving-fast.html" rel="nofollow">http:&#x2F;&#x2F;jheriko-rtw.blogspot.co.uk&#x2F;2009&#x2F;04&#x2F;understanding-and-...</a><p>As already mentioned in another comment it has been taken further by Řrřola who has optimised a version with the newton step including the constants involved in that.<p><a href="http://rrrola.wz.cz/inv_sqrt.html" rel="nofollow">http:&#x2F;&#x2F;rrrola.wz.cz&#x2F;inv_sqrt.html</a>
pixelcortabout 11 years ago
For a moment I thought this was a way to workaround any patents with this hack, by rediscovering the hack dynamically; but I could be remembering a different patented technique.
评论 #7557419 未加载
评论 #7557609 未加载
alexholehouseabout 11 years ago
This is probably one of my favourite HN posts of 2014. Outstanding.
acchowabout 11 years ago
Oh, I thought this gonna be like regex golf:<p><a href="http://xkcd.com/1313/" rel="nofollow">http:&#x2F;&#x2F;xkcd.com&#x2F;1313&#x2F;</a><p>Misleading title.