r/technology • • Dec 10 '13

By Special Request of the Admins Reddit’s empire is founded on a flawed algorithm

http://technotes.iangreenleaf.com/posts/2013-12-09-reddits-empire-is-built-on-a-flawed-algorithm.html
3.9k Upvotes

2.2k comments sorted by

View all comments

Show parent comment

3

u/buddhabuck Dec 10 '13

s is the score. log10(s) is only defined if s > 0 (and, since s is an integer, that effectively means if s is at least 1).

So a new posting, with no up or down votes, would crash reddit with "order = log10(s)". So would any posting with net negative votes.

So before the log, you have to take abs(s) to prevent taking the log of a negative number, and you have to special-case 0 to prevent taking the log of 0. That gives order = log10(max(abs(s),1), as in the code.

Unfortunately, log10(x) is only negative for 0<x<1, which will never be the case with a score that's an integer. So order is always positive.

It's (postulated) that it's desired that the order term should be positive when s is positive and negative when s is negative. Therefore, the sign needs to be computed so the order can be corrected. Hence, the whole rigamarole with the "if s>0..." bit to set the value of sign. Some languages have a sgn(x) function which basically does that, but apparently not the one reddit is written in.

Which basically gets us back to the code the OP claims is correct.

1

u/plus Dec 10 '13

This particular snippet is python, which most definitely has a sign function.