From bbf0b45c591cb3820f126a99828c051cdd76bd02 Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Wed, 13 Sep 2023 23:53:55 +0200 Subject: [PATCH] Update internal use of `decimal` to `float` (#10333) # Description We made the decision that our floating point type should be referred to as `float` over `decimal`. Commands were updated by #9979 and #10320 Now make the internal codebase consistent in referring to this data type as `float`. Work for #10332 # User-Facing Changes `decimal` has been removed as a type name/symbol. Instead of ```nushell def foo [bar: decimal] decimal -> decimal {} ``` use ```nushell def foo [bar: float] float -> float {} ``` Potential effect of `SyntaxShape`'s `Display` implementation now also referring to `float` instead of `decimal` # Details - Rename `SyntaxShape::Decimal` to `Float` - Update `Display for SyntaxShape` to `float` - Update error message + fn name in dataframe code - Fix docs in command examples - Rename tests that are float specific - Update doccomment on `SyntaxShape` - Update comment in script # Tests + Formatting Updates the names of some tests --- .../values/nu_dataframe/between_values.rs | 43 ++++++------------- .../nu-command/src/conversions/into/binary.rs | 2 +- .../nu-command/src/conversions/into/bool.rs | 4 +- .../src/conversions/into/filesize.rs | 2 +- .../nu-command/src/conversions/into/float.rs | 4 +- crates/nu-command/src/conversions/into/int.rs | 2 +- .../nu-command/src/conversions/into/string.rs | 10 ++--- .../tests/commands/into_filesize.rs | 2 +- crates/nu-command/tests/commands/math/mod.rs | 12 +++--- .../tests/commands/str_/into_string.rs | 8 ++-- crates/nu-command/tests/commands/str_/mod.rs | 2 +- crates/nu-parser/src/parser.rs | 4 +- crates/nu-protocol/src/syntax_shape.rs | 12 +++--- crates/nu-std/std/dt.nu | 2 +- tests/shell/pipeline/commands/internal.rs | 2 +- 15 files changed, 48 insertions(+), 63 deletions(-) diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs index 63fa850cc..9f7701c30 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs @@ -226,7 +226,7 @@ pub(super) fn compute_series_single_value( compute_series_i64(&lhs, *val, >::add, lhs_span) } Value::Float { val, .. } => { - compute_series_decimal(&lhs, *val, >::add, lhs_span) + compute_series_float(&lhs, *val, >::add, lhs_span) } Value::String { val, .. } => add_string_to_series(&lhs, val, lhs_span), _ => Err(ShellError::OperatorMismatch { @@ -242,7 +242,7 @@ pub(super) fn compute_series_single_value( compute_series_i64(&lhs, *val, >::sub, lhs_span) } Value::Float { val, .. } => { - compute_series_decimal(&lhs, *val, >::sub, lhs_span) + compute_series_float(&lhs, *val, >::sub, lhs_span) } _ => Err(ShellError::OperatorMismatch { op_span: operator.span, @@ -257,7 +257,7 @@ pub(super) fn compute_series_single_value( compute_series_i64(&lhs, *val, >::mul, lhs_span) } Value::Float { val, .. } => { - compute_series_decimal(&lhs, *val, >::mul, lhs_span) + compute_series_float(&lhs, *val, >::mul, lhs_span) } _ => Err(ShellError::OperatorMismatch { op_span: operator.span, @@ -281,12 +281,7 @@ pub(super) fn compute_series_single_value( if val.is_zero() { Err(ShellError::DivisionByZero { span }) } else { - compute_series_decimal( - &lhs, - *val, - >::div, - lhs_span, - ) + compute_series_float(&lhs, *val, >::div, lhs_span) } } _ => Err(ShellError::OperatorMismatch { @@ -301,7 +296,7 @@ pub(super) fn compute_series_single_value( Operator::Comparison(Comparison::Equal) => match &right { Value::Int { val, .. } => compare_series_i64(&lhs, *val, ChunkedArray::equal, lhs_span), Value::Float { val, .. } => { - compare_series_decimal(&lhs, *val, ChunkedArray::equal, lhs_span) + compare_series_float(&lhs, *val, ChunkedArray::equal, lhs_span) } Value::String { val, .. } => { let equal_pattern = format!("^{}$", fancy_regex::escape(val)); @@ -323,7 +318,7 @@ pub(super) fn compute_series_single_value( compare_series_i64(&lhs, *val, ChunkedArray::not_equal, lhs_span) } Value::Float { val, .. } => { - compare_series_decimal(&lhs, *val, ChunkedArray::not_equal, lhs_span) + compare_series_float(&lhs, *val, ChunkedArray::not_equal, lhs_span) } Value::Date { val, .. } => compare_series_i64( &lhs, @@ -342,7 +337,7 @@ pub(super) fn compute_series_single_value( Operator::Comparison(Comparison::LessThan) => match &right { Value::Int { val, .. } => compare_series_i64(&lhs, *val, ChunkedArray::lt, lhs_span), Value::Float { val, .. } => { - compare_series_decimal(&lhs, *val, ChunkedArray::lt, lhs_span) + compare_series_float(&lhs, *val, ChunkedArray::lt, lhs_span) } Value::Date { val, .. } => { compare_series_i64(&lhs, val.timestamp_millis(), ChunkedArray::lt, lhs_span) @@ -358,7 +353,7 @@ pub(super) fn compute_series_single_value( Operator::Comparison(Comparison::LessThanOrEqual) => match &right { Value::Int { val, .. } => compare_series_i64(&lhs, *val, ChunkedArray::lt_eq, lhs_span), Value::Float { val, .. } => { - compare_series_decimal(&lhs, *val, ChunkedArray::lt_eq, lhs_span) + compare_series_float(&lhs, *val, ChunkedArray::lt_eq, lhs_span) } Value::Date { val, .. } => { compare_series_i64(&lhs, val.timestamp_millis(), ChunkedArray::lt_eq, lhs_span) @@ -374,7 +369,7 @@ pub(super) fn compute_series_single_value( Operator::Comparison(Comparison::GreaterThan) => match &right { Value::Int { val, .. } => compare_series_i64(&lhs, *val, ChunkedArray::gt, lhs_span), Value::Float { val, .. } => { - compare_series_decimal(&lhs, *val, ChunkedArray::gt, lhs_span) + compare_series_float(&lhs, *val, ChunkedArray::gt, lhs_span) } Value::Date { val, .. } => { compare_series_i64(&lhs, val.timestamp_millis(), ChunkedArray::gt, lhs_span) @@ -390,7 +385,7 @@ pub(super) fn compute_series_single_value( Operator::Comparison(Comparison::GreaterThanOrEqual) => match &right { Value::Int { val, .. } => compare_series_i64(&lhs, *val, ChunkedArray::gt_eq, lhs_span), Value::Float { val, .. } => { - compare_series_decimal(&lhs, *val, ChunkedArray::gt_eq, lhs_span) + compare_series_float(&lhs, *val, ChunkedArray::gt_eq, lhs_span) } Value::Date { val, .. } => { compare_series_i64(&lhs, val.timestamp_millis(), ChunkedArray::gt_eq, lhs_span) @@ -514,12 +509,7 @@ where } } -fn compute_series_decimal( - series: &Series, - val: f64, - f: F, - span: Span, -) -> Result +fn compute_series_float(series: &Series, val: f64, f: F, span: Span) -> Result where F: Fn(ChunkedArray, f64) -> ChunkedArray, { @@ -548,7 +538,7 @@ where _ => Err(ShellError::GenericError( "Incorrect type".into(), format!( - "Series of type {} can not be used for operations with a decimal value", + "Series of type {} can not be used for operations with a float value", series.dtype() ), Some(span), @@ -668,12 +658,7 @@ where } } -fn compare_series_decimal( - series: &Series, - val: f64, - f: F, - span: Span, -) -> Result +fn compare_series_float(series: &Series, val: f64, f: F, span: Span) -> Result where F: Fn(&ChunkedArray, f64) -> ChunkedArray, { @@ -702,7 +687,7 @@ where _ => Err(ShellError::GenericError( "Incorrect type".into(), format!( - "Series of type {} can not be used for operations with a decimal value", + "Series of type {} can not be used for operations with a float value", series.dtype() ), Some(span), diff --git a/crates/nu-command/src/conversions/into/binary.rs b/crates/nu-command/src/conversions/into/binary.rs index e64905a39..f1197f5c5 100644 --- a/crates/nu-command/src/conversions/into/binary.rs +++ b/crates/nu-command/src/conversions/into/binary.rs @@ -107,7 +107,7 @@ impl Command for SubCommand { result: None, }, Example { - description: "convert a decimal to a nushell binary primitive", + description: "convert a float to a nushell binary primitive", example: "1.234 | into binary", result: Some(Value::binary( 1.234f64.to_ne_bytes().to_vec(), diff --git a/crates/nu-command/src/conversions/into/bool.rs b/crates/nu-command/src/conversions/into/bool.rs index fbbad6768..95ec00a87 100644 --- a/crates/nu-command/src/conversions/into/bool.rs +++ b/crates/nu-command/src/conversions/into/bool.rs @@ -95,12 +95,12 @@ impl Command for SubCommand { result: Some(Value::bool(true, span)), }, Example { - description: "convert decimal to boolean", + description: "convert float to boolean", example: "0.3 | into bool", result: Some(Value::bool(true, span)), }, Example { - description: "convert decimal string to boolean", + description: "convert float string to boolean", example: "'0.0' | into bool", result: Some(Value::bool(false, span)), }, diff --git a/crates/nu-command/src/conversions/into/filesize.rs b/crates/nu-command/src/conversions/into/filesize.rs index 3d8bc3510..283f202da 100644 --- a/crates/nu-command/src/conversions/into/filesize.rs +++ b/crates/nu-command/src/conversions/into/filesize.rs @@ -105,7 +105,7 @@ impl Command for SubCommand { result: Some(Value::filesize(2, Span::test_data())), }, Example { - description: "Convert decimal to filesize", + description: "Convert float to filesize", example: "8.3 | into filesize", result: Some(Value::filesize(8, Span::test_data())), }, diff --git a/crates/nu-command/src/conversions/into/float.rs b/crates/nu-command/src/conversions/into/float.rs index 7ccab4017..94eea9611 100644 --- a/crates/nu-command/src/conversions/into/float.rs +++ b/crates/nu-command/src/conversions/into/float.rs @@ -145,7 +145,7 @@ mod tests { #[test] #[allow(clippy::approx_constant)] - fn string_to_decimal() { + fn string_to_float() { let word = Value::test_string("3.1415"); let expected = Value::test_float(3.1415); @@ -154,7 +154,7 @@ mod tests { } #[test] - fn communicates_parsing_error_given_an_invalid_decimallike_string() { + fn communicates_parsing_error_given_an_invalid_floatlike_string() { let invalid_str = Value::test_string("11.6anra"); let actual = action( diff --git a/crates/nu-command/src/conversions/into/int.rs b/crates/nu-command/src/conversions/into/int.rs index 2bb3b5c7b..4962cea53 100644 --- a/crates/nu-command/src/conversions/into/int.rs +++ b/crates/nu-command/src/conversions/into/int.rs @@ -169,7 +169,7 @@ impl Command for SubCommand { result: Some(Value::test_int(2)), }, Example { - description: "Convert decimal to integer", + description: "Convert float to integer", example: "5.9 | into int", result: Some(Value::test_int(5)), }, diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index 780a0cf26..b3741515c 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -89,22 +89,22 @@ impl Command for SubCommand { result: Some(Value::test_string("5.000")), }, Example { - description: "convert decimal to string and round to nearest integer", + description: "convert float to string and round to nearest integer", example: "1.7 | into string -d 0", result: Some(Value::test_string("2")), }, Example { - description: "convert decimal to string", + description: "convert float to string", example: "1.7 | into string -d 1", result: Some(Value::test_string("1.7")), }, Example { - description: "convert decimal to string and limit to 2 decimals", + description: "convert float to string and limit to 2 decimals", example: "1.734 | into string -d 2", result: Some(Value::test_string("1.73")), }, Example { - description: "try to convert decimal to string and provide negative decimal points", + description: "try to convert float to string and provide negative decimal points", example: "1.734 | into string -d -2", result: None, // FIXME @@ -116,7 +116,7 @@ impl Command for SubCommand { // }), }, Example { - description: "convert decimal to string", + description: "convert float to string", example: "4.3 | into string", result: Some(Value::test_string("4.3")), }, diff --git a/crates/nu-command/tests/commands/into_filesize.rs b/crates/nu-command/tests/commands/into_filesize.rs index ad8a3892c..4821d7320 100644 --- a/crates/nu-command/tests/commands/into_filesize.rs +++ b/crates/nu-command/tests/commands/into_filesize.rs @@ -8,7 +8,7 @@ fn into_filesize_int() { } #[test] -fn into_filesize_decimal() { +fn into_filesize_float() { let actual = nu!("1.2 | into filesize"); assert!(actual.out.contains("1 B")); diff --git a/crates/nu-command/tests/commands/math/mod.rs b/crates/nu-command/tests/commands/math/mod.rs index a9b8075df..e9620388a 100644 --- a/crates/nu-command/tests/commands/math/mod.rs +++ b/crates/nu-command/tests/commands/math/mod.rs @@ -117,7 +117,7 @@ fn error_zero_division_int_int() { } #[test] -fn error_zero_division_decimal_int() { +fn error_zero_division_float_int() { let actual = nu!(pipeline( r#" 1.0 / 0 @@ -128,7 +128,7 @@ fn error_zero_division_decimal_int() { } #[test] -fn error_zero_division_int_decimal() { +fn error_zero_division_int_float() { let actual = nu!(pipeline( r#" 1 / 0.0 @@ -139,7 +139,7 @@ fn error_zero_division_int_decimal() { } #[test] -fn error_zero_division_decimal_decimal() { +fn error_zero_division_float_float() { let actual = nu!(pipeline( r#" 1.0 / 0.0 @@ -194,7 +194,7 @@ fn error_zero_floor_division_int_int() { } #[test] -fn error_zero_floor_division_decimal_int() { +fn error_zero_floor_division_float_int() { let actual = nu!(pipeline( r#" 1.0 // 0 @@ -205,7 +205,7 @@ fn error_zero_floor_division_decimal_int() { } #[test] -fn error_zero_floor_division_int_decimal() { +fn error_zero_floor_division_int_float() { let actual = nu!(pipeline( r#" 1 // 0.0 @@ -216,7 +216,7 @@ fn error_zero_floor_division_int_decimal() { } #[test] -fn error_zero_floor_division_decimal_decimal() { +fn error_zero_floor_division_float_float() { let actual = nu!(pipeline( r#" 1.0 // 0.0 diff --git a/crates/nu-command/tests/commands/str_/into_string.rs b/crates/nu-command/tests/commands/str_/into_string.rs index 3dff881e4..afc0d227c 100644 --- a/crates/nu-command/tests/commands/str_/into_string.rs +++ b/crates/nu-command/tests/commands/str_/into_string.rs @@ -21,7 +21,7 @@ fn from_number() { } #[test] -fn from_decimal() { +fn from_float() { let actual = nu!(r#" echo 1.5 | into string "#); @@ -90,7 +90,7 @@ fn from_filesize() { } #[test] -fn from_decimal_correct_trailing_zeros() { +fn from_float_correct_trailing_zeros() { let actual = nu!(r#" 1.23000 | into string -d 3 "#); @@ -99,7 +99,7 @@ fn from_decimal_correct_trailing_zeros() { } #[test] -fn from_int_decimal_correct_trailing_zeros() { +fn from_int_float_correct_trailing_zeros() { let actual = nu!(r#" 1.00000 | into string -d 3 "#); @@ -108,7 +108,7 @@ fn from_int_decimal_correct_trailing_zeros() { } #[test] -fn from_int_decimal_trim_trailing_zeros() { +fn from_int_float_trim_trailing_zeros() { let actual = nu!(r#" 1.00000 | into string | $"($in) flat" "#); diff --git a/crates/nu-command/tests/commands/str_/mod.rs b/crates/nu-command/tests/commands/str_/mod.rs index 710da2580..7991ad822 100644 --- a/crates/nu-command/tests/commands/str_/mod.rs +++ b/crates/nu-command/tests/commands/str_/mod.rs @@ -135,7 +135,7 @@ fn converts_to_int() { } #[test] -fn converts_to_decimal() { +fn converts_to_float() { let actual = nu!(pipeline( r#" echo "3.1, 0.0415" diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index b73a75f1f..b56c60022 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2709,7 +2709,7 @@ pub fn parse_shape_name( b"duration" => SyntaxShape::Duration, b"error" => SyntaxShape::Error, b"expr" => SyntaxShape::Expression, - b"float" | b"decimal" => SyntaxShape::Decimal, + b"float" => SyntaxShape::Float, b"filesize" => SyntaxShape::Filesize, b"full-cell-path" => SyntaxShape::FullCellPath, b"glob" => SyntaxShape::GlobPattern, @@ -4654,7 +4654,7 @@ pub fn parse_value( expression } SyntaxShape::Number => parse_number(working_set, span), - SyntaxShape::Decimal => parse_float(working_set, span), + SyntaxShape::Float => parse_float(working_set, span), SyntaxShape::Int => parse_int(working_set, span), SyntaxShape::Duration => parse_duration(working_set, span), SyntaxShape::DateTime => parse_datetime(working_set, span), diff --git a/crates/nu-protocol/src/syntax_shape.rs b/crates/nu-protocol/src/syntax_shape.rs index 6006849ec..f2447cb85 100644 --- a/crates/nu-protocol/src/syntax_shape.rs +++ b/crates/nu-protocol/src/syntax_shape.rs @@ -31,9 +31,6 @@ pub enum SyntaxShape { /// A datetime value, eg `2022-02-02` or `2019-10-12T07:20:50.52+00:00` DateTime, - /// A decimal value, eg `1.0` - Decimal, - /// A directory is allowed Directory, @@ -52,6 +49,9 @@ pub enum SyntaxShape { /// A filesize value is allowed, eg `10kb` Filesize, + /// A floating point value, eg `1.0` + Float, + /// A dotted path to navigate the table (including variable) FullCellPath, @@ -82,7 +82,7 @@ pub enum SyntaxShape { /// Nothing Nothing, - /// Only a numeric (integer or decimal) value is allowed + /// Only a numeric (integer or float) value is allowed Number, /// One of a list of possible items, checked in order @@ -137,7 +137,7 @@ impl SyntaxShape { SyntaxShape::Expression => Type::Any, SyntaxShape::Filepath => Type::String, SyntaxShape::Directory => Type::String, - SyntaxShape::Decimal => Type::Float, + SyntaxShape::Float => Type::Float, SyntaxShape::Filesize => Type::Filesize, SyntaxShape::FullCellPath => Type::Any, SyntaxShape::GlobPattern => Type::String, @@ -189,7 +189,7 @@ impl Display for SyntaxShape { SyntaxShape::Number => write!(f, "number"), SyntaxShape::Range => write!(f, "range"), SyntaxShape::Int => write!(f, "int"), - SyntaxShape::Decimal => write!(f, "decimal"), + SyntaxShape::Float => write!(f, "float"), SyntaxShape::Filepath => write!(f, "path"), SyntaxShape::Directory => write!(f, "directory"), SyntaxShape::GlobPattern => write!(f, "glob"), diff --git a/crates/nu-std/std/dt.nu b/crates/nu-std/std/dt.nu index c7432783a..4ee76c0cb 100644 --- a/crates/nu-std/std/dt.nu +++ b/crates/nu-std/std/dt.nu @@ -149,7 +149,7 @@ export def datetime-diff [from: datetime, to: datetime] { $result = (borrow-second $from_expanded $result) } - $result.millisecond = ($result.nanosecond / 1_000_000 | into int) # don't want a decimal + $result.millisecond = ($result.nanosecond / 1_000_000 | into int) # don't want a float $result.microsecond = (($result.nanosecond mod 1_000_000) / 1_000 | into int) $result.nanosecond = ($result.nanosecond mod 1_000 | into int) diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 2c22ac708..747d02911 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -561,7 +561,7 @@ fn index_out_of_bounds() { } #[test] -fn negative_decimal_start() { +fn negative_float_start() { let actual = nu!(" -1.3 + 4 ");