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
This commit is contained in:
rjboas 2020-10-22 10:16:51 +10:00 committed by GitHub
parent 3cf7652e86
commit cc06ea4d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -91,7 +91,9 @@ pub async fn eval(
}
pub fn parse<T: Into<Tag>>(math_expression: &str, tag: T) -> Result<Value, String> {
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()),

View File

@ -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"));
}