From cc06ea4d873c0eef038ec3723c390e1e4fe4a84a Mon Sep 17 00:00:00 2001 From: rjboas <55905955+rjboas@users.noreply.github.com> Date: Thu, 22 Oct 2020 10:16:51 +1000 Subject: [PATCH] Add Tau constant (#2673) Adds Tau constant using meval::Context. Also adds a test to match pi's. Note: Tau ends up not being more precise than 2*pi. Resolves: #2258 --- crates/nu-cli/src/commands/math/eval.rs | 4 +++- crates/nu-cli/tests/commands/math/eval.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/src/commands/math/eval.rs b/crates/nu-cli/src/commands/math/eval.rs index 3b6876428a..fe57819c67 100644 --- a/crates/nu-cli/src/commands/math/eval.rs +++ b/crates/nu-cli/src/commands/math/eval.rs @@ -91,7 +91,9 @@ pub async fn eval( } pub fn parse>(math_expression: &str, tag: T) -> Result { - match meval::eval_str(math_expression) { + let mut ctx = meval::Context::new(); + ctx.var("tau", std::f64::consts::TAU); + match meval::eval_str_with_context(math_expression, &ctx) { Ok(num) if num.is_infinite() || num.is_nan() => Err("cannot represent result".to_string()), Ok(num) => Ok(UntaggedValue::from(Primitive::from(num)).into_value(tag)), Err(error) => Err(error.to_string().to_lowercase()), diff --git a/crates/nu-cli/tests/commands/math/eval.rs b/crates/nu-cli/tests/commands/math/eval.rs index 084dc7b433..736d1f6419 100644 --- a/crates/nu-cli/tests/commands/math/eval.rs +++ b/crates/nu-cli/tests/commands/math/eval.rs @@ -71,3 +71,15 @@ fn evaluates_pi() { assert!(actual.out.contains("3.14")); } + +#[test] +fn evaluates_tau() { + let actual = nu!( + cwd: ".", pipeline( + r#" + math eval tau + "# + )); + + assert!(actual.out.contains("6.28")); +}