From 258f2f07a0671320009cc3ed21f7519ecab45fe5 Mon Sep 17 00:00:00 2001 From: coolsidd Date: Sun, 20 Oct 2024 20:51:16 +0530 Subject: [PATCH] Fixed flaky tests Fixed test shouldGenerateNearbyGPSCoordinateWithOriginInKilometers Also fixed shouldGenerateNearbyGPSCoordinateWithOriginInMiles Ran all tests locally 100 times. --- src/modules/location.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/modules/location.cpp b/src/modules/location.cpp index a94659553..a743f40af 100644 --- a/src/modules/location.cpp +++ b/src/modules/location.cpp @@ -232,11 +232,16 @@ std::tuple nearbyGPSCoordinate(Precision precision, co const auto radiusMetric = isMetric ? radius : radius * 1.60934; const auto distanceInKm = number::decimal(radiusMetric); - constexpr auto kmPerDegree = 40000 / 360; // The distance in km per degree for earth. - const auto distanceInDegree = distanceInKm / kmPerDegree; + constexpr auto kmPerDegreeLatitude = 110.574; // The distance in km per degree for earth's latitude. + const auto kmPerDegreeLongitude = + 111.320 * + std::cos(std::get<0>(origin) / 180 * M_PI); // The distance in km per degree for a specific longitude in earth. - auto coordinateLatitude = std::get<0>(origin) + distanceInDegree * std::sin(angleRadians); - auto coordinateLongitude = std::get<1>(origin) + distanceInDegree * std::cos(angleRadians); + const auto distanceInDegreeLatitude = distanceInKm / kmPerDegreeLatitude; + const auto distanceInDegreeLongitude = distanceInKm / kmPerDegreeLongitude; + + auto coordinateLatitude = std::get<0>(origin) + distanceInDegreeLatitude * std::sin(angleRadians); + auto coordinateLongitude = std::get<1>(origin) + distanceInDegreeLongitude * std::cos(angleRadians); // Box the latitude [-90, 90] coordinateLatitude = std::fmod(coordinateLatitude, 180.0);