Alex Rivera | Logout

Python vs Javascript floating point arithmetic giving very different answers. What am I doing wrong?

Asked 2011-08-07T17:56:14.820
14

Python version | Javascript version | Whitepaper

So, I'm working on a website to calculate Glicko ratings for two player games. It involves a lot of floating point arithmetic (square roots, exponents, division, all the nasty stuff) and I for some reason am getting a completely different answer from the Python implementation of the algorithm I translated line-for-line. The Python version is giving basically the expected answer for the example found in the original whitepaper describing the algorithm, but the Javascript version is quite a bit off.

Have I made an error in translation or is Javascript's floating point math just less accurate?

Expected answer: [1464, 151.4]
Python answer: [1462, 155.5]
Javascript answer: [1470.8, 89.7]

So the rating calculation isn't THAT bad, being 99.6% accurate, but the variance is off by 2/3!

Edit: People have pointed out that the default value of RD in the Pyglicko version is 200. This is a case of the original implementer leaving in test code I believe, as the test case is done on a person with an RD of 200, but clearly default is supposed to be 350. I did, however, specify 200 in my test case in Javascript, so that is not the issue here.

Edit: Changed the algorithm to use map/reduce. Rating is less accurate, variance is more accurate, both for no discernible reason. Wallbanging commence.

Edit
Report

1 Answer

2

My guess is that involves the approximations you're using for some of the constants in the JavaScript version. Your pi2 in particular seems a little.. brief. I believe Python is using doubles for those values.

answered 2011-08-07T18:01:49.307

Your Answer