nu_cmd_extra/extra/math/
arctan.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct MathArcTan;
5
6impl Command for MathArcTan {
7    fn name(&self) -> &str {
8        "math arctan"
9    }
10
11    fn signature(&self) -> Signature {
12        Signature::build("math arctan")
13            .switch("degrees", "Return degrees instead of radians", Some('d'))
14            .input_output_types(vec![
15                (Type::Number, Type::Float),
16                (
17                    Type::List(Box::new(Type::Number)),
18                    Type::List(Box::new(Type::Float)),
19                ),
20            ])
21            .allow_variants_without_examples(true)
22            .category(Category::Math)
23    }
24
25    fn description(&self) -> &str {
26        "Returns the arctangent of the number."
27    }
28
29    fn search_terms(&self) -> Vec<&str> {
30        vec!["trigonometry", "inverse"]
31    }
32
33    fn run(
34        &self,
35        engine_state: &EngineState,
36        stack: &mut Stack,
37        call: &Call,
38        input: PipelineData,
39    ) -> Result<PipelineData, ShellError> {
40        let head = call.head;
41        let use_degrees = call.has_flag(engine_state, stack, "degrees")?;
42        // This doesn't match explicit nulls
43        if let PipelineData::Empty = input {
44            return Err(ShellError::PipelineEmpty { dst_span: head });
45        }
46        input.map(
47            move |value| operate(value, head, use_degrees),
48            engine_state.signals(),
49        )
50    }
51
52    fn examples(&self) -> Vec<Example<'_>> {
53        let pi = std::f64::consts::PI;
54        vec![
55            Example {
56                description: "Get the arctangent of 1",
57                example: "1 | math arctan",
58                result: Some(Value::test_float(pi / 4.0f64)),
59            },
60            Example {
61                description: "Get the arctangent of -1 in degrees",
62                example: "-1 | math arctan --degrees",
63                result: Some(Value::test_float(-45.0)),
64            },
65        ]
66    }
67}
68
69fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
70    match value {
71        numeric @ (Value::Int { .. } | Value::Float { .. }) => {
72            let span = numeric.span();
73            let (val, span) = match numeric {
74                Value::Int { val, .. } => (val as f64, span),
75                Value::Float { val, .. } => (val, span),
76                _ => unreachable!(),
77            };
78
79            let val = val.atan();
80            let val = if use_degrees { val.to_degrees() } else { val };
81
82            Value::float(val, span)
83        }
84        Value::Error { .. } => value,
85        other => Value::error(
86            ShellError::OnlySupportsThisInputType {
87                exp_input_type: "numeric".into(),
88                wrong_type: other.get_type().to_string(),
89                dst_span: head,
90                src_span: other.span(),
91            },
92            head,
93        ),
94    }
95}
96
97#[cfg(test)]
98mod test {
99    use super::*;
100
101    #[test]
102    fn test_examples() {
103        use crate::test_examples;
104
105        test_examples(MathArcTan {})
106    }
107}