title calculate altitude using pressure description the below algorithm approximates the altitude using barometric formula this method calculates the altitude from pressure wrt to sea level pressure as reference pressure is in pascals https en wikipedia orgwikipressurealtitude https community boschsensortec comt5questionandanswershowtocalculatethealtitudefromthepressuresensordataqaqp5702 h 44330 1 pp015 255 where h altitude m p measured pressure p0 reference pressure at sea level 101325 pa examples getaltitudeatpressurepressure100000 105 47836610778828 getaltitudeatpressurepressure101325 0 0 getaltitudeatpressurepressure80000 1855 873388064995 getaltitudeatpressurepressure201325 traceback most recent call last valueerror value higher than pressure at sea level getaltitudeatpressurepressure80000 traceback most recent call last valueerror atmospheric pressure can not be negative this method calculates the altitude from pressure wrt to sea level pressure as reference pressure is in pascals https en wikipedia org wiki pressure_altitude https community bosch sensortec com t5 question and answers how to calculate the altitude from the pressure sensor data qaq p 5702 h 44330 1 p p0 1 5 255 where h altitude m p measured pressure p0 reference pressure at sea level 101325 pa examples get_altitude_at_pressure pressure 100_000 105 47836610778828 get_altitude_at_pressure pressure 101_325 0 0 get_altitude_at_pressure pressure 80_000 1855 873388064995 get_altitude_at_pressure pressure 201_325 traceback most recent call last valueerror value higher than pressure at sea level get_altitude_at_pressure pressure 80_000 traceback most recent call last valueerror atmospheric pressure can not be negative
def get_altitude_at_pressure(pressure: float) -> float:
    

    if pressure > 101325:
        raise ValueError("Value Higher than Pressure at Sea Level !")
    if pressure < 0:
        raise ValueError("Atmospheric Pressure can not be negative !")
    return 44_330 * (1 - (pressure / 101_325) ** (1 / 5.5255))


if __name__ == "__main__":
    import doctest

    doctest.testmod()