From c9f7422af1e0367aeaae0d3f34fd7f742daf4640 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 8 Apr 2024 03:15:54 +0200 Subject: [PATCH] fix unit conversion with negative distances --- src/nature.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/nature.rs b/src/nature.rs index 6d16b69..a873c25 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -84,19 +84,20 @@ pub fn ring_density(radius: f32) -> f32 { } pub fn readable_distance(distance: f64) -> String { - if distance > LIGHTYEAR2METER * 0.001 { + let abs_distance = distance.abs(); + if abs_distance > LIGHTYEAR2METER * 0.001 { let lightyears = distance / LIGHTYEAR2METER; return format!("{lightyears:.3}ly"); } - if distance >= 1.0e10 { + if abs_distance >= 1.0e10 { let gigameters = distance * 1.0e-9; return format!("{gigameters:.1}Gm"); } - if distance >= 1.0e7 { + if abs_distance >= 1.0e7 { let megameters = distance * 1.0e-6; return format!("{megameters:.1}Mm"); } - if distance >= 1.0e4 { + if abs_distance >= 1.0e4 { let kilometers = distance * 1.0e-3; return format!("{kilometers:.1}km"); }