The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,498 other followers

Archive for the ‘Algorithms’ Category

Fast inverse square root – Wikipedia

Posted by jpluimers on 2019/01/24

Cult code via [WayBack] Fast inverse square root – Wikipedia part of [WayBack] Quake-III-Arena/blob/master/code/game/q_math.c:

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;

It is a really fast way to approximate the square root for 32-bit IEEE754 calculations having origins around 1986:

  • [WayBackSymplectic Spacewar » Cleve’s Corner: Cleve Moler on Mathematics and Computing:

    Cleve Moler replied on June 27th, 2012 9:35 pm UTC :

    Jotaf — Thanks very much for your comment, and for reminding me about the fast inverse square root hack. I didn’t realize that the trick had attained a kind of cult status in the graphics community. The trick uses bit-fiddling integer operations on a floating point number to get a good starting approximation for Newton’s iteration. The Wikipedia article that you link to describes the trick in great detail, and also links to an article by Rys Sommefeldt about its origins. Sommefeldt goes back to the late ’80s and to me and my colleague Greg Walsh at Ardent Computer. I actually learned about trick from code written by Velvel Kahan and K.C. Ng at Berkeley around 1986. Here is a link to their description, in comments at the end of the fdlibm code for sqrt. . — Cleve

  • [WayBack] 

By now there is also a constant for 64-bit IEEE754 calculations 0x5fe6ec85e7de30da by [WayBack] 2003 research from Chris Lomont who also found a better 32-bit constant 0x5f375a86.

Note you need to be careful with boundary values like zero and infinity. This holds for approximations in general: [WayBackperformance – Why is SSE scalar sqrt(x) slower than rsqrt(x) * x? – Stack Overflow


Posted in Algorithms, C, Development, History, Software Development | Leave a Comment »

How to automatically choose a label color to contrast with background | TrendCT

Posted by jpluimers on 2018/11/14

Choosing label colours other than black or white is like making a dynamic mouse cursor that inverts the colours underneath it: it fails horribly in the low contrast regions, and looks very strange on pink-noise backgrounds.

This approach is uses black and white depending on the perceived brightness:

[WayBack] How to automatically choose a label color to contrast with background | TrendCT:

What would data viz be without labels? Just viz, that’s what. This guide aimed at web designers discusses how to choose a label text color with enough contrast.

Via: [WayBack] For all those people incapable of choosing the right color combinations. – Thomas Mueller (dummzeuch) – Google+


Posted in Algorithms, Development, Software Development, Usability, User Experience | Leave a Comment »

To help understanding combinations of boolean operators: Truth table – Wikipedia

Posted by jpluimers on 2018/10/04

Most software developers know they exist, but some (including me) find them hard to visualise, especially for combinations of operators, or for less common operators: the Truth table – Wikipedia.

The common operators that everyone seems to understand are these:

  • logical true
  • logical false
  • logical negation
  • logical and
  • logical or
  • logical xor

It becomes harder with a series of combinations, for instance series of and (not ...) and (not ...) and (not ...) – not to be confused with nand, similarly or (not ...) or (not ...) or (not ...) – not to be confused with nor, which both can be transformed according to the De Morgan’s laws – Wikipedia:

In set theory and Boolean algebra, these are written formally as

{\displaystyle {\begin{aligned}{\overline {A\cup B}}&={\overline {A}}\cap {\overline {B}},\\{\overline {A\cap B}}&={\overline {A}}\cup {\overline {B}},\end{aligned}}}

Using truth tables

Read the rest of this entry »

Posted in Algorithms, Conference Topics, Conferences, Development, Event, Software Development | 2 Comments »

roll a dice – Google Search

Posted by jpluimers on 2018/09/13

Happy programmers’ day!

Source: roll a dice – Google Search

Inspired by [WayBackSure, Google. Sure.


Read the rest of this entry »

Posted in Algorithms, Development, Fun, Quotes, Software Development | Leave a Comment »

Spelling with element symbols from the Periodic table

Posted by jpluimers on 2018/09/11

The [WayBackPeriodic table – Wikipedia contains many symbols.

Combing them allows you to spell word. Not all words, but many of them can be spelled.

So I was glad finding the below article that started with the same fascination I had in chemistry class.

[WayBackSpelling with Elemental Symbols

It has a great explanation of the algorithm, references to computer science literature and a nice Python implementation.

via: [WayBack] One of the best programming articles I’ve read in a while – This is why I Code – Google+


Read the rest of this entry »

Posted in Algorithms, Development, Fun, LifeHacker, Power User, Python, science, Scripting, Software Development | Leave a Comment »

%d bloggers like this: