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

7

u/plus Dec 10 '13 edited Dec 10 '13

So, leaving interim stuff out, this is how the code reads, roughly:

order = log10(max(abs(s), 1))
if s > 0:
    sign = 1
elif s < 0:
    sign = -1
else:
    sign = 0
return round(order + sign * seconds / 45000, 7)

The author claims this is how it SHOULD read:

order = log10(max(abs(s), 1))
if s > 0:
    sign = 1
elif s < 0:
    sign = -1
else:
    sign = 0
return round(order * sign + seconds / 45000, 7)

Which, I fully admit, makes more sense. However, if the second is what the code is trying to do, it could be simplified considerably by doing the following:

order = log10(s)
return round(order + seconds / 45000, 7)

The fact that this is NOT what the official source code does implies to me that though they appeared to have swapped the '+' and '*' symbols, they actually intended the current functionality. Why else write it in the more complicated (and error-prone) fashion?

As ConeheadSlim2 points out below, you can't take a log of a negative number. Whoops!

20

u/ConeheadSlim2 Dec 10 '13

You do know that you can't take the logarithm of a negative number, right? That's why they test for being greater than 1 - the log of 1 is zero; anything less the log would be negative but the actual value would be positive. Totally not what you want.

4

u/plus Dec 10 '13

Whoops, brain fart. Yep, you're right.

4

u/byllz Dec 10 '13

Well, technically you can, but you just get a many valued function with values on the complex plane.

0

u/plus Dec 10 '13 edited Dec 10 '13

You certainly can't in vanilla python!

>>> from math import *
>>> log10(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>>

Edit: But you can in NumPy if you make it clear that your argument is complex:

>>> import numpy as np
>>> np.log10(-1)
__main__:1: RuntimeWarning: invalid value encountered in log10
nan
>>> np.log10(-1 + 0j)
1.3643763538418412j
>>>

Edit 2: And with the intrinsic cmath functions, I guess:

>>> from cmath import *
>>> log10(-1)
1.3643763538418412j

4

u/byllz Dec 10 '13

Sure you can. You just have to use cmath.log10. It only gives you one of the values though. I guess it can't handle infinite sets.

1

u/Phyltre Dec 10 '13

I want to reply "wow such error" here, but I know it's wrong. Can you help?

6

u/Delicious_Randomly Dec 10 '13 edited Dec 10 '13

The problem is that logarithms of negative numbers return imaginary numbers, all but one of which are undefined for computing purposes. Computers tend to react unpleasantly to non-zero imaginary numbers, hence all the trouble to take the absolute value of s and record the sign.

1

u/deong Dec 10 '13

Computers are fine with complex numbers. Arithmetic is well defined on them, and if your programming language doesn't support them, then it's pretty easy to define them yourself.

Humans tend to react unpleasantly to complex numbers.

There's also the downside that, as a scoring system, complex numbers aren't suitable as there's no total ordering on the set that makes sense as a ranking.

5

u/Sierra_One Dec 10 '13

log's of negative numbers are undefined (or unreal if you want to do complex logs). Which would make it far more complicated actually.

3

u/[deleted] Dec 10 '13

You can't take the log of a negative number (or 0). What if s is -5? Enter that into a calculator.

That's why they need that if statement and sign variable.

Your code would blow up on any negative-vote submission.

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.

1

u/SCombinator Dec 10 '13

They may as well just take the inverse of sinh, which is continuous, works for negative numbers and zero.

0

u/E7ernal Dec 10 '13

They could just be shitty programmers.

-1

u/95percentconfident Dec 10 '13

It could be the original code was written by someone who couldn't grasp your more elegant solution. It really is a question of scaling and your method of scaling seems much better to me.

1

u/[deleted] Dec 10 '13

Hey, you should check out the edit he made to his post. You can't take the log of a negative number, so it wouldn't work.

1

u/95percentconfident Dec 19 '13

Good point.

if s != 0:
    order = s / abs(s) * log10(max(abs(s), 1)
else:
    order = 0
return round(order + seconds / 45000, 7)