Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add math tau #7408

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/nu-command/src/default_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ pub fn create_default_context() -> EngineState {
MathArcCosH,
MathArcTanH,
MathPi,
MathTau,
MathEuler,
MathLn,
};
Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod stddev;
mod sum;
mod tan;
mod tanh;
mod tau;
mod utils;
mod variance;

Expand Down Expand Up @@ -65,5 +66,6 @@ pub use arctanh::SubCommand as MathArcTanH;

pub use euler::SubCommand as MathEuler;
pub use pi::SubCommand as MathPi;
pub use tau::SubCommand as MathTau;

pub use ln::SubCommand as MathLn;
61 changes: 61 additions & 0 deletions crates/nu-command/src/math/tau.rs
Original file line number Diff line number Diff line change
@@ -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 tau"
}

fn signature(&self) -> Signature {
Signature::build("math tau")
.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 tau) / 2",
description: "Compare π and τ",
result: Some(Value::test_float(std::f64::consts::PI)),
}]
}

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::TAU,
span: call.head,
}
.into_pipeline_data())
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_examples() {
use crate::test_examples;

test_examples(SubCommand {})
}
}