From c23ff074e45056e53ddbc10de8903422df2d0586 Mon Sep 17 00:00:00 2001 From: Gregory Michael Travis Date: Tue, 12 Nov 2024 18:29:25 -0500 Subject: [PATCH] Make `round_integer` work for positive decimal places (#11457) --- .../Standard/Database/0.0.0-dev/src/DB_Column.enso | 7 ++----- .../Column_Operations_Spec.enso | 11 +++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/distribution/lib/Standard/Database/0.0.0-dev/src/DB_Column.enso b/distribution/lib/Standard/Database/0.0.0-dev/src/DB_Column.enso index 99bd28613d54..250f1a454803 100644 --- a/distribution/lib/Standard/Database/0.0.0-dev/src/DB_Column.enso +++ b/distribution/lib/Standard/Database/0.0.0-dev/src/DB_Column.enso @@ -1000,11 +1000,8 @@ type DB_Column round_integer : Integer -> Boolean -> DB_Column round_integer self decimal_places use_bankers = new_name = self.naming_helper.function_name "round" [self] - Runtime.assert (decimal_places <= 0) - ## We assert that the decimal_places is non-positive, so the scale will be an Integer. - We can tell that to the type-checker by adding an annotation. - scale = (10 ^ -decimal_places) : Integer - halfway = scale.div 2 + scale = 10 ^ -decimal_places + halfway = scale / 2 remainder = self % scale remainder.let "remainer" remainder-> scaled_down = (self / scale).truncate . cast Value_Type.Integer diff --git a/test/Table_Tests/src/Common_Table_Operations/Column_Operations_Spec.enso b/test/Table_Tests/src/Common_Table_Operations/Column_Operations_Spec.enso index ff9bf73903b1..aa66f3074dc5 100644 --- a/test/Table_Tests/src/Common_Table_Operations/Column_Operations_Spec.enso +++ b/test/Table_Tests/src/Common_Table_Operations/Column_Operations_Spec.enso @@ -180,6 +180,17 @@ add_column_operation_specs suite_builder setup = 3.1 . round 16 . should_fail_with Illegal_Argument 3.1 . round -16 . should_fail_with Illegal_Argument + group_builder.specify "Non-builtin integer column rounding" <| + cases = [[0, 0, 0], [3, 0, 3], [-3, 0, -3], [3, 1, 3], [-3, 1, -3]] + + [[11, -1, 10], [24, -1, 20], [-10, -1, -10], [-11, -1, -10]] + bankers_cases = [[0.235, 2, 0.24], [0.225, 2, 0.22], [-0.235, 2, -0.24]] + + [[-0.225, 2, -0.22], [12300, -2, 12300], [12301, -2, 12300]] + + [[-12300, -2, -12300], [-12301, -2, -12300]] + cases_transposed = cases.transpose + bankers_cases_transposed = bankers_cases.transpose + round_batch (cases_transposed.at 0) (cases_transposed.at 1) False . should_equal (cases_transposed.at 2) + round_batch (bankers_cases_transposed.at 0) (bankers_cases_transposed.at 1) True . should_equal (bankers_cases_transposed.at 2) + build_sorted_table table_structure table_builder=setup.table_builder = # Workaround for https://github.com/enso-org/enso/issues/10321 if setup.prefix.contains "Snowflake" . not then table_builder table_structure else