Skip to content

Commit

Permalink
move math constants to standard library (nushell#9678)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
amtoine authored Sep 5, 2023
1 parent 1ee3bf7 commit 456e2a8
Show file tree
Hide file tree
Showing 15 changed files with 11 additions and 333 deletions.
5 changes: 0 additions & 5 deletions crates/nu-cmd-extra/src/example_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ mod test_examples {
check_example_input_and_output_types_match_command_signature,
};

use crate::MathEuler;
use crate::MathPi;
use nu_protocol::{
engine::{Command, EngineState, StateWorkingSet},
Type,
Expand Down Expand Up @@ -64,9 +62,6 @@ mod test_examples {

working_set.add_decl(Box::new(nu_command::Enumerate));
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));

// Adding the command that is being tested to the working set
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-extra/src/extra/math/cos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Command for SubCommand {
vec![
Example {
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)),
},
Example {
Expand Down
64 changes: 0 additions & 64 deletions crates/nu-cmd-extra/src/extra/math/egamma.rs

This file was deleted.

59 changes: 0 additions & 59 deletions crates/nu-cmd-extra/src/extra/math/euler.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/nu-cmd-extra/src/extra/math/ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Command for SubCommand {
fn examples(&self) -> Vec<Example> {
vec![Example {
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)),
}]
}
Expand Down
10 changes: 0 additions & 10 deletions crates/nu-cmd-extra/src/extra/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ mod sinh;
mod tan;
mod tanh;

mod egamma;
mod euler;
mod exp;
mod ln;
mod phi;
mod pi;
mod tau;

mod arccos;
mod arccosh;
Expand All @@ -27,13 +22,8 @@ pub use sinh::SubCommand as MathSinH;
pub use tan::SubCommand as MathTan;
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 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 arccosh::SubCommand as MathArcCosH;
Expand Down
64 changes: 0 additions & 64 deletions crates/nu-cmd-extra/src/extra/math/phi.rs

This file was deleted.

59 changes: 0 additions & 59 deletions crates/nu-cmd-extra/src/extra/math/pi.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/nu-cmd-extra/src/extra/math/sin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Command for SubCommand {
vec![
Example {
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)),
},
Example {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-extra/src/extra/math/tan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Command for SubCommand {
vec![
Example {
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)),
},
Example {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-extra/src/extra/math/tanh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Command for SubCommand {
fn examples(&self) -> Vec<Example> {
vec![Example {
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)),
}]
}
Expand Down
Loading

0 comments on commit 456e2a8

Please sign in to comment.