diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-03-13 21:32:05 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-03-13 21:32:05 -0230 |
| commit | 8756ace462ab94e540d53c633b7e76c21b7a6bcd (patch) | |
| tree | 4ae8e0025aa8c25af8f15164ba06dd98f9a6324b /src/function.rs | |
| parent | c83c2315dd45bb0bc590995ea0899a0ebd1b59dc (diff) | |
| download | pfc-8756ace462ab94e540d53c633b7e76c21b7a6bcd.zip | |
inverse trig functions
Diffstat (limited to 'src/function.rs')
| -rw-r--r-- | src/function.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/function.rs b/src/function.rs new file mode 100644 index 0000000..bb6462d --- /dev/null +++ b/src/function.rs @@ -0,0 +1,61 @@ +use crate::AngleMode; + +pub enum Function { + Sin, // Sine + Cos, // Cosine + Tan, // Tangent + Asin, // Inverse sine + Acos, // Inverse cosine + Atan, // Inverse tangent + Deg, // Convert from radians to degrees + Rad, // Convert from degrees to radians +} + +impl Function { + pub fn parse(s: &str) -> Result<Self, ParseFunctionError> { + match s { + "sin" => Ok(Self::Sin), + "cos" => Ok(Self::Cos), + "tan" => Ok(Self::Tan), + "asin" => Ok(Self::Asin), + "acos" => Ok(Self::Acos), + "atan" => Ok(Self::Atan), + "deg" => Ok(Self::Deg), + "rad" => Ok(Self::Rad), + _ => Err(ParseFunctionError(s.to_string())), + } + } + + pub fn call(&self, val: f64, angle_mode: AngleMode) -> f64 { + match self { + Self::Sin => match angle_mode { + AngleMode::Degrees => val.to_radians().sin(), + AngleMode::Radians => val.sin(), + }, + Self::Cos => match angle_mode { + AngleMode::Degrees => val.to_radians().cos(), + AngleMode::Radians => val.cos(), + }, + Self::Tan => match angle_mode { + AngleMode::Degrees => val.to_radians().tan(), + AngleMode::Radians => val.tan(), + }, + Self::Asin => match angle_mode { + AngleMode::Degrees => val.asin().to_degrees(), + AngleMode::Radians => val.asin(), + }, + Self::Acos => match angle_mode { + AngleMode::Degrees => val.acos().to_degrees(), + AngleMode::Radians => val.acos(), + }, + Self::Atan => match angle_mode { + AngleMode::Degrees => val.atan().to_degrees(), + AngleMode::Radians => val.atan(), + }, + Self::Deg => val.to_degrees(), + Self::Rad => val.to_radians(), + } + } +} + +pub struct ParseFunctionError(String); |