nu_cmd_extra/extra/math/
arccos.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct MathArcCos;
5
6impl Command for MathArcCos {
7    fn name(&self) -> &str {
8        "math arccos"
9    }
10
11    fn signature(&self) -> Signature {
12        Signature::build("math arccos")
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 arccosine 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        vec![
54            Example {
55                description: "Get the arccosine of 1",
56                example: "1 | math arccos",
57                result: Some(Value::test_float(0.0f64)),
58            },
59            Example {
60                description: "Get the arccosine of -1 in degrees",
61                example: "-1 | math arccos --degrees",
62                result: Some(Value::test_float(180.0)),
63            },
64        ]
65    }
66}
67
68fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
69    match value {
70        numeric @ (Value::Int { .. } | Value::Float { .. }) => {
71            let span = numeric.span();
72            let (val, span) = match numeric {
73                Value::Int { val, .. } => (val as f64, span),
74                Value::Float { val, .. } => (val, span),
75                _ => unreachable!(),
76            };
77
78            if (-1.0..=1.0).contains(&val) {
79                let val = val.acos();
80                let val = if use_degrees { val.to_degrees() } else { val };
81
82                Value::float(val, span)
83            } else {
84                Value::error(
85                    ShellError::UnsupportedInput {
86                        msg: "'arccos' undefined for values outside the closed interval [-1, 1]."
87                            .into(),
88                        input: "value originates from here".into(),
89                        msg_span: head,
90                        input_span: span,
91                    },
92                    span,
93                )
94            }
95        }
96        Value::Error { .. } => value,
97        other => Value::error(
98            ShellError::OnlySupportsThisInputType {
99                exp_input_type: "numeric".into(),
100                wrong_type: other.get_type().to_string(),
101                dst_span: head,
102                src_span: other.span(),
103            },
104            head,
105        ),
106    }
107}
108
109#[cfg(test)]
110mod test {
111    use super::*;
112
113    #[test]
114    fn test_examples() {
115        use crate::test_examples;
116
117        test_examples(MathArcCos {})
118    }
119}