Make the default int an i64 (#3428)

* Make the default int an i64

* fmt

* Fix random integer

* Treat pids as i64 for now
This commit is contained in:
JT
2021-05-14 20:35:09 +12:00
committed by GitHub
parent 440a589f9e
commit d79a3130b8
59 changed files with 693 additions and 284 deletions

View File

@ -20,8 +20,9 @@ impl WholeStreamCommand for SubCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let mapped = args.input.map(move |val| match val.value {
UntaggedValue::Primitive(Primitive::Int(val)) => {
UntaggedValue::int(val.magnitude().clone()).into()
UntaggedValue::Primitive(Primitive::Int(val)) => UntaggedValue::int(val.abs()).into(),
UntaggedValue::Primitive(Primitive::BigInt(val)) => {
UntaggedValue::big_int(val.magnitude().clone()).into()
}
UntaggedValue::Primitive(Primitive::Decimal(val)) => {
UntaggedValue::decimal(val.abs()).into()

View File

@ -5,10 +5,7 @@ use crate::commands::math::utils::run_with_function;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{
hir::{convert_number_to_u64, Number, Operator},
Primitive, Signature, UntaggedValue, Value,
};
use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value};
use bigdecimal::FromPrimitive;
@ -47,7 +44,7 @@ impl WholeStreamCommand for SubCommand {
fn to_byte(value: &Value) -> Option<Value> {
match &value.value {
UntaggedValue::Primitive(Primitive::Int(num)) => {
Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value())
Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value())
}
_ => None,
}
@ -79,7 +76,7 @@ pub fn average(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
Value {
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
..
} => UntaggedValue::int(num.clone()).into_untagged_value(),
} => UntaggedValue::int(*num as i64).into_untagged_value(),
other => other.clone(),
})
.collect::<Vec<_>>(),
@ -100,15 +97,18 @@ pub fn average(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
..
} => {
let left = UntaggedValue::from(Primitive::Int(num));
let left = UntaggedValue::from(Primitive::Int(num as i64));
let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows);
match result {
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => {
let number = Number::Decimal(result);
let number = convert_number_to_u64(&number);
Ok(UntaggedValue::filesize(number).into_value(name))
}
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => match result.to_u64() {
Some(number) => Ok(UntaggedValue::filesize(number).into_value(name)),
None => Err(ShellError::labeled_error(
"could not calculate average of non-integer or unrelated types",
"source",
name,
)),
},
Ok(_) => Err(ShellError::labeled_error(
"could not calculate average of non-integer or unrelated types",
"source",

View File

@ -23,7 +23,13 @@ impl WholeStreamCommand for SubCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input;
run_with_numerical_functions_on_stream(input, ceil_big_int, ceil_big_decimal, ceil_default)
run_with_numerical_functions_on_stream(
input,
ceil_int,
ceil_big_int,
ceil_big_decimal,
ceil_default,
)
}
fn examples(&self) -> Vec<Example> {
@ -39,17 +45,28 @@ impl WholeStreamCommand for SubCommand {
}
}
fn ceil_big_int(val: BigInt) -> Value {
fn ceil_int(val: i64) -> Value {
UntaggedValue::int(val).into()
}
fn ceil_big_int(val: BigInt) -> Value {
UntaggedValue::big_int(val).into()
}
fn ceil_big_decimal(val: BigDecimal) -> Value {
let mut maybe_ceiled = val.round(0);
if maybe_ceiled < val {
maybe_ceiled += BigDecimal::one();
}
let (ceiled, _) = maybe_ceiled.into_bigint_and_exponent();
UntaggedValue::int(ceiled).into()
let ceiling = maybe_ceiled.to_i64();
match ceiling {
Some(x) => UntaggedValue::int(x).into(),
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
"Value too big to ceiling to an 64-bit integer",
))
.into(),
}
}
fn ceil_default(_: UntaggedValue) -> Value {

View File

@ -25,6 +25,7 @@ impl WholeStreamCommand for SubCommand {
run_with_numerical_functions_on_stream(
input,
floor_int,
floor_big_int,
floor_big_decimal,
floor_default,
@ -36,25 +37,29 @@ impl WholeStreamCommand for SubCommand {
description: "Apply the floor function to a list of numbers",
example: "echo [1.5 2.3 -3.1] | math floor",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(-4).into(),
UntaggedValue::big_int(1).into(),
UntaggedValue::big_int(2).into(),
UntaggedValue::big_int(-4).into(),
]),
}]
}
}
fn floor_big_int(val: BigInt) -> Value {
fn floor_int(val: i64) -> Value {
UntaggedValue::int(val).into()
}
fn floor_big_int(val: BigInt) -> Value {
UntaggedValue::big_int(val).into()
}
fn floor_big_decimal(val: BigDecimal) -> Value {
let mut maybe_floored = val.round(0);
if maybe_floored > val {
maybe_floored -= BigDecimal::one();
}
let (floored, _) = maybe_floored.into_bigint_and_exponent();
UntaggedValue::int(floored).into()
UntaggedValue::big_int(floored).into()
}
fn floor_default(_: UntaggedValue) -> Value {

View File

@ -4,10 +4,7 @@ use crate::prelude::*;
use bigdecimal::FromPrimitive;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{
hir::{convert_number_to_u64, Number, Operator},
Primitive, Signature, UntaggedValue, Value,
};
use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value};
pub struct SubCommand;
@ -124,14 +121,21 @@ fn compute_average(values: &[Value], name: impl Into<Tag>) -> Result<Value, Shel
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
..
} => {
let left = UntaggedValue::from(Primitive::Int(num));
let left = UntaggedValue::from(Primitive::Int(num as i64));
let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows);
match result {
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => {
let number = Number::Decimal(result);
let number = convert_number_to_u64(&number);
Ok(UntaggedValue::filesize(number).into_value(name))
let (bi, _) = result.as_bigint_and_exponent();
let number = bi.to_u64();
match number {
Some(number) => Ok(UntaggedValue::filesize(number).into_value(name)),
None => Err(ShellError::labeled_error(
"Can't convert to filesize",
"can't convert to filesize",
name,
)),
}
}
Ok(_) => Err(ShellError::labeled_error(
"could not calculate median of non-numeric or unrelated types",

View File

@ -36,7 +36,7 @@ impl WholeStreamCommand for SubCommand {
fn to_byte(value: &Value) -> Option<Value> {
match &value.value {
UntaggedValue::Primitive(Primitive::Int(num)) => {
Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value())
Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value())
}
_ => None,
}
@ -59,7 +59,7 @@ pub fn product(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
Value {
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
..
} => UntaggedValue::int(num.clone()).into_untagged_value(),
} => UntaggedValue::int(*num as i64).into_untagged_value(),
other => other.clone(),
})
.collect::<Vec<_>>(),

View File

@ -62,7 +62,7 @@ fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
0
};
let mapped = input.map(move |val| match val.value {
UntaggedValue::Primitive(Primitive::Int(val)) => round_big_int(val),
UntaggedValue::Primitive(Primitive::BigInt(val)) => round_big_int(val),
UntaggedValue::Primitive(Primitive::Decimal(val)) => {
round_big_decimal(val, precision.into())
}
@ -72,18 +72,22 @@ fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
}
fn round_big_int(val: BigInt) -> Value {
UntaggedValue::int(val).into()
UntaggedValue::big_int(val).into()
}
fn round_big_decimal(val: BigDecimal, precision: i64) -> Value {
if precision > 0 {
UntaggedValue::decimal(val.with_scale(precision + 1).round(precision)).into()
} else {
let (rounded, _) = val
.with_scale(precision + 1)
.round(precision)
.as_bigint_and_exponent();
UntaggedValue::int(rounded).into()
let rounded = val.with_scale(precision + 1).round(precision).to_i64();
match rounded {
Some(x) => UntaggedValue::int(x).into(),
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
"Number too larger to round to 64-bit int",
))
.into(),
}
}
}

