nu_cmd_extra/extra/math/
arcsin.rs1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct MathArcSin;
5
6impl Command for MathArcSin {
7 fn name(&self) -> &str {
8 "math arcsin"
9 }
10
11 fn signature(&self) -> Signature {
12 Signature::build("math arcsin")
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 arcsine 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 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 arcsine of 1",
57 example: "1 | math arcsin",
58 result: Some(Value::test_float(pi / 2.0)),
59 },
60 Example {
61 description: "Get the arcsine of 1 in degrees",
62 example: "1 | math arcsin --degrees",
63 result: Some(Value::test_float(90.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 if (-1.0..=1.0).contains(&val) {
80 let val = val.asin();
81 let val = if use_degrees { val.to_degrees() } else { val };
82
83 Value::float(val, span)
84 } else {
85 Value::error(
86 ShellError::UnsupportedInput {
87 msg: "'arcsin' undefined for values outside the closed interval [-1, 1]."
88 .into(),
89 input: "value originates from here".into(),
90 msg_span: head,
91 input_span: span,
92 },
93 span,
94 )
95 }
96 }
97 Value::Error { .. } => value,
98 other => Value::error(
99 ShellError::OnlySupportsThisInputType {
100 exp_input_type: "numeric".into(),
101 wrong_type: other.get_type().to_string(),
102 dst_span: head,
103 src_span: other.span(),
104 },
105 head,
106 ),
107 }
108}
109
110#[cfg(test)]
111mod test {
112 use super::*;
113
114 #[test]
115 fn test_examples() {
116 use crate::test_examples;
117
118 test_examples(MathArcSin {})
119 }
120}