Haversine formula: Difference between revisions
→{{header|Rust}}: Improved Rust implementation which doesn't rely on mutable function arguments
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
RyanTenney (talk | contribs) (→{{header|Rust}}: Improved Rust implementation which doesn't rely on mutable function arguments) |
||
Line 3,001:
=={{header|Rust}}==
<syntaxhighlight lang="rust">
struct Point {
lat: f64,
Line 3,010 ⟶ 3,006:
}
fn haversine(
origin.lon = origin.lon.to_radians();▼
origin.lat = origin.lat.to_radians();▼
let dz: f64 = origin.lat.sin() - destination.lat.sin();▼
}▼
let
let d_lat = lat2
lon:-86.67▼
};▼
let a = (d_lat / 2.0).sin().powi(2) + (d_lon / 2.0).sin().powi(2) * lat1.cos() * lat2.cos();
let destination: Point = Point {▼
let c = 2.0
R *
};▼
println!("Distance: {} km ({} mi)", d, d / 1.609344);▼
}
#[cfg(test)]
mod test {
use super::{Point, haversine};
#[test]
fn test_haversine() {
let origin: Point = Point {
lat: 36.12,
▲ lon: -86.67
▲ };
▲ let destination: Point = Point {
lat: 33.94,
lon: -118.4
};
▲ println!("Distance: {} km ({} mi)", d, d / 1.609344);
assert_eq!(d, 2887.2599506071106);
▲}
</syntaxhighlight>Output <pre>Distance: 2887.2599506071106 km (1794.060157807846 mi)</pre>
|