rpn(1): makes rounding more efficient
This commit is contained in:
		
							parent
							
								
									731e62ee7e
								
							
						
					
					
						commit
						62ce288524
					
				
							
								
								
									
										10
									
								
								src/rpn.rs
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/rpn.rs
									
									
									
									
									
								
							| @ -129,10 +129,6 @@ impl StrError for EvaluationError { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| /* 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; | ||||
| 
 | ||||
| fn err<T: StrError>(argv0: &String, e: &T, code: Option<u8>) -> ExitCode { | ||||
| 	eprintln!("{}: {}", argv0, e.strerror()); | ||||
| 	ExitCode::from(code.unwrap_or(1 /* unknown error */)) | ||||
| @ -211,9 +207,9 @@ fn eval( | ||||
| fn round_precise(value: &f64) -> f64 { | ||||
| 	/* Set floating-point precision for correcting rounding errors based on
 | ||||
| 	 * machine epsilon */ | ||||
| 	let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as i32; | ||||
| 	
 | ||||
| 	let multiplier = 10_f64.powi(precision as i32); | ||||
| 	let precision = (-f64::EPSILON.log10()).floor() as i32; | ||||
| 	let multiplier = 10_f64.powi(precision); | ||||
| 
 | ||||
| 	(value * multiplier).round() / multiplier | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user