{"record":{"id":"6443f7d6ad3433f2","repo":"TheAlgorithms/Python","slug":"latitude-must-be-between-90-and-90-degrees","errorCode":null,"errorMessage":"Latitude must be between -90 and 90 degrees","messagePattern":"Latitude must be between -90 and 90 degrees","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"geodesy/lamberts_ellipsoidal_distance.py","lineNumber":71,"sourceCode":"    ValueError: Longitude must be between -180 and 180 degrees\n\n    >>> from collections import namedtuple\n    >>> point_2d = namedtuple(\"point_2d\", \"lat lon\")\n    >>> 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","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/geodesy/lamberts_ellipsoidal_distance.py#L53-L89","documentation":"Raised by lamberts_ellipsoidal_distance() in geodesy/lamberts_ellipsoidal_distance.py when either latitude argument falls outside [-90, 90]. Lambert's formula converts latitudes via tan/radians for the parametric latitude; out-of-range values are geometrically invalid on the WGS84 ellipsoid and rejected before the trigonometry runs.","triggerScenarios":"Calling lamberts_ellipsoidal_distance(95.5, -122.4, 40.7, -74.0) — lat1 > 90. Note the check tests both lat1 and lat2 with one condition, so either bad coordinate raises the same error without saying which.","commonSituations":"Coordinate-order mixups (lat/lon swapped — longitudes like 40.7 pass as valid latitudes, so swaps often surface here or at the longitude check), missing minus signs on southern-hemisphere latitudes after arithmetic, or unnormalized data from projections emitting values beyond geodetic range.","solutions":["Ensure both latitudes are within [-90, 90].","If lat/lon were swapped at the call site, swap them back — check the longitude error that may follow.","Normalize coordinates upstream: clamp or recompute from the source CRS."],"exampleFix":"# before\nlamberts_ellipsoidal_distance(lon1, lat1, lon2, lat2)  # swapped order\n\n# after\nlamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)  # lat first","handlingStrategy":"validation","validationCode":"for name, lat in (('lat1', lat1), ('lat2', lat2)):\n    if not -90 <= lat <= 90:\n        raise ValueError(f'{name}={lat} outside [-90, 90]')\nlamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)","typeGuard":"def is_valid_lat(lat: float) -> bool:\n    return isinstance(lat, (int, float)) and -90.0 <= lat <= 90.0","tryCatchPattern":"try:\n    d = lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)\nexcept ValueError as exc:\n    if 'Latitude' in str(exc):\n        raise ValueError(f'bad input coords: {(lat1, lon1, lat2, lon2)}') from exc\n    raise","preventionTips":["Validate lat/lon order at ingestion — order swaps are the top cause.","Validate both latitude and longitude together; the function checks lat before lon."],"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"}