Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
geo / geo-haversineOpenRuns on Liberas

Haversine distance

Great-circle distance between a and b on a sphere of radius R = 6,371,008.8 m (IUGG mean Earth radius) by the haversine formula, in millimetres. With φ1 = rad(a.lat), φ2 = rad(b.lat): s1 = sin((φ2 − φ1) ÷ 2), s2 = sin((rad(b.lon) − rad(a.lon)) ÷ 2), h = s1 × s1 + cos φ1 × cos φ2 × s2 × s2, d = 2 × R × asin(√min(h, 1)); the result is round(d × 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-haversine --lang rust
cd geo-haversine && 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-haversine.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 vectors18›
geo-haversine.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": 0,
        "lon": 0
      },
      "b": {
        "lat": 0.5,
        "lon": 179.5
      }
    }
  },
  {
    "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
      }
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 18 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%.