Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
geo / geo-vincenty-inverseOpenRuns on Liberas

Vincenty distance (WGS84)

Geodesic distance between a and b on the WGS84 ellipsoid (a = 6,378,137 m, f = 1 ÷ 298.257223563, b = a × (1 − f)) by Vincenty's inverse formula (1975), in millimetres. L = rad(b.lon) − rad(a.lon); U1 = atan((1 − f) × tan(rad(a.lat))), U2 the same for b.lat; λ = L. Repeat: t1 = cosU2 × sinλ; t2 = cosU1 × sinU2 − sinU1 × cosU2 × cosλ; sinσ = √(t1 × t1 + t2 × t2), and if sinσ = 0 the result is 0; cosσ = sinU1 × sinU2 + cosU1 × cosU2 × cosλ; σ = atan2(sinσ, cosσ); sinα = cosU1 × cosU2 × sinλ ÷ sinσ; c2 = 1 − sinα × sinα; m = cosσ − 2 × sinU1 × sinU2 ÷ c2, or 0 when c2 = 0; C = f ÷ 16 × c2 × (4 + f × (4 − 3 × c2)); λ′ = λ; λ = L + (1 − C) × f × sinα × (σ + C × sinσ × (m + C × cosσ × (−1 + 2 × m × m))); stop when |λ − λ′| ≤ 1e-12. After 200 rounds without stopping (nearly antipodal points) the result is an error. Then u = c2 × (a × a − b × b) ÷ (b × b); A = 1 + u ÷ 16384 × (4096 + u × (−768 + u × (320 − 175 × u))); B = u ÷ 1024 × (256 + u × (−128 + u × (74 − 47 × u))); Δσ = B × sinσ × (m + B ÷ 4 × (cosσ × (−1 + 2 × m × m) − B ÷ 6 × m × (−3 + 4 × sinσ × sinσ) × (−3 + 4 × m × m))); s = b × A × (σ − Δσ); the result is round(s × 1000). A point is {lat, lon} in degrees; it is an error unless both are finite with −90 ≤ lat ≤ 90 and −180 ≤ lon ≤ 180 (NaN and ±Infinity are errors). rad(v) = v × π ÷ 180 and deg(v) = v × 180 ÷ π, left to right, with π the binary64 nearest π. Arithmetic is IEEE 754 binary64, each operation rounded to nearest-even and evaluated in exactly the order written (left to right; no fused multiply-add). sin, cos, tan, asin, atan, atan2, exp and ln are the platform's own: the result is an integer, round half away from zero, so any libm accurate to a few ulps gives the same answer except within a hair of a rounding boundary.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init geo-vincenty-inverse --lang rust
cd geo-vincenty-inverse && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)21 lines›
geo-vincenty-inverse.wit
package root:component;

world root {
  import liberas:geo/types@0.1.0;

  export liberas:geo/distance@0.1.0;
}
package liberas:geo@0.1.0 {
  interface types {
    record point {
      lat: f64,
      lon: f64,
    }
  }
  interface distance {
    use types.{point};

    distance: func(a: point, b: point) -> result<u64, string>;
  }
}
Test vectors20›
geo-vincenty-inverse.cases.json
[
  {
    "args": {
      "a": {
        "lat": 51.5074,
        "lon": -0.1278
      },
      "b": {
        "lat": 48.8566,
        "lon": 2.3522
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 40.7128,
        "lon": -74.006
      },
      "b": {
        "lat": 37.5665,
        "lon": 126.978
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 37.5665,
        "lon": 126.978
      },
      "b": {
        "lat": 35.1796,
        "lon": 129.0756
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": 0
      },
      "b": {
        "lat": 0,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": -33.8688,
        "lon": 151.2093
      },
      "b": {
        "lat": -33.8688,
        "lon": 151.2093
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 90,
        "lon": 0
      },
      "b": {
        "lat": -90,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 90,
        "lon": 0
      },
      "b": {
        "lat": 90,
        "lon": 123
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": 179.9
      },
      "b": {
        "lat": 0,
        "lon": -179.9
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": 0
      },
      "b": {
        "lat": 0,
        "lon": 90
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 1,
        "lon": 1
      },
      "b": {
        "lat": 1.000001,
        "lon": 1.000001
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": -37.95103341666667,
        "lon": 144.42486788888888
      },
      "b": {
        "lat": -37.65282113888889,
        "lon": 143.92649552777777
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": "NaN",
        "lon": 0
      },
      "b": {
        "lat": 0,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": "Infinity"
      },
      "b": {
        "lat": 0,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 90.0000001,
        "lon": 0
      },
      "b": {
        "lat": 0,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": -91,
        "lon": 0
      },
      "b": {
        "lat": 0,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": 180.5
      },
      "b": {
        "lat": 0,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": "-Infinity"
      },
      "b": {
        "lat": 0,
        "lon": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": 0
      },
      "b": {
        "lat": 0,
        "lon": 180
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 0,
        "lon": 0
      },
      "b": {
        "lat": 0.5,
        "lon": 179.7
      }
    }
  },
  {
    "args": {
      "a": {
        "lat": 10,
        "lon": 20
      },
      "b": {
        "lat": -10,
        "lon": -160
      }
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 20 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 1,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.