Add math pi and math e constants (#7258)

Currently implemented as commands

Work towards #7073

Add them to the example test harness together with `math round`
This commit is contained in:
sholderbach 2022-11-27 16:25:51 +01:00 committed by Stefan Holderbach
parent 5c1606ed82
commit 64f226f7da
5 changed files with 134 additions and 2 deletions

View File

@ -405,6 +405,8 @@ pub fn create_default_context() -> EngineState {
MathStddev, MathStddev,
MathSum, MathSum,
MathVariance, MathVariance,
MathPi,
MathEuler,
}; };
// Network // Network

View File

@ -9,8 +9,8 @@ pub fn test_examples(cmd: impl Command + 'static) {
#[cfg(test)] #[cfg(test)]
mod test_examples { mod test_examples {
use super::super::{ use super::super::{
Ansi, Date, Echo, From, If, Into, LetEnv, Math, Path, Random, Split, SplitColumn, SplitRow, Ansi, Date, Echo, From, If, Into, LetEnv, Math, MathEuler, MathPi, MathRound, Path, Random,
Str, StrJoin, StrLength, StrReplace, Url, Wrap, Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace, Url, Wrap,
}; };
use crate::{Break, Mut, To}; use crate::{Break, Mut, To};
use itertools::Itertools; use itertools::Itertools;
@ -82,6 +82,9 @@ mod test_examples {
working_set.add_decl(Box::new(Echo)); working_set.add_decl(Box::new(Echo));
working_set.add_decl(Box::new(Break)); working_set.add_decl(Box::new(Break));
working_set.add_decl(Box::new(Mut)); working_set.add_decl(Box::new(Mut));
working_set.add_decl(Box::new(MathEuler));
working_set.add_decl(Box::new(MathPi));
working_set.add_decl(Box::new(MathRound));
// Adding the command that is being tested to the working set // Adding the command that is being tested to the working set
working_set.add_decl(cmd); working_set.add_decl(cmd);

View File

@ -0,0 +1,61 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, IntoPipelineData, PipelineData, Signature, Type, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"math e"
}
fn signature(&self) -> Signature {
Signature::build("math e")
.input_output_types(vec![(Type::Any, Type::Float)])
.category(Category::Math)
}
fn usage(&self) -> &str {
"Returns the mathematical constant π."
}
fn search_terms(&self) -> Vec<&str> {
vec!["euler", "constant"]
}
#[allow(clippy::approx_constant)]
fn examples(&self) -> Vec<Example> {
vec![Example {
example: "math e | math round --precision 3",
description: "Get the first three decimal digits of e",
result: Some(Value::test_float(2.718)),
}]
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::E,
span: call.head,
}
.into_pipeline_data())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -1,6 +1,7 @@
mod abs; mod abs;
mod avg; mod avg;
mod ceil; mod ceil;
mod euler;
mod eval; mod eval;
mod floor; mod floor;
pub mod math_; pub mod math_;
@ -8,6 +9,7 @@ mod max;
mod median; mod median;
mod min; mod min;
mod mode; mod mode;
mod pi;
mod product; mod product;
mod reducers; mod reducers;
mod round; mod round;
@ -33,3 +35,6 @@ pub use sqrt::SubCommand as MathSqrt;
pub use stddev::SubCommand as MathStddev; pub use stddev::SubCommand as MathStddev;
pub use sum::SubCommand as MathSum; pub use sum::SubCommand as MathSum;
pub use variance::SubCommand as MathVariance; pub use variance::SubCommand as MathVariance;
pub use euler::SubCommand as MathEuler;
pub use pi::SubCommand as MathPi;

View File

@ -0,0 +1,61 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, IntoPipelineData, PipelineData, Signature, Type, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"math pi"
}
fn signature(&self) -> Signature {
Signature::build("math pi")
.input_output_types(vec![(Type::Any, Type::Float)])
.category(Category::Math)
}
fn usage(&self) -> &str {
"Returns the mathematical constant π."
}
fn search_terms(&self) -> Vec<&str> {
vec!["trigonometry", "constant"]
}
#[allow(clippy::approx_constant)]
fn examples(&self) -> Vec<Example> {
vec![Example {
example: "math pi | math round --precision 2",
description: "Get the first two decimal digits of π",
result: Some(Value::test_float(3.14)),
}]
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::PI,
span: call.head,
}
.into_pipeline_data())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}