mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:55:42 +02:00
added pow operator, and filesize math (#2976)
* added pow operator, and filesize math * removed + and - arms, removed some pow, pow higher precedence * Update value.rs Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
@ -8,7 +8,7 @@ use nu_protocol::ShellTypeName;
|
||||
use nu_protocol::{Primitive, Type, UntaggedValue};
|
||||
use nu_source::{DebugDocBuilder, PrettyDebug, Span, Tagged};
|
||||
use nu_table::TextStyle;
|
||||
use num_traits::Zero;
|
||||
use num_traits::{ToPrimitive, Zero};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Date;
|
||||
@ -119,6 +119,11 @@ pub fn compute_values(
|
||||
}?;
|
||||
Ok(UntaggedValue::Primitive(Primitive::Filesize(result)))
|
||||
}
|
||||
(Primitive::Filesize(x), Primitive::Int(y)) => match operator {
|
||||
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Int(x * y))),
|
||||
Operator::Divide => Ok(UntaggedValue::Primitive(Primitive::Int(x / y))),
|
||||
_ => Err((left.type_name(), right.type_name())),
|
||||
},
|
||||
(Primitive::Int(x), Primitive::Int(y)) => match operator {
|
||||
Operator::Plus => Ok(UntaggedValue::Primitive(Primitive::Int(x + y))),
|
||||
Operator::Minus => Ok(UntaggedValue::Primitive(Primitive::Int(x - y))),
|
||||
@ -142,6 +147,13 @@ pub fn compute_values(
|
||||
Ok(UntaggedValue::Primitive(Primitive::Int(x % y)))
|
||||
}
|
||||
}
|
||||
Operator::Pow => {
|
||||
let prim_u32 = ToPrimitive::to_u32(y);
|
||||
match prim_u32 {
|
||||
Some(num) => Ok(UntaggedValue::Primitive(Primitive::Int(x.pow(num)))),
|
||||
_ => Err((left.type_name(), right.type_name())),
|
||||
}
|
||||
}
|
||||
_ => Err((left.type_name(), right.type_name())),
|
||||
},
|
||||
(Primitive::Decimal(x), Primitive::Int(y)) => {
|
||||
@ -161,6 +173,16 @@ pub fn compute_values(
|
||||
}
|
||||
Ok(x % bigdecimal::BigDecimal::from(y.clone()))
|
||||
}
|
||||
// leaving this here for the hope that bigdecimal will one day support pow/powf/fpow
|
||||
// Operator::Pow => {
|
||||
// let xp = bigdecimal::ToPrimitive::to_f64(x).unwrap_or(0.0);
|
||||
// let yp = bigdecimal::ToPrimitive::to_f64(y).unwrap_or(0.0);
|
||||
// let pow = bigdecimal::FromPrimitive::from_f64(xp.powf(yp));
|
||||
// match pow {
|
||||
// Some(p) => Ok(p),
|
||||
// None => Err((left.type_name(), right.type_name())),
|
||||
// }
|
||||
// }
|
||||
_ => Err((left.type_name(), right.type_name())),
|
||||
}?;
|
||||
Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))
|
||||
@ -182,7 +204,11 @@ pub fn compute_values(
|
||||
}
|
||||
Ok(bigdecimal::BigDecimal::from(x.clone()) % y)
|
||||
}
|
||||
|
||||
// big decimal doesn't support pow yet
|
||||
// Operator::Pow => {
|
||||
// let yp = bigdecimal::ToPrimitive::to_u32(y).unwrap_or(0);
|
||||
// Ok(bigdecimal::BigDecimal::from(x.pow(yp)))
|
||||
// }
|
||||
_ => Err((left.type_name(), right.type_name())),
|
||||
}?;
|
||||
Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))
|
||||
@ -204,7 +230,16 @@ pub fn compute_values(
|
||||
}
|
||||
Ok(x % y)
|
||||
}
|
||||
|
||||
// big decimal doesn't support pow yet
|
||||
// Operator::Pow => {
|
||||
// let xp = bigdecimal::ToPrimitive::to_f64(x).unwrap_or(0.0);
|
||||
// let yp = bigdecimal::ToPrimitive::to_f64(y).unwrap_or(0.0);
|
||||
// let pow = bigdecimal::FromPrimitive::from_f64(xp.powf(yp));
|
||||
// match pow {
|
||||
// Some(p) => Ok(p),
|
||||
// None => Err((left.type_name(), right.type_name())),
|
||||
// }
|
||||
// }
|
||||
_ => Err((left.type_name(), right.type_name())),
|
||||
}?;
|
||||
Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))
|
||||
|
Reference in New Issue
Block a user