From 8756ace462ab94e540d53c633b7e76c21b7a6bcd Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Mon, 13 Mar 2023 21:32:05 -0230 Subject: inverse trig functions --- src/lib.rs | 59 ++++------------------------------------------------------- 1 file changed, 4 insertions(+), 55 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 039258c..db316f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,12 @@ use std::{ fmt::{self, Display, Formatter}, }; +mod function; mod input; pub mod ui; +pub use function::Function; + #[derive(Default)] pub struct Calculator { stack: Vec, @@ -35,41 +38,10 @@ impl Calculator { Operator::Exp => lhs.powf(rhs), }); } - - fn call_function(&mut self, func: Function) { - let mut val = match self.stack.pop() { - Some(v) => v, - None => { - return; - } - }; - self.stack.push(match func { - Function::Sin => { - if self.angle_mode == AngleMode::Degrees { - val = val.to_radians(); - } - val.sin() - } - Function::Cos => { - if self.angle_mode == AngleMode::Degrees { - val = val.to_radians(); - } - val.cos() - } - Function::Tan => { - if self.angle_mode == AngleMode::Degrees { - val = val.to_radians(); - } - val.tan() - } - Function::Deg => val.to_degrees(), - Function::Rad => val.to_radians(), - }); - } } #[derive(Default, Copy, Clone, PartialEq)] -enum AngleMode { +pub enum AngleMode { #[default] Degrees, Radians, @@ -120,29 +92,6 @@ impl Operator { struct ParseOperatorError(char); -enum Function { - Sin, // Sine - Cos, // Cosine - Tan, // Tangent - Deg, // Convert from radians to degrees - Rad, // Convert from degrees to radians -} - -impl Function { - fn parse(s: &str) -> Result { - match s { - "sin" => Ok(Self::Sin), - "cos" => Ok(Self::Cos), - "tan" => Ok(Self::Tan), - "deg" => Ok(Self::Deg), - "rad" => Ok(Self::Rad), - _ => Err(ParseFunctionError(s.to_string())), - } - } -} - -struct ParseFunctionError(String); - enum Constant { Pi, // Archimedes’ constant (π) E, // Euler's number (e) -- cgit v1.2.3