rpn(1): highest possible precision

This commit is contained in:
Emma Tebibyte 2024-02-01 01:18:13 -07:00
parent 942e284f93
commit 5debb9d954
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -78,6 +78,10 @@ impl fmt::Display 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 * 100.0;
// str_to_calc_type converts a string to an optional `CalcType`. // str_to_calc_type converts a string to an optional `CalcType`.
fn str_to_calc_type(string: &str) -> Option<CalcType> { fn str_to_calc_type(string: &str) -> Option<CalcType> {
let as_int = string.parse::<f64>(); let as_int = string.parse::<f64>();
@ -186,7 +190,7 @@ fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
let mut stack = VecDeque::new(); let mut stack = VecDeque::new();
let mut buf = String::new(); let mut buf = String::new();
let precision = (-f64::EPSILON.log10() * 0.999).ceil() as usize; let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as usize;
if argv.get(1).is_none() { if argv.get(1).is_none() {
while let Ok(_) = stdin().read_line(&mut buf) { while let Ok(_) = stdin().read_line(&mut buf) {