mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
move math constants to standard library (#9678)
# Description we talked about this before in some meetings so i thought, why not? the hope is that these constants do not require Rust code to be implemented and that this move will make the Rust source base a bit smaller 🤞 # User-Facing Changes mathematical constants (e, pi, tau, phi and gamma) are now in `std math` rather than `math` ## what can be done ```nushell > use std; $std.math > use std math; $math > use std *; $math ``` will all give ``` ╭───────┬────────────────────╮ │ GAMMA │ 0.5772156649015329 │ │ E │ 2.718281828459045 │ │ PI │ 3.141592653589793 │ │ TAU │ 6.283185307179586 │ │ PHI │ 1.618033988749895 │ ╰───────┴────────────────────╯ ``` and the following will work too ```nushell > use std math E; $E 2.718281828459045 ``` ```nushell > use std math *; $GAMMA 0.5772156649015329 ``` ## what can NOT be done looks like every export works fine now 😌 # Tests + Formatting # After Submitting
This commit is contained in:
parent
1ee3bf784c
commit
456e2a8ee3
15 changed files with 11 additions and 333 deletions
|
@ -15,8 +15,6 @@ mod test_examples {
|
||||||
check_example_input_and_output_types_match_command_signature,
|
check_example_input_and_output_types_match_command_signature,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::MathEuler;
|
|
||||||
use crate::MathPi;
|
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{Command, EngineState, StateWorkingSet},
|
engine::{Command, EngineState, StateWorkingSet},
|
||||||
Type,
|
Type,
|
||||||
|
@ -64,9 +62,6 @@ mod test_examples {
|
||||||
|
|
||||||
working_set.add_decl(Box::new(nu_command::Enumerate));
|
working_set.add_decl(Box::new(nu_command::Enumerate));
|
||||||
working_set.add_decl(Box::new(nu_cmd_lang::If));
|
working_set.add_decl(Box::new(nu_cmd_lang::If));
|
||||||
// math commands
|
|
||||||
working_set.add_decl(Box::new(MathEuler));
|
|
||||||
working_set.add_decl(Box::new(MathPi));
|
|
||||||
working_set.add_decl(Box::new(nu_command::MathRound));
|
working_set.add_decl(Box::new(nu_command::MathRound));
|
||||||
|
|
||||||
// Adding the command that is being tested to the working set
|
// Adding the command that is being tested to the working set
|
||||||
|
|
|
@ -54,7 +54,7 @@ impl Command for SubCommand {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Apply the cosine to π",
|
description: "Apply the cosine to π",
|
||||||
example: "math pi | math cos",
|
example: "3.141592 | math cos | math round --precision 4",
|
||||||
result: Some(Value::test_float(-1f64)),
|
result: Some(Value::test_float(-1f64)),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
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<Example> {
|
|
||||||
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<PipelineData, ShellError> {
|
|
||||||
// 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 {})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,59 +0,0 @@
|
||||||
use nu_protocol::ast::Call;
|
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
||||||
use nu_protocol::{
|
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, 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 e (exp(1)/'1 | math exp')."
|
|
||||||
}
|
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
|
||||||
vec!["euler", "number", "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<PipelineData, ShellError> {
|
|
||||||
Ok(Value::float(std::f64::consts::E, call.head).into_pipeline_data())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_examples() {
|
|
||||||
use crate::test_examples;
|
|
||||||
|
|
||||||
test_examples(SubCommand {})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -52,7 +52,7 @@ impl Command for SubCommand {
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Get the natural logarithm of e",
|
description: "Get the natural logarithm of e",
|
||||||
example: "math e | math ln",
|
example: "2.7182818 | math ln | math round --precision 4",
|
||||||
result: Some(Value::test_float(1.0f64)),
|
result: Some(Value::test_float(1.0f64)),
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,8 @@ mod sinh;
|
||||||
mod tan;
|
mod tan;
|
||||||
mod tanh;
|
mod tanh;
|
||||||
|
|
||||||
mod egamma;
|
|
||||||
mod euler;
|
|
||||||
mod exp;
|
mod exp;
|
||||||
mod ln;
|
mod ln;
|
||||||
mod phi;
|
|
||||||
mod pi;
|
|
||||||
mod tau;
|
|
||||||
|
|
||||||
mod arccos;
|
mod arccos;
|
||||||
mod arccosh;
|
mod arccosh;
|
||||||
|
@ -27,13 +22,8 @@ pub use sinh::SubCommand as MathSinH;
|
||||||
pub use tan::SubCommand as MathTan;
|
pub use tan::SubCommand as MathTan;
|
||||||
pub use tanh::SubCommand as MathTanH;
|
pub use tanh::SubCommand as MathTanH;
|
||||||
|
|
||||||
pub use egamma::SubCommand as MathEulerGamma;
|
|
||||||
pub use euler::SubCommand as MathEuler;
|
|
||||||
pub use exp::SubCommand as MathExp;
|
pub use exp::SubCommand as MathExp;
|
||||||
pub use ln::SubCommand as MathLn;
|
pub use ln::SubCommand as MathLn;
|
||||||
pub use phi::SubCommand as MathPhi;
|
|
||||||
pub use pi::SubCommand as MathPi;
|
|
||||||
pub use tau::SubCommand as MathTau;
|
|
||||||
|
|
||||||
pub use arccos::SubCommand as MathArcCos;
|
pub use arccos::SubCommand as MathArcCos;
|
||||||
pub use arccosh::SubCommand as MathArcCosH;
|
pub use arccosh::SubCommand as MathArcCosH;
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
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<Example> {
|
|
||||||
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<PipelineData, ShellError> {
|
|
||||||
// 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 {})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,59 +0,0 @@
|
||||||
use nu_protocol::ast::Call;
|
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
||||||
use nu_protocol::{
|
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, 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<PipelineData, ShellError> {
|
|
||||||
Ok(Value::float(std::f64::consts::PI, call.head).into_pipeline_data())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_examples() {
|
|
||||||
use crate::test_examples;
|
|
||||||
|
|
||||||
test_examples(SubCommand {})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -54,7 +54,7 @@ impl Command for SubCommand {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Apply the sine to π/2",
|
description: "Apply the sine to π/2",
|
||||||
example: "(math pi) / 2 | math sin",
|
example: "3.141592 / 2 | math sin | math round --precision 4",
|
||||||
result: Some(Value::test_float(1f64)),
|
result: Some(Value::test_float(1f64)),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
|
|
|
@ -54,7 +54,7 @@ impl Command for SubCommand {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Apply the tangent to π/4",
|
description: "Apply the tangent to π/4",
|
||||||
example: "(math pi) / 4 | math tan",
|
example: "3.141592 / 4 | math tan | math round --precision 4",
|
||||||
result: Some(Value::test_float(1f64)),
|
result: Some(Value::test_float(1f64)),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
|
|
|
@ -52,7 +52,7 @@ impl Command for SubCommand {
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Apply the hyperbolic tangent to 10*π",
|
description: "Apply the hyperbolic tangent to 10*π",
|
||||||
example: "(math pi) * 10 | math tanh",
|
example: "3.141592 * 10 | math tanh | math round --precision 4",
|
||||||
result: Some(Value::test_float(1f64)),
|
result: Some(Value::test_float(1f64)),
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
use nu_protocol::ast::Call;
|
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
||||||
use nu_protocol::{
|
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, 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 | math round --precision 2",
|
|
||||||
description: "Get the first two decimal digits of τ",
|
|
||||||
result: Some(Value::test_float(6.28)),
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(
|
|
||||||
&self,
|
|
||||||
_engine_state: &EngineState,
|
|
||||||
_stack: &mut Stack,
|
|
||||||
call: &Call,
|
|
||||||
_input: PipelineData,
|
|
||||||
) -> Result<PipelineData, ShellError> {
|
|
||||||
Ok(Value::float(std::f64::consts::TAU, call.head).into_pipeline_data())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_examples() {
|
|
||||||
use crate::test_examples;
|
|
||||||
|
|
||||||
test_examples(SubCommand {})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -24,13 +24,8 @@ pub use math::MathSinH;
|
||||||
pub use math::MathTan;
|
pub use math::MathTan;
|
||||||
pub use math::MathTanH;
|
pub use math::MathTanH;
|
||||||
|
|
||||||
pub use math::MathEuler;
|
|
||||||
pub use math::MathEulerGamma;
|
|
||||||
pub use math::MathExp;
|
pub use math::MathExp;
|
||||||
pub use math::MathLn;
|
pub use math::MathLn;
|
||||||
pub use math::MathPhi;
|
|
||||||
pub use math::MathPi;
|
|
||||||
pub use math::MathTau;
|
|
||||||
|
|
||||||
pub use math::MathArcCos;
|
pub use math::MathArcCos;
|
||||||
pub use math::MathArcCosH;
|
pub use math::MathArcCosH;
|
||||||
|
@ -111,9 +106,6 @@ pub fn add_extra_command_context(mut engine_state: EngineState) -> EngineState {
|
||||||
MathSinH,
|
MathSinH,
|
||||||
MathCosH,
|
MathCosH,
|
||||||
MathTanH,
|
MathTanH,
|
||||||
MathPi,
|
|
||||||
MathTau,
|
|
||||||
MathEuler,
|
|
||||||
MathExp,
|
MathExp,
|
||||||
MathLn
|
MathLn
|
||||||
};
|
};
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub fn load_standard_library(
|
||||||
("assert.nu", include_str!("../std/assert.nu")),
|
("assert.nu", include_str!("../std/assert.nu")),
|
||||||
("xml.nu", include_str!("../std/xml.nu")),
|
("xml.nu", include_str!("../std/xml.nu")),
|
||||||
("input.nu", include_str!("../std/input.nu")),
|
("input.nu", include_str!("../std/input.nu")),
|
||||||
|
("math.nu", include_str!("../std/math.nu")),
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
|
|
5
crates/nu-std/std/math.nu
Normal file
5
crates/nu-std/std/math.nu
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
export const GAMMA = 0.5772156649015329
|
||||||
|
export const E = 2.718281828459045
|
||||||
|
export const PI = 3.141592653589793
|
||||||
|
export const TAU = 6.283185307179586
|
||||||
|
export const PHI = 1.618033988749895
|
Loading…
Reference in a new issue