nu_cmd_extra/extra/math/
sinh.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct MathSinH;
5
6impl Command for MathSinH {
7    fn name(&self) -> &str {
8        "math sinh"
9    }
10
11    fn signature(&self) -> Signature {
12        Signature::build("math sinh")
13            .input_output_types(vec![
14                (Type::Number, Type::Float),
15                (
16                    Type::List(Box::new(Type::Number)),
17                    Type::List(Box::new(Type::Float)),
18                ),
19            ])
20            .allow_variants_without_examples(true)
21            .category(Category::Math)
22    }
23
24    fn description(&self) -> &str {
25        "Returns the hyperbolic sine of the number."
26    }
27
28    fn search_terms(&self) -> Vec<&str> {
29        vec!["trigonometry", "hyperbolic"]
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        // This doesn't match explicit nulls
41        if let PipelineData::Empty = input {
42            return Err(ShellError::PipelineEmpty { dst_span: head });
43        }
44        input.map(move |value| operate(value, head), engine_state.signals())
45    }
46
47    fn examples(&self) -> Vec<Example<'_>> {
48        let e = std::f64::consts::E;
49        vec![Example {
50            description: "Apply the hyperbolic sine to 1",
51            example: "1 | math sinh",
52            result: Some(Value::test_float((e * e - 1.0) / (2.0 * e))),
53        }]
54    }
55}
56
57fn operate(value: Value, head: Span) -> Value {
58    match value {
59        numeric @ (Value::Int { .. } | Value::Float { .. }) => {
60            let span = numeric.span();
61            let (val, span) = match numeric {
62                Value::Int { val, .. } => (val as f64, span),
63                Value::Float { val, .. } => (val, span),
64                _ => unreachable!(),
65            };
66
67            Value::float(val.sinh(), span)
68        }
69        Value::Error { .. } => value,
70        other => Value::error(
71            ShellError::OnlySupportsThisInputType {
72                exp_input_type: "numeric".into(),
73                wrong_type: other.get_type().to_string(),
74                dst_span: head,
75                src_span: other.span(),
76            },
77            head,
78        ),
79    }
80}
81
82#[cfg(test)]
83mod test {
84    use super::*;
85
86    #[test]
87    fn test_examples() {
88        use crate::test_examples;
89
90        test_examples(MathSinH {})
91    }
92}