nu_cmd_extra/extra/math/
ln.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct MathLn;
5
6impl Command for MathLn {
7    fn name(&self) -> &str {
8        "math ln"
9    }
10
11    fn signature(&self) -> Signature {
12        Signature::build("math ln")
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 natural logarithm. Base: (math e)."
26    }
27
28    fn search_terms(&self) -> Vec<&str> {
29        vec!["natural", "logarithm", "inverse", "euler"]
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        vec![Example {
49            description: "Get the natural logarithm of e",
50            example: "2.7182818 | math ln | math round --precision 4",
51            result: Some(Value::test_float(1.0f64)),
52        }]
53    }
54}
55
56fn operate(value: Value, head: Span) -> Value {
57    match value {
58        numeric @ (Value::Int { .. } | Value::Float { .. }) => {
59            let span = numeric.span();
60            let (val, span) = match numeric {
61                Value::Int { val, .. } => (val as f64, span),
62                Value::Float { val, .. } => (val, span),
63                _ => unreachable!(),
64            };
65
66            if val > 0.0 {
67                let val = val.ln();
68
69                Value::float(val, span)
70            } else {
71                Value::error(
72                    ShellError::UnsupportedInput {
73                        msg: "'ln' undefined for values outside the open interval (0, Inf).".into(),
74                        input: "value originates from here".into(),
75                        msg_span: head,
76                        input_span: span,
77                    },
78                    span,
79                )
80            }
81        }
82        Value::Error { .. } => value,
83        other => Value::error(
84            ShellError::OnlySupportsThisInputType {
85                exp_input_type: "numeric".into(),
86                wrong_type: other.get_type().to_string(),
87                dst_span: head,
88                src_span: other.span(),
89            },
90            head,
91        ),
92    }
93}
94
95#[cfg(test)]
96mod test {
97    use super::*;
98
99    #[test]
100    fn test_examples() {
101        use crate::test_examples;
102
103        test_examples(MathLn {})
104    }
105}