Skip to content

Commit

Permalink
Make round_integer work for positive decimal places (#11457)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryTravis authored Nov 12, 2024
1 parent 978a009 commit c23ff07
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c23ff07

Please sign in to comment.