forked from bonsai/harakit
		
	rpn(1): better comments
This commit is contained in:
		
							parent
							
								
									a9b388fe4b
								
							
						
					
					
						commit
						b0602388e7
					
				
							
								
								
									
										25
									
								
								src/rpn.rs
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								src/rpn.rs
									
									
									
									
									
								
							@ -57,7 +57,7 @@ extern crate sysexits;
 | 
			
		||||
use sysexits::EX_DATAERR;
 | 
			
		||||
 | 
			
		||||
#[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 {
 | 
			
		||||
	Add,
 | 
			
		||||
	Subtract,
 | 
			
		||||
@ -117,8 +117,8 @@ struct EvaluationError {
 | 
			
		||||
	code: i32,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// I’m no math nerd but I want the highest possible approximation of 0.9
 | 
			
		||||
// repeating and it seems this can give it to me
 | 
			
		||||
/* I’m no math nerd but I want the highest possible approximation of 0.9
 | 
			
		||||
 * repeating and it seems this can give it to me */
 | 
			
		||||
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
 | 
			
		||||
 | 
			
		||||
fn eval(
 | 
			
		||||
@ -133,7 +133,7 @@ fn eval(
 | 
			
		||||
		return Ok((stack, oper));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Split the input into tokens.
 | 
			
		||||
	/* Split the input into tokens. */
 | 
			
		||||
	let mut toks: VecDeque<CalcType> = input
 | 
			
		||||
		.split_whitespace()
 | 
			
		||||
		.rev()
 | 
			
		||||
@ -183,7 +183,7 @@ fn eval(
 | 
			
		||||
	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 {
 | 
			
		||||
	let multiplier = 10_f64.powi(precision as i32);
 | 
			
		||||
	(value * multiplier).round() / multiplier
 | 
			
		||||
@ -193,11 +193,11 @@ fn main() -> ExitCode {
 | 
			
		||||
	let argv = args().collect::<Vec<String>>();
 | 
			
		||||
	let mut stack = VecDeque::new();
 | 
			
		||||
	let mut buf = String::new();
 | 
			
		||||
	// Set floating-point precision for correcting rounding errors based on
 | 
			
		||||
	// machine epsilon
 | 
			
		||||
	/* Set floating-point precision for correcting rounding errors based on
 | 
			
		||||
	 * machine epsilon */
 | 
			
		||||
	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) {
 | 
			
		||||
			match eval(&buf.trim(), stack) {
 | 
			
		||||
				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
 | 
			
		||||
			.iter()
 | 
			
		||||
			.skip(1)
 | 
			
		||||
			.map(|x| x.to_owned())
 | 
			
		||||
			.collect::<Vec<String>>()
 | 
			
		||||
			.collect::<Vec<_>>()
 | 
			
		||||
			.join(" ");
 | 
			
		||||
 | 
			
		||||
		match eval(&input, stack) {
 | 
			
		||||
@ -233,7 +234,7 @@ fn main() -> ExitCode {
 | 
			
		||||
 | 
			
		||||
				let val = match stack.iter().last() {
 | 
			
		||||
					Some(v) => v,
 | 
			
		||||
					None => return ExitCode::from(0),
 | 
			
		||||
					None => return ExitCode::SUCCESS,
 | 
			
		||||
				};
 | 
			
		||||
 | 
			
		||||
				println!("{}", round_precise(val, precision).to_string())
 | 
			
		||||
@ -244,5 +245,5 @@ fn main() -> ExitCode {
 | 
			
		||||
			},
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
	ExitCode::from(0)
 | 
			
		||||
	ExitCode::SUCCESS
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user