nushell/crates/nu-cmd-extra/src/extra/math/egamma.rs
Michael Angerman 942c66a9f3
cratification: part II of the math commands to nu-cmd-extra (#9657)
The following math commands are being moved to nu-cmd-extra

* cos
* cosh
* egamma
* phi
* pi
* sin
* sinh
* tan
* tanh
* tau

For now I think we have most of the obvious commands moved over based on

@sholderbach this should cover moving the "high school" commands..

>>Yeah I think this rough separation into "high school" math in extra
and "middle school"/"programmer" math in the core makes a ton of sense.

And to reference the @fdncred list from
https://github.com/nushell/nushell/pull/9647#issuecomment-1629498812
2023-07-11 11:23:39 -07:00

65 lines
1.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 EulerMascheroni 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 {})
}
}