View File

@ -46,10 +46,18 @@ fn operate(args: CommandArgs) -> OutputStream {
fn sqrt_big_decimal(val: BigDecimal) -> Value {
let squared = val.sqrt();
match squared {
None => UntaggedValue::Error(ShellError::unexpected("Cant square root a negative number"))
.into(),
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
"Can't square root a negative number",
))
.into(),
Some(val) if !val.is_integer() => UntaggedValue::decimal(val.normalized()).into(),
Some(val) => UntaggedValue::int(val.with_scale(0).as_bigint_and_exponent().0).into(),
Some(val) => match val.to_i64() {
Some(x) => UntaggedValue::int(x).into(),
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
"Value too large to convert to 64-bit integer",
))
.into(),
},
}
}

View File

@ -44,7 +44,7 @@ impl WholeStreamCommand for SubCommand {
fn to_byte(value: &Value) -> Option<Value> {
match &value.value {
UntaggedValue::Primitive(Primitive::Int(num)) => {
Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value())
Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value())
}
_ => None,
}
@ -71,7 +71,7 @@ pub fn summation(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
Value {
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
..
} => UntaggedValue::int(num.clone()).into_untagged_value(),
} => UntaggedValue::int(*num as i64).into_untagged_value(),
other => other.clone(),
})
.collect::<Vec<_>>(),

View File

@ -34,7 +34,9 @@ pub fn run_with_function(
}
}
pub type IntFunction = fn(val: BigInt) -> Value;
pub type BigIntFunction = fn(val: BigInt) -> Value;
pub type IntFunction = fn(val: i64) -> Value;
pub type DecimalFunction = fn(val: BigDecimal) -> Value;
@ -43,11 +45,13 @@ pub type DefaultFunction = fn(val: UntaggedValue) -> Value;
pub fn run_with_numerical_functions_on_stream(
input: InputStream,
int_function: IntFunction,
big_int_function: BigIntFunction,
decimal_function: DecimalFunction,
default_function: DefaultFunction,
) -> Result<OutputStream, ShellError> {
let mapped = input.map(move |val| match val.value {
UntaggedValue::Primitive(Primitive::Int(val)) => int_function(val),
UntaggedValue::Primitive(Primitive::BigInt(val)) => big_int_function(val),
UntaggedValue::Primitive(Primitive::Decimal(val)) => decimal_function(val),
other => default_function(other),
});

View File

@ -120,7 +120,7 @@ fn sum_of_squares(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
..
} => {
UntaggedValue::from(Primitive::Int(num.clone()))
UntaggedValue::from(Primitive::Int(*num as i64))
},
Value {
value: UntaggedValue::Primitive(num),