hru(1): adds more descriptive comments

This commit is contained in:
Emma Tebibyte 2024-07-13 18:18:32 -06:00
parent e4e823a309
commit a9b388fe4b
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

View File

@ -29,40 +29,45 @@ extern crate sysexits;
use strerror::StrError; use strerror::StrError;
use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE }; use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE };
/* list of SI prefixes */
const LIST: [(u32, &str); 10] = [ const LIST: [(u32, &str); 10] = [
(3, "k"), (3, "k"), /* kilo */
(6, "M"), (6, "M"), /* mega */
(9, "G"), (9, "G"), /* giga */
(12, "T"), (12, "T"), /* tera */
(15, "P"), (15, "P"), /* peta */
(18, "E"), (18, "E"), /* exa */
(21, "Z"), (21, "Z"), /* zetta */
(24, "Y"), (24, "Y"), /* yotta */
(27, "R"), (27, "R"), /* ronna */
(30, "Q") (30, "Q"), /* quetta */
]; ];
fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> { fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> {
/* preserve decimal places in output by casting to a float */
let mut out = (input as f64, (0_u32, "")); let mut out = (input as f64, (0_u32, ""));
if input < 1000 { return Ok(out); }
if input < 1000 { return Ok(out); } /* too low to convert */
for (n, p) in LIST { for (n, p) in LIST {
let c = match 10_u128.checked_pow(n) { let c = match 10_u128.checked_pow(n) {
Some(c) => c, Some(c) => c,
None => { None => { /* too big for the laws of computing :( */
return Err(format!("10^{}: Integer overflow", n.to_string())); return Err(format!("10^{}: Integer overflow", n.to_string()));
}, },
}; };
match c.cmp(&input) { match c.cmp(&input) {
Ordering::Less => { Ordering::Less => { /* c < input */
/* the program will keep assigning out every loop until either
* the list runs out of higher prefix bases or the input is
* greater than the prefix base */
out = (input as f64 / c as f64, (n, p)); out = (input as f64 / c as f64, (n, p));
}, },
Ordering::Equal => { Ordering::Equal => { /* c == input */
return Ok((input as f64 / c as f64, (n, p))); return Ok((input as f64 / c as f64, (n, p)));
}, },
Ordering::Greater => {}, Ordering::Greater => {}, /* c > input */
}; };
} }