From 2f731fa1ae184ce45e97ebd463d5109b41d80136 Mon Sep 17 00:00:00 2001 From: simdimdim Date: Thu, 1 Jun 2023 04:28:08 +0300 Subject: [PATCH] Adding more math constants (and a small correction to the description of an existing one) (#9181) Adding more float constants for when https://github.com/rust-lang/rust/issues/103883 is accepted and merged. And fixing a small conflation in the description of the Euler number. Please take a look and let me know if I've missed or screwed up anything. --- crates/nu-command/src/math/egamma.rs | 64 ++++++++++++++++++++++++++++ crates/nu-command/src/math/euler.rs | 2 +- crates/nu-command/src/math/mod.rs | 4 ++ crates/nu-command/src/math/phi.rs | 64 ++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 crates/nu-command/src/math/egamma.rs create mode 100644 crates/nu-command/src/math/phi.rs diff --git a/crates/nu-command/src/math/egamma.rs b/crates/nu-command/src/math/egamma.rs new file mode 100644 index 0000000000..f78ccccabd --- /dev/null +++ b/crates/nu-command/src/math/egamma.rs @@ -0,0 +1,64 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{ + Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value, +}; + +#[allow(clippy::excessive_precision)] +/// The Euler-Mascheroni constant (γ) +pub const EGAMMA: f64 = 0.577215664901532860606512090082402431_f64; + +#[derive(Clone)] +pub struct SubCommand; + +impl Command for SubCommand { + fn name(&self) -> &str { + "math egamma" + } + + fn signature(&self) -> Signature { + Signature::build("math egamma") + .input_output_types(vec![(Type::Any, Type::Float)]) + .category(Category::Math) + } + + fn usage(&self) -> &str { + "Returns the Euler–Mascheroni constant γ. ( 1 | math egamma)." + } + + fn search_terms(&self) -> Vec<&str> { + vec!["euler", "constant", "gamma"] + } + + #[allow(clippy::approx_constant)] + fn examples(&self) -> Vec { + vec![Example { + example: "math egamma | math round --precision 3", + description: "Get the first three decimal digits of γ", + result: Some(Value::test_float(0.577)), + }] + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + // TODO: replace with std::f64::consts::EGAMMA when available https://github.com/rust-lang/rust/issues/103883 + Ok(Value::float(EGAMMA, call.head).into_pipeline_data()) + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_examples() { + use crate::test_examples; + + test_examples(SubCommand {}) + } +} diff --git a/crates/nu-command/src/math/euler.rs b/crates/nu-command/src/math/euler.rs index fc01fb40f9..d56544e4c8 100644 --- a/crates/nu-command/src/math/euler.rs +++ b/crates/nu-command/src/math/euler.rs @@ -23,7 +23,7 @@ impl Command for SubCommand { } fn search_terms(&self) -> Vec<&str> { - vec!["euler", "constant"] + vec!["euler", "number", "constant"] } #[allow(clippy::approx_constant)] diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs index 87ab457040..b602daee99 100644 --- a/crates/nu-command/src/math/mod.rs +++ b/crates/nu-command/src/math/mod.rs @@ -9,6 +9,7 @@ mod avg; mod ceil; mod cos; mod cosh; +mod egamma; mod euler; mod exp; mod floor; @@ -19,6 +20,7 @@ mod max; mod median; mod min; mod mode; +mod phi; mod pi; mod product; mod reducers; @@ -64,7 +66,9 @@ pub use arcsinh::SubCommand as MathArcSinH; pub use arctan::SubCommand as MathArcTan; pub use arctanh::SubCommand as MathArcTanH; +pub use egamma::SubCommand as MathEulerGamma; pub use euler::SubCommand as MathEuler; +pub use phi::SubCommand as MathPhi; pub use pi::SubCommand as MathPi; pub use tau::SubCommand as MathTau; diff --git a/crates/nu-command/src/math/phi.rs b/crates/nu-command/src/math/phi.rs new file mode 100644 index 0000000000..56b0dee9b1 --- /dev/null +++ b/crates/nu-command/src/math/phi.rs @@ -0,0 +1,64 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{ + Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value, +}; + +#[allow(clippy::excessive_precision)] +/// The golden ratio (φ) +pub const PHI: f64 = 1.618033988749894848204586834365638118_f64; + +#[derive(Clone)] +pub struct SubCommand; + +impl Command for SubCommand { + fn name(&self) -> &str { + "math phi" + } + + fn signature(&self) -> Signature { + Signature::build("math phi") + .input_output_types(vec![(Type::Any, Type::Float)]) + .category(Category::Math) + } + + fn usage(&self) -> &str { + "Returns the golden ratio φ. ( (1 + sqrt(5) ) / 2 )" + } + + fn search_terms(&self) -> Vec<&str> { + vec!["golden", "ratio", "constant"] + } + + #[allow(clippy::approx_constant)] + fn examples(&self) -> Vec { + vec![Example { + example: "math phi | math round --precision 3", + description: "Get the first two decimal digits of φ", + result: Some(Value::test_float(1.618)), + }] + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + // TODO: replace with std::f64::consts::PHI when available https://github.com/rust-lang/rust/issues/103883 + Ok(Value::float(PHI, call.head).into_pipeline_data()) + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_examples() { + use crate::test_examples; + + test_examples(SubCommand {}) + } +}