Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

LatLng distanceTo returns null for very close points #14216

Closed
bsterry opened this issue Mar 23, 2019 · 3 comments · Fixed by #14220
Closed

LatLng distanceTo returns null for very close points #14216

bsterry opened this issue Mar 23, 2019 · 3 comments · Fixed by #14220
Assignees
Labels
Android Mapbox Maps SDK for Android bug

Comments

@bsterry
Copy link

bsterry commented Mar 23, 2019

LatLng.distanceTo() returns NaN for very close points on Nexus One API 22 emulator (does not repro in two other emulators I tried)

Sample code:
LatLng(40.00599, -105.29261).distanceTo(LatLng(40.005990000000025, -105.29260999999997)

Steps to reproduce

  1. Call LatLng.distanceTo for two very near points (see above) on a Nexus One API 22 emulator (Android Studio, OSX)

Expected behavior

LatLng.distanceTo() returns 0.0 for any very close points

Actual behavior

LatLng.distnaceTo() returns NaN for some specific very close points in specific environments

It would seem that a test for isNaN(tt) is a safer way to implement this.

Configuration

OSX Android Studio emulator

Android versions:
Android 22

Device models:
Nexus One (emulated)

Mapbox SDK versions:
7.2

@bsterry
Copy link
Author

bsterry commented Mar 23, 2019

Related to but not the same as issue 3556.

@bsterry
Copy link
Author

bsterry commented Mar 24, 2019

I created a test to find more repros and found loads of very near location pairs that return NaN on my local machine (Android Studio, OSX, unit test)

import com.mapbox.mapboxsdk.geometry.LatLng
import org.junit.Test

class LatLngTest {
    @Test
    fun test() {
        val startLat = 40.0059514
        val startLng = -105.2927542

        val startLatLng = LatLng(startLat, startLng)

        val deltaLat = 40.005990000000025 - 40.00599
        val deltaLng = 105.29261 - 105.29260999999997

        println("offsets $deltaLat, $deltaLng")

        val deltaLats = listOf<Double>(deltaLat, 2.0 * deltaLat, 10.0 * deltaLat, 0.5 * deltaLat, 0.1 * deltaLat, 0.0, -deltaLat, 2.0 * -deltaLat, 10.0 * -deltaLat, 0.5 * -deltaLat, 0.1 * -deltaLat)
        val deltaLngs = listOf<Double>(deltaLng, 2.0 * deltaLng, 10.0 * deltaLng, 0.5 * deltaLng, 0.1 * deltaLng, 0.0, -deltaLng, 2.0 * -deltaLng, 10.0 * -deltaLng, 0.5 * -deltaLng, 0.1 * -deltaLng)

        for (i in 0..1000000) {
            val lat1 = Math.random() * 180.0 - 90.0
            val lng1 = Math.random() * 360.0 - 180.0

            val ll = LatLng(lat1, lng1)
            for (latDelta in deltaLats) {
                for (lngDelta in deltaLngs) {
                    val ll2 = LatLng(lat1 + latDelta, lng1 + lngDelta)
                    if (ll.distanceTo(ll2).isNaN()) {
                        println("NaN: $ll, $ll2, $latDelta, $lngDelta")
                    }

                }
            }
            if (i % 10000 == 0) {
                println(i)
            }
        }
    }
}

@bsterry
Copy link
Author

bsterry commented Mar 24, 2019

Here is a snippet of output from the above test:

NaN: LatLng [latitude=-4.220846880803606, longitude=-68.67139730089878, altitude=0.0], LatLng [latitude=-4.220846880803322, longitude=-68.67139730089849, altitude=0.0], 2.8421709430404007E-13, 2.8421709430404007E-13
NaN: LatLng [latitude=-4.220846880803606, longitude=-68.67139730089878, altitude=0.0], LatLng [latitude=-4.220846880803322, longitude=-68.67139730089876, altitude=0.0], 2.8421709430404007E-13, 1.4210854715202004E-14
NaN: LatLng [latitude=-4.220846880803606, longitude=-68.67139730089878, altitude=0.0], LatLng [latitude=-4.220846880803322, longitude=-68.67139730089878, altitude=0.0], 2.8421709430404007E-13, 2.842170943040401E-15
NaN: LatLng [latitude=-4.220846880803606, longitude=-68.67139730089878, altitude=0.0], LatLng [latitude=-4.220846880803322, longitude=-68.67139730089878, altitude=0.0], 2.8421709430404007E-13, 0.0
NaN: LatLng [latitude=-4.220846880803606, longitude=-68.67139730089878, altitude=0.0], LatLng [latitude=-4.220846880803322, longitude=-68.6713973008988, altitude=0.0], 2.8421709430404007E-13, -2.8421709430404007E-14
NaN: LatLng [latitude=-4.220846880803606, longitude=-68.67139730089878, altitude=0.0], LatLng [latitude=-4.220846880803322, longitude=-68.67139730089883, altitude=0.0], 2.8421709430404007E-13, -5.6843418860808015E-14
NaN: LatLng [latitude=-4.220846880803606, longitude=-68.67139730089878, altitude=0.0], LatLng [latitude=-4.220846880803322, longitude=-68.67139730089906, altitude=0.0], 2.8421709430404007E-13, -2.8421709430404007E-13

@bsterry bsterry changed the title LatLng distanceTo returns null for very close points on Nexus one API 22 emulator LatLng distanceTo returns null for very close points Mar 24, 2019
@tobrun tobrun added the Android Mapbox Maps SDK for Android label Mar 25, 2019
@tobrun tobrun added this to the release-m milestone Mar 25, 2019
@tobrun tobrun self-assigned this Mar 25, 2019
@tobrun tobrun added the bug label Mar 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants