acos(1)返回NaN的一些值,而不是其他值 [英] acos(1) returns NaN for some values, not others

查看:1229
本文介绍了acos(1)返回NaN的一些值,而不是其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经度和纬度值的列表,我试图找到他们之间的距离。使用一个标准的大圆方法,我需要找到:

$ $ $ $ $ $ $ $ c $(ac(sin(lat1)* sin(lat2)+ cos lat1)* cos(lat2)* cos(long2-long1))

地球,在我使用的单位。只要我们采取的值在[-1,1]的范围内,这是有效的。如果他们甚至略微超出这个范围,即使差异是由于四舍五入,也会返回 NaN



<我有的问题是,有时,当两个纬度/长度值是相同的,这给了我一个 NaN 错误。并不总是,即使是同一对数字,但总是在列表中相同的数字。例如,我有一个人停在沙漠的路上:

 时间| lat | long 
1: 00 PM|35.08646|-117.5023
1:01 PM|35.08646|-117.5023
1:02 PM|35.08646|-117.5023
1:03 PM|35.08646|-117.5023
1:04 PM| 35.08646 | -117.5023

当我计算连续点之间的距离时,第三个值,将始终是 NaN ,即使其他人不是。这似乎是R舍入一个奇怪的错误。

解决方案

如果没有看到您的数据, $ c> dput ),但这大多是 FAQ 7.31

>(x1 <-1)
## [1] 1
(x2 <-1 + 1e-16)
## [1] 1
(x3 < ; - 1 + 1e-8)
## [1] 1
acos(x1)
## [1] 0
acos(x2)
## [1] 0
acos(x3)
## [1] NaN

就是说,即使你的值是如此相似以至于它们的打印表示是相同的,它们可能仍然不同:一些将在 .Machine $ double.eps 中, '...

确保输入值受[-1,1]限制的一种方法是使用 pmax pmin acos(pmin(pmax(x,-1.0),1.0)) p>

I have a list of latitude and longitude values, and I'm trying to find the distance between them. Using a standard great circle method, I need to find:

acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(long2-long1))

And multiply this by the radius of earth, in the units I am using. This is valid as long as the values we take the acos of are in the range [-1,1]. If they are even slightly outside of this range, it will return NaN, even if the difference is due to rounding.

The issue I have is that sometimes, when two lat/long values are identical, this gives me an NaN error. Not always, even for the same pair of numbers, but always the same ones in a list. For instance, I have a person stopped on a road in the desert:

Time  |lat     |long
1:00PM|35.08646|-117.5023
1:01PM|35.08646|-117.5023
1:02PM|35.08646|-117.5023
1:03PM|35.08646|-117.5023
1:04PM|35.08646|-117.5023

When I calculate the distance between the consecutive points, the third value, for instance, will always be NaN, even though the others are not. This seems to be a weird bug with R rounding.

解决方案

Can't tell exactly without seeing your data (try dput), but this is mostly likely a consequence of FAQ 7.31.

(x1 <- 1)
## [1] 1
(x2 <- 1+1e-16)
## [1] 1
(x3 <- 1+1e-8)
## [1] 1
acos(x1)
## [1] 0
acos(x2)
## [1] 0
acos(x3)
## [1] NaN

That is, even if your values are so similar that their printed representations are the same, they may still differ: some will be within .Machine$double.eps and others won't ...

One way to make sure the input values are bounded by [-1,1] is to use pmax and pmin: acos(pmin(pmax(x,-1.0),1.0))

这篇关于acos(1)返回NaN的一些值,而不是其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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