rpn(1): better comments

This commit is contained in:
Emma Tebibyte 2024-07-13 18:29:27 -06:00
parent a9b388fe4b
commit b0602388e7
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -57,7 +57,7 @@ extern crate sysexits;
use sysexits::EX_DATAERR; use sysexits::EX_DATAERR;
#[derive(Clone, PartialEq, PartialOrd, Debug)] #[derive(Clone, PartialEq, PartialOrd, Debug)]
// enum CalcType is a type containing operations used in the calculator /* enum CalcType is a type containing operations used in the calculator */
enum CalcType { enum CalcType {
Add, Add,
Subtract, Subtract,
@ -117,8 +117,8 @@ struct EvaluationError {
code: i32, code: i32,
} }
// Im no math nerd but I want the highest possible approximation of 0.9 /* Im no math nerd but I want the highest possible approximation of 0.9
// repeating and it seems this can give it to me * repeating and it seems this can give it to me */
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0; const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
fn eval( fn eval(
@ -133,7 +133,7 @@ fn eval(
return Ok((stack, oper)); return Ok((stack, oper));
} }
// Split the input into tokens. /* Split the input into tokens. */
let mut toks: VecDeque<CalcType> = input let mut toks: VecDeque<CalcType> = input
.split_whitespace() .split_whitespace()
.rev() .rev()
@ -183,7 +183,7 @@ fn eval(
Ok((stack, oper)) Ok((stack, oper))
} }
// Round a float to the given precision level /* Round a float to the given precision level */
fn round_precise(value: &f64, precision: usize) -> f64 { fn round_precise(value: &f64, precision: usize) -> f64 {
let multiplier = 10_f64.powi(precision as i32); let multiplier = 10_f64.powi(precision as i32);
(value * multiplier).round() / multiplier (value * multiplier).round() / multiplier
@ -193,11 +193,11 @@ fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
let mut stack = VecDeque::new(); let mut stack = VecDeque::new();
let mut buf = String::new(); let mut buf = String::new();
// Set floating-point precision for correcting rounding errors based on /* Set floating-point precision for correcting rounding errors based on
// machine epsilon * machine epsilon */
let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as usize; let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as usize;
if argv.get(1).is_none() { if argv.get(1).is_none() { /* read from stdin */
while let Ok(_) = stdin().read_line(&mut buf) { while let Ok(_) = stdin().read_line(&mut buf) {
match eval(&buf.trim(), stack) { match eval(&buf.trim(), stack) {
Ok(s) => { Ok(s) => {
@ -219,12 +219,13 @@ fn main() -> ExitCode {
}, },
}; };
} }
} else { } else { /* read from argv */
/* join argv into an owned String joined by spaces minus argv[0] */
let input = argv let input = argv
.iter() .iter()
.skip(1) .skip(1)
.map(|x| x.to_owned()) .map(|x| x.to_owned())
.collect::<Vec<String>>() .collect::<Vec<_>>()
.join(" "); .join(" ");
match eval(&input, stack) { match eval(&input, stack) {
@ -233,7 +234,7 @@ fn main() -> ExitCode {
let val = match stack.iter().last() { let val = match stack.iter().last() {
Some(v) => v, Some(v) => v,
None => return ExitCode::from(0), None => return ExitCode::SUCCESS,
}; };
println!("{}", round_precise(val, precision).to_string()) println!("{}", round_precise(val, precision).to_string())
@ -244,5 +245,5 @@ fn main() -> ExitCode {
}, },
}; };
} }
ExitCode::from(0) ExitCode::SUCCESS
} }