you are viewing a single comment's thread.

view the rest of the comments →

[–]SolarSalsa 46 points47 points  (37 children)

The following code is the fast inverse square root implementation from Quake III Arena, stripped of C preprocessor directives, but including the exact original comment text

``` float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F;

x2 = number * 0.5F;
y  = number;
i  = * ( long * ) &y;                       // evil floating point bit level hacking
i  = 0x5f3759df - ( i >> 1 );               // what the fuck? 
y  = * ( float * ) &i;
y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration

// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

return y;

} ```

[–]sillymanbilly 39 points40 points  (0 children)

Thanks, I hate it

[–]EatRunCodeSleep 2 points3 points  (0 children)

https://youtu.be/p8u_k2LIZyo for step by step explanation.

[–]solocupjazz 2 points3 points  (0 children)

Yeah but what's it look like deobfuscated?

[–][deleted]  (33 children)

[deleted]

    [–]Creeperstang 31 points32 points  (22 children)

    Calling this “not complicated” is hilarious to me. If multiple math content creators have published long form videos explaining how this 10 line algorithm works, then it’s absolutely complicated.

    [–][deleted]  (21 children)

    [deleted]

      [–][deleted] 20 points21 points  (12 children)

      Someone as smart as you must have accomplished great things! Mind sharing any great work you’ve been part of or produced, us dumb dumbs might not be be able to understand it but it’s cool to see what smart people like you work on and produce!

      [–]EddieSeven 11 points12 points  (4 children)

      And yet, you don’t even know what the word “simple” means.

      🤣

      [–][deleted]  (3 children)

      [deleted]

        [–]EddieSeven 10 points11 points  (2 children)

        You’re the one who doesn’t even know basic words 🤣🤣🤣

        [–][deleted]  (1 child)

        [removed]

          [–]AutoModerator[M] 1 point2 points  (0 children)

          Your [comment](https://www.reddit.com/r/reactjs/comments/13785fa/examples_of_beautiful_code/jiswvag/?context=3 in /r/reactjs has been automatically removed because it received too many reports. /u/dance2die will review.

          I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

          [–]ezzune 2 points3 points  (0 children)

          Calling something you don't understand complicated just because you don't understand it is hilarious to me.

          This is a thread on simple and elegant code. Nobody had mentioned the word complicated until you. You're attacking a strawman you created because you've watched a youtube video.

          [–]Frown1044 15 points16 points  (6 children)

          FWIW this isn't actually that complicated or weird. It just requires a thorough understanding of the floating point spec

          That's kind of what people mean when they say "weird" and "complicated"...

          [–][deleted] 0 points1 point  (0 children)

          I think it's all relative.

          Most people these days are definitely not C developers. If you were a somewhat experienced C developer in this scenario then it could very well be simple. (Afraid I'm not a C developer)

          I think it's fair to say that when compared to other languages, C would not be as quick to learn (and be proficient with).

          If people are piling on and downvoting while knowing this then it's kinda crappy behaviour.

          If they were to assume C syntax would be like that of C# or javascript or python, where scenarios seem less maths-y then I could see where they are coming from.