diff --git a/src/rpn.rs b/src/rpn.rs index 6907772..7383201 100644 --- a/src/rpn.rs +++ b/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(argv0: &String, e: &T, code: Option) -> 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 }