geosphere icon indicating copy to clipboard operation
geosphere copied to clipboard

gcIntersectBearing(): Warning in acos((sin(lat2) - sin(lat1) * cos(dist12))/(sin(dist12) * cos(lat1))) : NaNs produced

Open casa-henrym opened this issue 5 years ago • 0 comments

The same issue as raised previously - an attempt to calculate acos() of a number (slightly) greater than 1 and (slightly) less than -1:

  • dput((sin(lat2) - sin(lat1) * cos(dist12))/(sin(dist12) * cos(lat1)))
    • 1.00000000000008
  • dput((sin(lat1) - sin(lat2) * cos(dist12))/(sin(dist12) * cos(lat2)))
    • -1.00000000000004

My work-around:

...
i <- rep(TRUE, length(dist12))

i[dist12 == 0] <- FALSE

foo <- (sin(lat2) - sin(lat1) * cos(dist12))/(sin(dist12) * cos(lat1))
foo[foo > 1] <- 1
foo[foo < -1] <- -1

brngA <- acos(foo)

brngA[is.na(brngA)] <- 0

bar <- (sin(lat1) - sin(lat2) * cos(dist12))/(sin(dist12) * cos(lat2))
bar[bar > 1] <- 1
bar[bar < -1] <- -1

brngB <- acos(bar)

g <- (sin(lon2 - lon1) > 0)
...

casa-henrym avatar Nov 24 '20 01:11 casa-henrym