rpn(1): makes rounding more efficient

This commit is contained in:
Emma Tebibyte 2024-09-04 22:12:44 -06:00
parent 731e62ee7e
commit 62ce288524
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -129,10 +129,6 @@ impl StrError for EvaluationError {
} }
} }
/* Im 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 { fn err<T: StrError>(argv0: &String, e: &T, code: Option<u8>) -> ExitCode {
eprintln!("{}: {}", argv0, e.strerror()); eprintln!("{}: {}", argv0, e.strerror());
ExitCode::from(code.unwrap_or(1 /* unknown error */)) ExitCode::from(code.unwrap_or(1 /* unknown error */))
@ -211,9 +207,9 @@ fn eval(
fn round_precise(value: &f64) -> f64 { fn round_precise(value: &f64) -> f64 {
/* 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 i32; let precision = (-f64::EPSILON.log10()).floor() as i32;
let multiplier = 10_f64.powi(precision);
let multiplier = 10_f64.powi(precision as i32);
(value * multiplier).round() / multiplier (value * multiplier).round() / multiplier
} }