不同语言的浮点精度 [英] Floating point accuracy with different languages
问题描述
我目前正在计算坐标之间的距离,并且根据所使用的语言获得的结果略有不同.
I'm currently doing distance calculations between coordinates and have been getting slightly different results depending on the language used.
部分计算工作是计算给定 radian
的 cosine
.我得到以下结果
Part of the calculation is taking calculating the cosine
of a given radian
. I get the following results
// cos(0.8941658257446736)
// 0.6261694290123146 node
// 0.6261694290123146 rust
// 0.6261694290123148 go
// 0.6261694290123148 python
// 0.6261694290123148 swift
// 0.6261694290123146 c++
// 0.6261694290123146 java
// 0.6261694290123147 c
我想尝试并理解原因.如果您过去的 16dp
c
是唯一的正确"字样,四舍五入的答案.令我惊讶的是 python
具有不同的结果.
I would like to try and understand why. If you look past 16dp
c
is the only "correct" answer in terms of rounding. What I am surprised as is python
having a different result.
这个小差异目前正在放大,并且在000多个位置上增加了不小的距离.
This small difference is being amplified currently and over 000's of positions it is adding a not insignificant distance.
不太确定这是如何重复的.另外,我要求的是整体答案,而不是特定于语言的答案.我没有计算机科学学位.
Not really sure how this is a duplicate. Also I am more asking for a holistic answer rather than language specific. I don't have a compute science degree.
更新
我接受这可能是一个太宽泛的问题,我想我为什么对我的背景不是CS感到好奇.我感谢评论中发布的博客链接.
I accept that maybe this is too broad a question I suppose I was curious as to why as my background isn't CS. I appreciate the links to the blogs that were posted in the comments.
更新2
这个问题源于将服务从 nodejs
移植到 go
. Go
甚至更奇怪,因为距离的总和随多个值而变化,所以我现在无法运行测试.
This question arose from porting a service from nodejs
to go
. Go
is even weirder as I am now unable to run tests as the summation of distances varies with multiple values.
给出一个坐标列表并计算距离并将它们加在一起,我得到不同的结果.我不是在问一个问题,而是为 go
会产生不同的结果而感到疯狂.
Given a list of coordinates and calculating the distance and adding them together I get different results. I'm not asking a question but seems crazy that go
will produce different results.
9605.795975874069
9605.795975874067
9605.79597587407
为完整起见,这是我正在使用的距离计算:
For completeness here is the Distance calculation I am using:
func Distance(pointA Coordinate, pointB Coordinate) float64 {
const R = 6371000 // Earth radius meters
phi1 := pointA.Lat * math.Pi / 180
phi2 := pointB.Lat * math.Pi / 180
lambda1 := pointA.Lon * math.Pi / 180
lambda2 := pointB.Lon * math.Pi / 180
deltaPhi := phi2 - phi1
deltaLambda := lambda2 - lambda1
a := math.Sin(deltaPhi/2)*math.Sin(deltaPhi/2) + math.Cos(phi1)*math.Cos(phi2)*math.Sin(deltaLambda/2)*math.Sin(deltaLambda/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
d := R * c
return d
}
推荐答案
通常,浮点数的表示由标准 IEEE 754 ,我的假设是该标准由所有(主要)编程语言实现.
Generally, the representation of floating point numbers is defined by the standard IEEE 754 and my assumption is that this standard is implemented by all (major) programming languages.
Precision and rounding are known issues and may sometimes lead to unexpected results.
根据编程语言或使用的数学库,可能会对计算结果产生影响的方面:
Aspects that may have an influence on the result of a calculation depending on the programming language or used math library:
- different calculation methods (in your case: the cosine function might be implemented by numerical approximation with different approaches)
- different rounding strategies during calculation or for the final output
这篇关于不同语言的浮点精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!