diff --git a/src/main.rs b/src/main.rs index 247ba53..107dbf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,22 +12,8 @@ pub fn parse_and_calc(s: &str) -> Float { nodes::Nodes::from_str(s).unwrap().evaluate() } -pub fn get_significant_digits(n: &Float) -> usize { - let raw_string = format!("{n}"); - - let mut significant_digits = raw_string.len() - 1; - for (i, _) in raw_string.char_indices() { - if !raw_string[i..raw_string.len()].contains(|c| matches!(c, '1'..'9')) { - significant_digits = i; - break; - } - } - - significant_digits -} - fn main() { - let input = match env::args().skip(1).reduce(|x, a| a + &x) { + let input = match env::args().nth(1) { Some(s) => s, None => { let mut buf = String::new(); @@ -36,7 +22,5 @@ fn main() { } }; - let result = parse_and_calc(&input); - - println!("{result:.0$}", get_significant_digits(&result).clamp(0, 6)); + println!("{:.3}", parse_and_calc(&input)); } diff --git a/src/nodes/evaluate.rs b/src/nodes/evaluate.rs index c9984bd..eae3d8e 100644 --- a/src/nodes/evaluate.rs +++ b/src/nodes/evaluate.rs @@ -40,8 +40,6 @@ fn collapse_toplevel(nodes: &mut Vec, mut end: usize) { { match &mut nodes[start..end][index + 1] { Node::Number(n) => match variant { - Function::Sqrt => n.sqrt_mut(), - Function::Sine => n.sin_mut(), Function::ArcSine => n.asin_mut(), Function::Cosine => n.cos_mut(), diff --git a/src/nodes/from_str.rs b/src/nodes/from_str.rs index a28b15a..4f0f20d 100644 --- a/src/nodes/from_str.rs +++ b/src/nodes/from_str.rs @@ -1,4 +1,3 @@ -use core::f64; use std::str::FromStr; use rug::{Float, float::ParseFloatError, ops::CompleteRound}; @@ -10,7 +9,6 @@ impl Node { Ok(match s { "c" => Self::Number(Float::with_val(PREC, 299_792_458)), "A" => Self::Number(Float::with_val(PREC, 6.02214076e23)), - "pi" => Self::Number(Float::with_val(PREC, f64::consts::PI)), _ => Self::Function(s.parse()?), }) } @@ -21,8 +19,8 @@ impl TryFrom for Operator { match value { '+' => Ok(Self::Plus), '-' => Ok(Self::Minus), - '*' | '×' => Ok(Self::Mult), - '/' | '÷' => Ok(Self::Div), + '*' => Ok(Self::Mult), + '/' => Ok(Self::Div), '^' => Ok(Self::Exp), _ => Err(value), } @@ -32,8 +30,6 @@ impl FromStr for Function { type Err = String; fn from_str(s: &str) -> Result { match s { - "sqrt" => Ok(Self::Sqrt), - "sin" => Ok(Self::Sine), "asin" => Ok(Self::ArcSine), "cos" => Ok(Self::Cosine), diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index f46ad3b..a7326bf 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -40,8 +40,6 @@ enum Operator { #[derive(Debug, Copy, Clone, PartialEq)] enum Function { - Sqrt, - Sine, ArcSine, Cosine, diff --git a/src/nodes/tests.rs b/src/nodes/tests.rs index ae50576..4c3caa6 100644 --- a/src/nodes/tests.rs +++ b/src/nodes/tests.rs @@ -1,18 +1,9 @@ -use core::f64; use std::str::FromStr; use rug::Float; use crate::nodes::{Function, Node, Nodes, Operator, PREC}; -#[test] -fn implicit_mult() { - assert_eq!( - Nodes::from_str("10 sin(10)").unwrap().evaluate().to_f64(), - -5.440211108893698 - ); -} - #[test] fn constants() { assert_eq!( @@ -23,10 +14,6 @@ fn constants() { Nodes::from_str("A").unwrap().evaluate().to_f64(), 6.02214076e23 ); - assert_eq!( - Nodes::from_str("pi").unwrap().evaluate().to_f64(), - f64::consts::PI - ); } #[test]