{"record":{"id":"f032737166bb6e56","repo":"TheAlgorithms/Python","slug":"longitude-must-be-between-180-and-180-degrees","errorCode":null,"errorMessage":"Longitude must be between -180 and 180 degrees","messagePattern":"Longitude must be between -180 and 180 degrees","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"geodesy/lamberts_ellipsoidal_distance.py","lineNumber":75,"sourceCode":"    >>> SAN_FRANCISCO = point_2d(37.774856, -122.424227)\n    >>> YOSEMITE = point_2d(37.864742, -119.537521)\n    >>> NEW_YORK = point_2d(40.713019, -74.012647)\n    >>> VENICE = point_2d(45.443012, 12.313071)\n    >>> f\"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *YOSEMITE):0,.0f} meters\"\n    '254,351 meters'\n    >>> f\"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *NEW_YORK):0,.0f} meters\"\n    '4,138,992 meters'\n    >>> f\"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *VENICE):0,.0f} meters\"\n    '9,737,326 meters'\n    \"\"\"\n\n    # Validate latitude values\n    if not -90 <= lat1 <= 90 or not -90 <= lat2 <= 90:\n        raise ValueError(\"Latitude must be between -90 and 90 degrees\")\n\n    # Validate longitude values\n    if not -180 <= lon1 <= 180 or not -180 <= lon2 <= 180:\n        raise ValueError(\"Longitude must be between -180 and 180 degrees\")\n\n    # CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System\n    # Distance in metres(m)\n    # Equation Parameters\n    # https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines\n    flattening = (AXIS_A - AXIS_B) / AXIS_A\n    # Parametric latitudes\n    # https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude\n    b_lat1 = atan((1 - flattening) * tan(radians(lat1)))\n    b_lat2 = atan((1 - flattening) * tan(radians(lat2)))\n\n    # Compute central angle between two points\n    # using haversine theta. sigma =  haversine_distance / equatorial radius\n    sigma = haversine_distance(lat1, lon1, lat2, lon2) / EQUATORIAL_RADIUS\n\n    # Intermediate P and Q values\n    p_value = (b_lat1 + b_lat2) / 2\n    q_value = (b_lat2 - b_lat1) / 2","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/geodesy/lamberts_ellipsoidal_distance.py#L57-L93","documentation":"Raised by lamberts_ellipsoidal_distance() when either longitude falls outside [-180, 180]. The formula feeds longitudes into trigonometric central-angle computation; values outside the geodetic range produce wrong distances silently, so the function rejects them up front. Checked after latitude, so a bad latitude masks a bad longitude.","triggerScenarios":"Calling lamberts_ellipsoidal_distance(40.7, -190.0, 45.4, 12.3) — lon1 < -180. Values just above 180 (e.g. 190) typically come from 0-360-convention data.","commonSituations":"Longitudes from systems using [0, 360) instead of [-180, 180] (common in satellite/remote-sensing data — 190 vs -170), arithmetic that adds offsets past 180 without wrapping, or dateline-crossing computations that never normalize.","solutions":["Normalize longitudes to [-180, 180]: lon = ((lon + 180) % 360) - 180.","Fix the source convention: convert [0, 360) data before calling.","Double-check argument order — the function signature is (lat1, lon1, lat2, lon2)."],"exampleFix":"# before\nd = lamberts_ellipsoidal_distance(37.8, 190.0, 40.7, -74.0)  # 0..360 convention\n\n# after\nlon1 = ((190.0 + 180) % 360) - 180  # -170.0\nd = lamberts_ellipsoidal_distance(37.8, lon1, 40.7, -74.0)","handlingStrategy":"validation","validationCode":"def wrap_lon(lon: float) -> float:\n    return ((lon + 180.0) % 360.0) - 180.0\nlon1, lon2 = wrap_lon(lon1), wrap_lon(lon2)\nif not all(-180 <= l <= 180 for l in (lon1, lon2)):\n    raise ValueError('longitude out of range')\nlamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)","typeGuard":"def is_valid_lon(lon: float) -> bool:\n    return isinstance(lon, (int, float)) and -180.0 <= lon <= 180.0","tryCatchPattern":"try:\n    d = lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)\nexcept ValueError as exc:\n    if 'Longitude' in str(exc):\n        lon1, lon2 = wrap_lon(lon1), wrap_lon(lon2)\n        d = lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)\n    else:\n        raise","preventionTips":["Wrap longitudes after any offset arithmetic, especially near the antimeridian.","Note swapped args in mid-latitude ranges can pass both checks silently — validate order separately."],"tags":["geodesy","coordinates","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}