Python与Java浮点算术给出了截然不同的答案.我究竟做错了什么? [英] Python vs Javascript floating point arithmetic giving very different answers. What am I doing wrong?

查看:87
本文介绍了Python与Java浮点算术给出了截然不同的答案.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python版本 | JavaScript版本 | 白皮书

因此,我正在一个网站上计算两个玩家游戏的Glicko评分.它涉及很多浮点算法(平方根,指数,除法,所有令人讨厌的东西),由于某种原因,我得到的答案与我逐行翻译的算法的Python实现完全不同.对于描述该算法的原始白皮书中的示例,Python版本基本上给出了预期的答案,但是Javascript版本却有很多.

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.

我在翻译中犯了一个错误,还是Javascript的浮点数学运算不那么精确?

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]

因此评分计算并不差,准确度为99.6%,但差异只有2/3!

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

人们指出,Pyglicko版本中RD的默认值为200.这是原始实现者留下测试代码的情况,我相信,因为该测试用例是在RD为200,但显然默认值应为350.但是,我确实在Java的测试用例中指定了200,所以这里不是问题.

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.

更改了使用映射/归约的算法.评级不那么准确,方差更准确,这两者都没有明显的原因.围墙开始.

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

推荐答案

通常,当您减去两个相似的数字时,会出现类似的错误-值之间通常不显着的差异会被放大.例如,如果您有两个值,在python中分别为1.2345和1.2346,而在javascript中为1.2344和1.2347,则两者的区别分别为1e-4和3 e-4(即,一个是另一个的3倍).

typically you get errors like this where you are subtracting two similar numbers - then the normally insignificant differences between values become amplified. for example, if you have two values that are 1.2345 and 1.2346 in python, but 1.2344 and 1.2347 in javascript, then the differences are 1e-4 and 3 e-4 respectively (ie one is 3x the other).

所以我看一下您的代码中有减法的地方,并检查这些值.您可能会发现,您可以(1)重写数学运算以避免减法(通常事实证明,您可以找到以其他方式计算差值的表达式),或者(2)着眼于为什么在该特定点处的值两种语言之间的差异(也许在pi上的差异是,以这种方式放大了识别出的其他答案).

so i would look at where you have subtractions in your code and check those values. you may find that you can either (1) rewrite the maths to avoid the subtraction (often it turns out that you can find an expression that calculates the difference in some other way) or (2) focus on why the values at that particular point differ between the two languages (perhaps the difference in pi that the other answer identified is being amplified in this way).

虽然在这里不太可能,但也有可能有所不同,因为某些东西在python中被视为整数,而在javascript中被视为浮点数.在python中,整数和浮点数之间有区别,如果您不小心,可以执行以下操作:将两个整数相除以获得另一个整数(例如python中的3/2 = 1).而在javascript中,所有数字都是真正"的浮点数,因此不会发生.

it's also possible, although less likely here, that you have a difference because something is treated as an integer in python, but as a float in javascript. in python there is a difference between integers and floats, and if you are not careful you can do things like divide two integers to get another integer (eg 3/2 = 1 in python). while in javascript, all numbers are "really" floats, so this does not occur.

最后,计算方式可能会有细微的差异.但是这些都是正常的"-要获得如此巨大的差异,您还需要像我上面所述的东西.

finally, it's possible there are small differences in how the calculations are performed. but these are "normal" - to get such drastic differences you need something like what i described above to occur as well.

PS:还请注意Daniel Baulig在上面的评论中对参数rd的初始值所说的话.

PS: also note what Daniel Baulig said about the initial value of the parameter rd in the comments above.

这篇关于Python与Java浮点算术给出了截然不同的答案.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