From 62ce28852429a5a0ef74c4cf270f2da0738cfd84 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 4 Sep 2024 22:12:44 -0600 Subject: [PATCH] rpn(1): makes rounding more efficient --- src/rpn.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) 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 }