nu_cmd_extra/extra/math/
tan.rs

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