diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fe892b4c340..89a70bc3007f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -140,6 +140,8 @@ - [Changed in-memory `Column.zip` to match `Table.zip`, removing the `function` parameter][12626] - [Added Regex_match for Postgres][12663] +- [Replace `use_bankers` flag with `Rounding_Mode` in all `round` + methods][12641] [11926]: https://github.com/enso-org/enso/pull/11926 [12031]: https://github.com/enso-org/enso/pull/12031 @@ -163,6 +165,7 @@ [12590]: https://github.com/enso-org/enso/pull/12590 [12626]: https://github.com/enso-org/enso/pull/12626 [12663]: https://github.com/enso-org/enso/pull/12663 +[12641]: https://github.com/enso-org/enso/pull/12641 #### Enso Language & Runtime diff --git a/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Decimal.md b/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Decimal.md index 98175681f168..5a5dd567d3c5 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Decimal.md +++ b/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Decimal.md @@ -31,7 +31,7 @@ - pow self exp:Standard.Base.Data.Numbers.Integer -> Standard.Base.Data.Decimal.Decimal - precision self -> Standard.Base.Data.Numbers.Integer - remainder self that:Standard.Base.Data.Decimal.Decimal -> Standard.Base.Data.Decimal.Decimal - - round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Data.Decimal.Decimal + - round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Data.Decimal.Decimal - scale self -> Standard.Base.Data.Numbers.Integer - set_scale self new_scale:Standard.Base.Data.Numbers.Integer -> Standard.Base.Data.Decimal.Decimal - signum self -> Standard.Base.Data.Numbers.Integer diff --git a/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numbers.md b/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numbers.md index e51a681308a2..21544a192467 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numbers.md +++ b/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numbers.md @@ -18,7 +18,7 @@ - min_value -> Standard.Base.Any.Any - negate self -> Standard.Base.Data.Numbers.Float - parse text:Standard.Base.Data.Text.Text locale:(Standard.Base.Data.Locale.Locale|Standard.Base.Nothing.Nothing)= format:(Standard.Base.Data.Text.Text|Standard.Base.Nothing.Nothing)= -> Standard.Base.Any.Any - - round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any + - round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Data.Numbers.Float - to_decimal self -> Standard.Base.Data.Decimal.Decimal - to_float self -> Standard.Base.Data.Numbers.Float - truncate self -> Standard.Base.Data.Numbers.Integer @@ -47,7 +47,7 @@ - floor self -> Standard.Base.Data.Numbers.Integer - negate self -> Standard.Base.Data.Numbers.Integer - parse text:Standard.Base.Data.Text.Text radix:Standard.Base.Data.Numbers.Integer= -> Standard.Base.Any.Any - - round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any + - round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Any.Any - to_decimal self -> Standard.Base.Data.Decimal.Decimal - to_float self -> Standard.Base.Data.Numbers.Float - truncate self -> Standard.Base.Data.Numbers.Integer diff --git a/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numeric/Rounding_Mode.md b/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numeric/Rounding_Mode.md index 47bf4af47ffb..984ca4d099e7 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numeric/Rounding_Mode.md +++ b/distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numeric/Rounding_Mode.md @@ -1,6 +1,6 @@ ## Enso Signatures 1.0 ## module Standard.Base.Data.Numeric.Rounding_Mode - type Rounding_Mode - - Value rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.RoundingMode - - bankers -> Standard.Base.Any.Any - - half_up -> Standard.Base.Any.Any + - Bankers + - Half_Up +- Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode.from that:Standard.Base.Data.Boolean.Boolean -> Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Decimal.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Decimal.enso index fb60b5aa3910..73f232e660ad 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Decimal.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Decimal.enso @@ -991,7 +991,7 @@ type Decimal Round to a specified number of decimal places. By default, rounding uses "symmetric round-half-up", also known as - "half-up." If use_bankers=True, then it uses "round-half-even", also + "half-up." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also known as "banker's rounding". Arguments: @@ -999,7 +999,8 @@ type Decimal negative, which results in rounding to positive integer powers of 10. Must be between Java `Integer.MIN_VALUE` and `Integer.MAX_VALUE` (-2147483648 and 2147483647) (inclusive). - - use_bankers: Rounds mid-point to nearest even number. + - rounding_mode: specifies how to break ties for the least significant + digit. ! Error Conditions @@ -1032,12 +1033,12 @@ type Decimal > Example Use Banker's Rounding. - Decimal.new "2.5" . round use_bankers=True + Decimal.new "2.5" . round rounding_mode=Rounding_Mode.Bankers # => 2 - round : Integer -> Boolean -> Decimal - round self (decimal_places:Integer=0) (use_bankers:Boolean=False) -> Decimal = + round : Integer -> Rounding_Mode -> Decimal + round self (decimal_places:Integer=0) (rounding_mode : Rounding_Mode = ..Half_Up) -> Decimal = out_of_range = decimal_places > Java_Integer.MAX_VALUE || decimal_places < Java_Integer.MIN_VALUE - if out_of_range.not then Decimal.Value (Decimal_Utils.round self.big_decimal decimal_places use_bankers) else + if out_of_range.not then Decimal.Value (Decimal_Utils.round self.big_decimal decimal_places (rounding_mode == Rounding_Mode.Bankers)) else message = "round decimal_places must be between "+Java_Integer.MIN_VALUE.to_text+" and "+Java_Integer.MAX_VALUE.to_text+" (inclusive), but was "+decimal_places.to_text Error.throw (Out_Of_Range.Error decimal_places message) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso index c88b5411a0b3..55e63f875f4e 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso @@ -1,6 +1,7 @@ import project.Any.Any import project.Data.Decimal.Decimal import project.Data.Locale.Locale +import project.Data.Numeric.Rounding_Mode.Rounding_Mode import project.Data.Ordering.Comparable import project.Data.Text.Text import project.Error.Error @@ -659,7 +660,7 @@ type Float Round to a specified number of decimal places. By default, rounding uses "symmetric round-half-up", also known as - "half-up." If use_bankers=True, then it uses "round-half-even", also + "half-up." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also known as "banker's rounding". If `decimal_places` > 0, `round` returns a `Float`; otherwise, it @@ -672,7 +673,8 @@ type Float - decimal_places: The number of decimal places to round to. Can be negative, which results in rounding to positive integer powers of 10. Must be between -15 and 15 (inclusive). - - use_bankers: Rounds mid-point to nearest even number. + - rounding_mode: specifies how to break ties for the least significant + digit. ! Error Conditions @@ -705,12 +707,12 @@ type Float > Example Use Banker's Rounding. - 2.5 . round use_bankers=True == 2 - round : Integer -> Boolean -> Integer | Float ! Illegal_Argument - round self (decimal_places:Integer=0) (use_bankers:Boolean=False) = + 2.5 . round rounding_mode=Rounding_Mode.Bankers == 2 + round : Integer -> Rounding_Mode -> Integer | Float ! Illegal_Argument + round self (decimal_places:Integer=0) (rounding_mode : Rounding_Mode = ..Half_Up) -> Float = report_unsupported cp = Error.throw (Illegal_Argument.Error cp.payload.message) Panic.catch Unsupported_Argument_Types handler=report_unsupported - round_float_builtin self decimal_places use_bankers + round_float_builtin self decimal_places (rounding_mode == Rounding_Mode.Bankers) ## GROUP Operators ICON operators @@ -1074,14 +1076,15 @@ type Integer argument. For negative decimal places, see below. By default, rounding uses "symmetric round-half-up", also known as - "half-up." If use_bankers=True, then it uses "round-half-even", also + "half-up." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also known as "banker's rounding". Arguments: - decimal_places: The number of decimal places to round to. Can be negative, which results in rounding to positive integer powers of 10. Must be between -15 and 15 (inclusive). - - use_bankers: Rounds mid-point to nearest even number. + - rounding_mode: specifies how to break ties for the least significant + digit. ! Error Conditions If `self` is outside the range -99999999999999..99999999999999 @@ -1108,14 +1111,14 @@ type Integer > Example Round to the nearest hundred, using Banker's Rounding. - 12250 . round -2 use_bankers=True == 12200 - round self (decimal_places:Integer=0) (use_bankers:Boolean=False) -> Integer ! Illegal_Argument = + 12250 . round -2 rounding_mode=Rounding_Mode.Bankers == 12200 + round self (decimal_places:Integer=0) (rounding_mode : Rounding_Mode = ..Half_Up) -> Integer ! Illegal_Argument = ## We reject values outside the range of `long` here, so we don't also do this check in the Java. Rounding_Helpers.check_round_input self <| report_unsupported cp = Error.throw (Illegal_Argument.Error cp.payload.message) Panic.catch Unsupported_Argument_Types handler=report_unsupported - round_integer_builtin self decimal_places use_bankers + round_integer_builtin self decimal_places (rounding_mode == Rounding_Mode.Bankers) ## GROUP Operators ICON operators diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Math_Context.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Math_Context.enso index aa9b64b3b441..a33a51a4b754 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Math_Context.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Math_Context.enso @@ -42,7 +42,7 @@ type Math_Context specifies exact operations. - rounding_mode: The rounding mode to use. new : Integer | Unlimited -> Rounding_Mode -> Math_Context - new (precision : Integer | Unlimited) (rounding_mode : Rounding_Mode = Rounding_Mode.half_up) = + new (precision : Integer | Unlimited) (rounding_mode : Rounding_Mode = Rounding_Mode.Half_Up) = case precision of - _ : Integer -> Math_Context.Value (MathContext.new precision rounding_mode.rounding_mode) - _ : Unlimited -> Math_Context.Value (MathContext.new 0 rounding_mode.rounding_mode) + _ : Integer -> Math_Context.Value (MathContext.new precision rounding_mode.to_java_rounding_mode) + _ : Unlimited -> Math_Context.Value (MathContext.new 0 rounding_mode.to_java_rounding_mode) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Rounding_Mode.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Rounding_Mode.enso index fb670b8c1056..27e1f75ebe4e 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Rounding_Mode.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Rounding_Mode.enso @@ -1,3 +1,6 @@ +from Standard.Base import all +import project.Errors.Deprecated.Deprecated + polyglot java import java.math.RoundingMode ## A wrapper around the Java @@ -7,17 +10,24 @@ polyglot java import java.math.RoundingMode `Rounding_Mode` is used to specify the method to use for rounding a value in operations on `Decimal`s.` See `Decimal` for more details. type Rounding_Mode - ## PRIVATE - Value (rounding_mode : RoundingMode) - ## ICON math Round towards positive infinity for positive numbers, and negative infinity for negative numbers. (This is the default.) - half_up : Rounding_Mode - half_up = Rounding_Mode.Value RoundingMode.HALF_UP + Half_Up ## ICON math Rownd towards the nearest neighbor, with ties broken by rounding towards the nearest even neighbor. - bankers : Rounding_Mode - bankers = Rounding_Mode.Value RoundingMode.HALF_EVEN + Bankers + + private to_java_rounding_mode self -> RoundingMode = + case self of + Rounding_Mode.Half_Up -> RoundingMode.HALF_UP + Rounding_Mode.Bankers -> RoundingMode.HALF_EVEN + +## PRIVATE + Convert deprecated `use_bankers : Boolean` arguments (passed as + `rounding_mode`) to `Rounding_Mode`. +Rounding_Mode.from (that:Boolean) = + rounding_mode_resolved = if that then Rounding_Mode.Bankers else Rounding_Mode.Half_Up + Warning.attach (Deprecated.Warning "Standard.Base.Data.Numeric" "Rounding_Mode" "use_bankers:Boolean is deprecated, use Rounding_Mode instead") rounding_mode_resolved diff --git a/distribution/lib/Standard/Database/0.0.0-dev/docs/api/DB_Column.md b/distribution/lib/Standard/Database/0.0.0-dev/docs/api/DB_Column.md index 4404c61cdf29..4c0cb5a4eeab 100644 --- a/distribution/lib/Standard/Database/0.0.0-dev/docs/api/DB_Column.md +++ b/distribution/lib/Standard/Database/0.0.0-dev/docs/api/DB_Column.md @@ -77,15 +77,15 @@ - read self max_rows:Standard.Table.Rows_To_Read.Rows_To_Read= -> Standard.Table.Column.Column - regex_match self pattern:(Standard.Database.DB_Column.DB_Column|Standard.Base.Data.Text.Text|Standard.Base.Data.Text.Regex.Regex) -> Standard.Database.DB_Column.DB_Column - rename self name:Standard.Base.Data.Text.Text -> Standard.Base.Any.Any - - round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any - - round_builtin self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any - - round_decimal self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any - - round_float self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any - - round_integer self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any + - round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Any.Any + - round_builtin self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any + - round_decimal self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any + - round_float self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any + - round_integer self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any - second self -> Standard.Base.Any.Any - short_circuit_special_floating_point self exp:Standard.Base.Any.Any -> Standard.Base.Any.Any - should_be_selected_by_type self value_type:Standard.Table.Value_Type.Value_Type -> Standard.Base.Data.Boolean.Boolean - - should_use_builtin_round self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any + - should_use_builtin_round self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any - sort self order:Standard.Base.Data.Sort_Direction.Sort_Direction= -> Standard.Database.DB_Column.DB_Column - sql_type self -> Standard.Base.Any.Any - starts_with self other:(Standard.Database.DB_Column.DB_Column|Standard.Base.Data.Text.Text|Standard.Base.Any.Any) case_sensitivity:Standard.Base.Data.Text.Case_Sensitivity.Case_Sensitivity= -> Standard.Database.DB_Column.DB_Column 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 442608fdbecf..309791cf0631 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 @@ -853,7 +853,7 @@ type DB_Column `Value_Type.Integer` columns. By default, rounding uses "symmetric round-half-up", also known as - "round towards 0." If use_bankers=True, then it uses "round-half-even", + "round towards 0." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also known as "banker's rounding". The return type depends on the backend. @@ -862,7 +862,8 @@ type DB_Column - decimal_places: The number of decimal places to round to. Can be negative, which results in rounding to positive integer powers of 10. Must be between -15 and 15 (inclusive). - - use_bankers: Rounds mid-point to nearest even number. + - rounding_mode: specifies how to break ties for the least significant + digit. ! NaN/Inf If a `NaN` or `Inf` value is passed to `round`, it immediately returns @@ -884,14 +885,14 @@ type DB_Column import Standard.Examples example_round = Examples.decimal_column.round 2 - round self decimal_places:Integer=0 use_bankers:Boolean=False -> DB_Column ! Illegal_Argument | Invalid_Value_Type = + round self decimal_places:Integer=0 (rounding_mode : Rounding_Mode = ..Half_Up) -> DB_Column ! Illegal_Argument | Invalid_Value_Type = Rounding_Helpers.check_decimal_places decimal_places <| Value_Type.expect_numeric self <| - if self.should_use_builtin_round decimal_places use_bankers then self.round_builtin decimal_places use_bankers else + if self.should_use_builtin_round decimal_places rounding_mode then self.round_builtin decimal_places rounding_mode else self.short_circuit_special_floating_point <| case self.value_type of - Value_Type.Integer _ -> self.round_integer decimal_places use_bankers - Value_Type.Float _ -> self.round_float decimal_places use_bankers - Value_Type.Decimal _ _ -> self.round_decimal decimal_places use_bankers + Value_Type.Integer _ -> self.round_integer decimal_places rounding_mode + Value_Type.Float _ -> self.round_float decimal_places rounding_mode + Value_Type.Decimal _ _ -> self.round_decimal decimal_places rounding_mode _ -> expected = "Decimal, Float, or Integer" Error.throw (Invalid_Value_Type.Column expected self.value_type self.name) @@ -903,9 +904,9 @@ type DB_Column - We are not passing a floating point value to round with a non-zero decimal_places param (prohibited by postgres) (Note that Postgres is fine with a `numeric` value here, just not `double precision`.) should_use_builtin_round : Integer -> Boolean -> Boolean - should_use_builtin_round self decimal_places use_bankers = + should_use_builtin_round self decimal_places rounding_mode = # Don't use for banker's rounding (unless the dialect has a builtin for it) - bankers_ok = self.connection.dialect.flagged ..Use_Builtin_Bankers || use_bankers.not + bankers_ok = self.connection.dialect.flagged ..Use_Builtin_Bankers || rounding_mode != Rounding_Mode.Bankers # Don't use for negative decimal places, if the backend doesn't support it negative_dp_ok = decimal_places >= 0 || self.connection.dialect.flagged ..Supports_Negative_Decimal_Places # Don't use for floating-point inputs if decimal_places != 0, for Postgres @@ -915,12 +916,12 @@ type DB_Column ## PRIVATE Round a float-like column using the backend's builtin ROUND function. round_builtin : Integer -> Boolean -> DB_Column - round_builtin self decimal_places use_bankers = + round_builtin self decimal_places rounding_mode = new_name = self.naming_helper.function_name "round" [self] - case use_bankers of - True -> + case rounding_mode of + Rounding_Mode.Bankers -> self.make_binary_op "ROUND_BANKERS" (SQL_Expression.Literal decimal_places.to_text) new_name - False -> + Rounding_Mode.Half_Up -> case decimal_places == 0 of True -> self.make_unary_op "ROUND" new_name @@ -930,7 +931,7 @@ type DB_Column ## PRIVATE Round a float-like column. round_float : Integer -> Boolean -> DB_Column - round_float self decimal_places use_bankers = + round_float self decimal_places rounding_mode = new_name = self.naming_helper.function_name "round" [self] scale = 10 ^ decimal_places scaled = self * scale @@ -942,7 +943,7 @@ type DB_Column round_midpoint.let "round_midpoint" round_midpoint-> ((scaled.truncate % 2) == 0).let "scaled_truncate_mod_2_equals_0" scaled_truncate_mod_2_equals_0-> even_is_up = (self >= 0).iif scaled_truncate_mod_2_equals_0.not scaled_truncate_mod_2_equals_0 - half_goes_up = if use_bankers then even_is_up else self >= 0 + half_goes_up = if rounding_mode == Rounding_Mode.Bankers then even_is_up else self >= 0 do_round_up = half_goes_up.let "half_goes_up" x-> x.iif (self >= round_midpoint) (self > round_midpoint) result = do_round_up.let "do_round_up" x-> x.iif ((round_base + 1.0) / scale) (round_base / scale) result.rename new_name @@ -950,7 +951,7 @@ type DB_Column ## PRIVATE Round a float-like column. round_decimal : Integer -> Boolean -> DB_Column - round_decimal self decimal_places use_bankers = + round_decimal self decimal_places rounding_mode = # Construct a constant Decimal column. k x = self.const x . cast Value_Type.Decimal new_name = self.naming_helper.function_name "round" [self] @@ -963,7 +964,7 @@ type DB_Column round_midpoint.let "round_midpoint" round_midpoint-> ((scaled.truncate.decimal_mod (k 2)) != k 0).let "scaled_truncate_mod_2_equals_0" scaled_truncate_mod_2_equals_0-> even_is_up = (self >= k 0).iif scaled_truncate_mod_2_equals_0.not scaled_truncate_mod_2_equals_0 - half_goes_up = if use_bankers then even_is_up else self >= k 0 + half_goes_up = if rounding_mode == Rounding_Mode.Bankers then even_is_up else self >= k 0 do_round_up = half_goes_up.let "half_goes_up" x-> x.iif (self >= round_midpoint) (self > round_midpoint) result = do_round_up.let "do_round_up" x-> x.iif ((round_base + k 1).decimal_div scale) (round_base.decimal_div scale) result.rename new_name @@ -971,7 +972,7 @@ type DB_Column ## PRIVATE Round an integer column. round_integer : Integer -> Boolean -> DB_Column - round_integer self decimal_places use_bankers = + round_integer self decimal_places rounding_mode = new_name = self.naming_helper.function_name "round" [self] scale = 10 ^ -decimal_places halfway = scale / 2 @@ -982,11 +983,11 @@ type DB_Column result_unnudged = scaled_down * scale result_unnudged.let "result_unnudged" result_unnudged-> if_non_neg = - half_goes_up = if use_bankers then (scaled_down % 2) != 0 else self >= 0 + half_goes_up = if rounding_mode == Rounding_Mode.Bankers then (scaled_down % 2) != 0 else self >= 0 round_up = half_goes_up.let "half_goes_up" x-> x.iif (remainder >= halfway) (remainder > halfway) round_up.let "round_up" x-> x.iif (result_unnudged + scale) result_unnudged if_neg = - half_goes_up = if use_bankers then (scaled_down % 2) == 0 else self >= 0 + half_goes_up = if rounding_mode == Rounding_Mode.Bankers then (scaled_down % 2) == 0 else self >= 0 round_up = half_goes_up.let "half_goes_up" x-> x.iif (remainder < -halfway) (remainder <= -halfway) round_up.let "round_up" x-> x.iif (result_unnudged - scale) result_unnudged diff --git a/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column.md b/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column.md index 8fc1c781fa45..235a475b5a70 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column.md +++ b/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column.md @@ -85,7 +85,7 @@ - rename self name:Standard.Base.Data.Text.Text -> Standard.Base.Any.Any - requires_numeric_formatter_check self -> Standard.Base.Any.Any - reverse self -> Standard.Table.Column.Column - - round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any + - round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Any.Any - running self statistic:Standard.Base.Data.Statistics.Statistic= name:Standard.Base.Data.Text.Text= -> Standard.Table.Column.Column - second self -> Standard.Base.Any.Any - should_be_selected_by_type self value_type:Standard.Table.Value_Type.Value_Type -> Standard.Base.Data.Boolean.Boolean diff --git a/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column_Operation.md b/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column_Operation.md index ee84095faab8..383728eadbff 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column_Operation.md +++ b/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Column_Operation.md @@ -17,7 +17,7 @@ - Not input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Boolean.Boolean) - Or input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Boolean.Boolean) rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Boolean.Boolean) - Power input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number) rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number) - - Round input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number) precision:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= + - Round input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number) precision:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= - Subtract input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number) rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number) - Trim input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Text.Text) where:Standard.Base.Data.Text.Location.Location= what:(Standard.Base.Data.Text.Text|Standard.Table.Column_Ref.Column_Ref)= - Truncate input:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number|Standard.Base.Data.Time.Date_Time.Date_Time) diff --git a/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Simple_Expression.md b/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Simple_Expression.md index 92404d34b1a6..d925053984de 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Simple_Expression.md +++ b/distribution/lib/Standard/Table/0.0.0-dev/docs/api/Simple_Expression.md @@ -27,7 +27,7 @@ - Multiply rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number)= - Negate - Power rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number)= - - Round precision:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= + - Round precision:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= - Subtract rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number)= - Truncate - create_widget table:Standard.Table.Internal.Table_Ref.Table_Ref display:Standard.Base.Metadata.Display= -> Standard.Base.Any.Any @@ -54,7 +54,7 @@ - Not - Or rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Boolean.Boolean)= - Power rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number)= - - Round precision:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= + - Round precision:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= - Subtract rhs:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Number)= - Text operation:Standard.Table.Simple_Expression.Text_Operation= - Text_Left length:(Standard.Table.Column_Ref.Column_Ref|Standard.Table.Expression.Expression|Standard.Base.Data.Numbers.Integer)= diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso index 951d7ac31c75..1933e378abac 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso @@ -867,7 +867,7 @@ type Column argument. For negative decimal places, see below. By default, rounding uses "asymmetric round-half-up", also known as - "round towards positive infinity." If use_bankers=True, then it uses + "round towards positive infinity." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also known as "banker's rounding". If the column is of type `Float` and `decimal_places` > 0, `round` @@ -878,7 +878,8 @@ type Column - decimal_places: The number of decimal places to round to. Can be negative, which results in rounding to positive integer powers of 10. Must be between -15 and 15 (inclusive). - - use_bankers: Rounds mid-point to nearest even number. + - rounding_mode: specifies how to break ties for the least significant + digit. - on_problems: Specifies how to handle if a problem occurs, raising as a warning by default. @@ -910,10 +911,10 @@ type Column Round a column of `Float` values`. Column.from_vector "foo" [1.2, 2.3, 3.6] . round == (Column.from_vector "foo" [1, 2, 4]) - round self decimal_places:Integer=0 use_bankers:Boolean=False -> Column ! Illegal_Argument | Invalid_Value_Type = Value_Type.expect_numeric self <| + round self decimal_places:Integer=0 (rounding_mode : Rounding_Mode = ..Half_Up) -> Column ! Illegal_Argument | Invalid_Value_Type = Value_Type.expect_numeric self <| Illegal_Argument.handle_java_exception <| Rounding_Helpers.check_decimal_places decimal_places <| new_name = naming_helper.function_name "round" [self] - run_vectorized_ternary_op self Java_Storage.Maps.ROUND decimal_places use_bankers new_name=new_name + run_vectorized_ternary_op self Java_Storage.Maps.ROUND decimal_places (rounding_mode == Rounding_Mode.Bankers) new_name=new_name ## ALIAS int GROUP Standard.Base.Rounding diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Column_Operation.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Column_Operation.enso index aee692b652de..a4276871c573 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Column_Operation.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Column_Operation.enso @@ -31,7 +31,7 @@ type Column_Operation Power (input : Column_Ref|Expression|Number) (rhs : Column_Ref|Expression|Number) ## Rounds values in the column to the specified precision. - Round (input : Column_Ref|Expression|Number) (precision:Integer = 0) (use_bankers:Boolean = False) + Round (input : Column_Ref|Expression|Number) (precision:Integer = 0) (rounding_mode : Rounding_Mode = Rounding_Mode.Half_Up) ## Rounds values in the column up to the nearest integer. Ceil (input : Column_Ref|Expression|Number) @@ -85,7 +85,7 @@ Simple_Expression.from (that:Column_Operation) = Column_Operation.Divide input rhs -> Simple_Expression.Simple_Expr input (Simple_Calculation.Divide rhs) Column_Operation.Mod input rhs -> Simple_Expression.Simple_Expr input (Simple_Calculation.Mod rhs) Column_Operation.Power input rhs -> Simple_Expression.Simple_Expr input (Simple_Calculation.Power rhs) - Column_Operation.Round input precision use_bankers -> Simple_Expression.Simple_Expr input (Simple_Calculation.Round precision use_bankers) + Column_Operation.Round input precision rounding_mode -> Simple_Expression.Simple_Expr input (Simple_Calculation.Round precision rounding_mode) Column_Operation.Ceil input -> Simple_Expression.Simple_Expr input Simple_Calculation.Ceil Column_Operation.Floor input -> Simple_Expression.Simple_Expr input Simple_Calculation.Floor Column_Operation.Truncate input -> Simple_Expression.Simple_Expr input Simple_Calculation.Truncate diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Simple_Expression.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Simple_Expression.enso index 05e3b7ec06e8..f66649795ff5 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Simple_Expression.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Simple_Expression.enso @@ -42,7 +42,7 @@ type Simple_Expression Simple_Calculation.Divide rhs -> input_column / (table.resolve rhs) Simple_Calculation.Mod rhs -> input_column % (table.resolve rhs) Simple_Calculation.Power rhs -> input_column ^ (table.resolve rhs) - Simple_Calculation.Round precision use_bankers -> input_column.round precision use_bankers + Simple_Calculation.Round precision rounding_mode -> input_column.round precision rounding_mode Simple_Calculation.Ceil -> input_column.ceil Simple_Calculation.Floor -> input_column.floor Simple_Calculation.Truncate -> input_column.truncate @@ -166,7 +166,7 @@ type Math_Operation Power (rhs : Column_Ref|Expression|Number = 1) ## Rounds values in the column to the specified precision. - Round (precision:Integer = 0) (use_bankers:Boolean = False) + Round (precision:Integer = 0) (rounding_mode : Rounding_Mode = Rounding_Mode.Half_Up) ## Rounds values in the column up to the nearest integer. Ceil @@ -206,7 +206,7 @@ type Math_Operation Math_Operation.Negate -> column * -1 Math_Operation.Mod rhs -> column % (table.resolve rhs) Math_Operation.Power rhs -> column ^ (table.resolve rhs) - Math_Operation.Round precision use_bankers -> column.round precision use_bankers + Math_Operation.Round precision rounding_mode -> column.round precision rounding_mode Math_Operation.Ceil -> column.ceil Math_Operation.Floor -> column.floor Math_Operation.Truncate -> column.truncate @@ -346,7 +346,7 @@ type Simple_Calculation Power (rhs : Column_Ref|Expression|Number = 1) ## Rounds values in the column to the specified precision. - Round (precision:Integer = 0) (use_bankers:Boolean = False) + Round (precision:Integer = 0) (rounding_mode : Rounding_Mode = Rounding_Mode.Half_Up) ## Rounds values in the column up to the nearest integer. Ceil diff --git a/lib/scala/common-polyglot-core-utils/src/main/java/org/enso/polyglot/common_utils/Core_Math_Utils.java b/lib/scala/common-polyglot-core-utils/src/main/java/org/enso/polyglot/common_utils/Core_Math_Utils.java index 787ca8d2ae51..eed416356c01 100644 --- a/lib/scala/common-polyglot-core-utils/src/main/java/org/enso/polyglot/common_utils/Core_Math_Utils.java +++ b/lib/scala/common-polyglot-core-utils/src/main/java/org/enso/polyglot/common_utils/Core_Math_Utils.java @@ -80,8 +80,8 @@ public static double roundDouble(double n, long decimalPlaces, boolean useBanker *

For integers, rounding to 0 or more decimal places simply returns the argument. * *

By default, rounding uses "asymmetric round-half-up", also known as "round towards positive - * infinity." If use_bankers=True, then it uses "round-half-even", also known as "banker's - * rounding". + * infinity." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also known + * as "banker's rounding". * * @param n the number to round. * @param decimalPlaces the number of decimal places to round to. Can be negative, which results diff --git a/test/Base_Tests/src/Data/Decimal_Spec.enso b/test/Base_Tests/src/Data/Decimal_Spec.enso index cb33c6a3e928..0402452306c9 100644 --- a/test/Base_Tests/src/Data/Decimal_Spec.enso +++ b/test/Base_Tests/src/Data/Decimal_Spec.enso @@ -42,7 +42,7 @@ add_specs suite_builder = Decimal.new "123.45" mc . should_equal (Decimal.new "123.5") group_builder.specify "should be able to construct a Decimal from a string, with an explicit precision, using a non-default rounding" <| - mc = Math_Context.new 4 Rounding_Mode.bankers + mc = Math_Context.new 4 Rounding_Mode.Bankers Decimal.new "123.45" mc . should_have_rep [1234, 4, 1] Decimal.new "123.45" mc . should_equal (Decimal.new "123.4") @@ -59,7 +59,7 @@ add_specs suite_builder = Decimal.new 1234500 mc . should_equal (Decimal.new 1235000) group_builder.specify "should be able to construct a Decimal from a small integer, with an explicit precision, using the non-default rounding" <| - mc = Math_Context.new 4 Rounding_Mode.bankers + mc = Math_Context.new 4 Rounding_Mode.Bankers Decimal.new 1234500 mc . should_have_rep [1234, 4, -3] Decimal.new 1234500 mc . should_equal (Decimal.new 1234000) @@ -72,7 +72,7 @@ add_specs suite_builder = Decimal.new 495782984723948723947239938732974241234500 mc . should_equal (Decimal.new 495782984723948723947239938732974241235000 ) group_builder.specify "should be able to construct a Decimal from a large integer, with an explicit precision, using the non-default rounding" <| - mc = Math_Context.new 39 Rounding_Mode.bankers + mc = Math_Context.new 39 Rounding_Mode.Bankers Decimal.new 495782984723948723947239938732974241234500 mc . should_have_rep [495782984723948723947239938732974241234 , 39, -3] Decimal.new 495782984723948723947239938732974241234500 mc . should_equal (Decimal.new 495782984723948723947239938732974241234000 ) @@ -87,7 +87,7 @@ add_specs suite_builder = d.should_equal (Decimal.new "123.13") group_builder.specify "should be able to construct a Decimal from a float, with an explicit precision, using a non-default rounding" <| - mc = Math_Context.new 5 Rounding_Mode.bankers + mc = Math_Context.new 5 Rounding_Mode.Bankers d = Decimal.from_float 123.125 mc d.should_have_rep [12312, 5, 2] d.should_equal (Decimal.new "123.12") @@ -507,17 +507,17 @@ add_specs suite_builder = group_builder.specify "should allow arithmetic with Decimals, with precision setting and rounding mode" <| (Decimal.from_float 3.4 . add (Decimal.from_float 20.05) (Math_Context.new 3)) . should_equal (Decimal.from_float 23.5) - (Decimal.from_float 3.4 . add (Decimal.from_float 20.05) (Math_Context.new 3 Rounding_Mode.bankers)) . should_equal (Decimal.from_float 23.4) + (Decimal.from_float 3.4 . add (Decimal.from_float 20.05) (Math_Context.new 3 Rounding_Mode.Bankers)) . should_equal (Decimal.from_float 23.4) (Decimal.from_float -3.4 . add (Decimal.from_float -20.05) (Math_Context.new 3)) . should_equal (Decimal.from_float -23.5) - (Decimal.from_float -3.4 . add (Decimal.from_float -20.05) (Math_Context.new 3 Rounding_Mode.bankers)) . should_equal (Decimal.from_float -23.4) + (Decimal.from_float -3.4 . add (Decimal.from_float -20.05) (Math_Context.new 3 Rounding_Mode.Bankers)) . should_equal (Decimal.from_float -23.4) (Decimal.from_float -867.625 . subtract (Decimal.from_float -33.05) (Math_Context.new 5)) . should_equal (Decimal.from_float -834.58) - (Decimal.from_float -867.625 . subtract (Decimal.from_float -33.05) (Math_Context.new 5 Rounding_Mode.bankers)) . should_equal (Decimal.from_float -834.58) + (Decimal.from_float -867.625 . subtract (Decimal.from_float -33.05) (Math_Context.new 5 Rounding_Mode.Bankers)) . should_equal (Decimal.from_float -834.58) (Decimal.from_float 49.31 . multiply (Decimal.from_integer 5) (Math_Context.new 4)) . should_equal (Decimal.from_float 246.6) - (Decimal.from_float 49.31 . multiply (Decimal.from_integer 5) (Math_Context.new 4 Rounding_Mode.bankers)) . should_equal (Decimal.from_float 246.6) + (Decimal.from_float 49.31 . multiply (Decimal.from_integer 5) (Math_Context.new 4 Rounding_Mode.Bankers)) . should_equal (Decimal.from_float 246.6) (Decimal.from_float 49.29 . multiply (Decimal.from_integer 5) (Math_Context.new 4)) . should_equal (Decimal.from_float 246.5) - (Decimal.from_float 49.29 . multiply (Decimal.from_integer 5) (Math_Context.new 4 Rounding_Mode.bankers)) . should_equal (Decimal.from_float 246.4) + (Decimal.from_float 49.29 . multiply (Decimal.from_integer 5) (Math_Context.new 4 Rounding_Mode.Bankers)) . should_equal (Decimal.from_float 246.4) (Decimal.from_float 247.75 . divide (Decimal.from_integer -5) (Math_Context.new 3)) . should_equal (Decimal.from_float -49.6) - (Decimal.from_float 247.75 . divide (Decimal.from_integer -5) (Math_Context.new 3 Rounding_Mode.bankers)) . should_equal (Decimal.from_float -49.6) + (Decimal.from_float 247.75 . divide (Decimal.from_integer -5) (Math_Context.new 3 Rounding_Mode.Bankers)) . should_equal (Decimal.from_float -49.6) group_builder.specify "should allow mixed arithmetic (integer)" <| (Decimal.from_integer 1 + 2) . should_equal 3 @@ -843,11 +843,11 @@ add_specs suite_builder = Decimal.new "0" . signum . should_equal 0 suite_builder.group "(Decimal_Spec) Rounding" group_builder-> - do_round n dp=0 use_bankers=False = - d = Decimal.new n . round dp use_bankers + do_round n dp=0 rounding_mode=Rounding_Mode.Half_Up = + d = Decimal.new n . round dp rounding_mode d.to_float - Round_Spec.add_specs group_builder (Round_Spec.Batch_Runner.from_function do_round) run_advanced=True + Round_Spec.add_specs False group_builder (Round_Spec.Batch_Runner.from_function do_round) run_advanced=True group_builder.specify "Large values" <| Decimal.new "1234.5678E-50" . round 53 . should_equal (Decimal.new "1234.568E-50") @@ -873,7 +873,7 @@ add_specs suite_builder = Decimal.new "3.3" . round . should_equal (Decimal.new "3") Decimal.new "3.1415" . round 2 . should_equal (Decimal.new "3.14") Decimal.new "1234.5678E-50" . round 53 . should_equal (Decimal.new "1234.568E-50") - Decimal.new "2.5" . round use_bankers=True . should_equal 2 + Decimal.new "2.5" . round rounding_mode=Rounding_Mode.Bankers . should_equal 2 suite_builder.group "(Decimal_Spec) floor, ceil, truncate" group_builder-> group_builder.specify "should calculate floor correctly" <| diff --git a/test/Base_Tests/src/Data/Numbers_Spec.enso b/test/Base_Tests/src/Data/Numbers_Spec.enso index 1d8f654d12f2..8c9fb4d7e34e 100644 --- a/test/Base_Tests/src/Data/Numbers_Spec.enso +++ b/test/Base_Tests/src/Data/Numbers_Spec.enso @@ -607,7 +607,7 @@ add_specs suite_builder = ((99999999999998 * 1000).div 1000) . round . should_equal 99999999999998 suite_builder.group "Rounding" group_builder-> - Round_Spec.add_specs group_builder (Round_Spec.Batch_Runner.from_function .round) run_advanced=True + Round_Spec.add_specs False group_builder (Round_Spec.Batch_Runner.from_function .round) run_advanced=True group_builder.specify "Decimal places out of range" <| 3.1 . round 16 . should_fail_with Illegal_Argument diff --git a/test/Base_Tests/src/Data/Round_Spec.enso b/test/Base_Tests/src/Data/Round_Spec.enso index 7820f8447793..9d0a8b4f7416 100644 --- a/test/Base_Tests/src/Data/Round_Spec.enso +++ b/test/Base_Tests/src/Data/Round_Spec.enso @@ -1,5 +1,6 @@ from Standard.Base import all import Standard.Base.Errors.Common.Type_Error +import Standard.Base.Errors.Deprecated.Deprecated import Standard.Base.Errors.Illegal_Argument.Illegal_Argument from Standard.Test import all @@ -10,25 +11,29 @@ type Batch_Runner Value (run_batch : Vector -> Vector -> Boolean -> Vector) (run_one : Number -> Integer -> Boolean -> Number) from_function (f : Number -> Integer -> Boolean -> Number) = - batch values_vec dps_vec use_bankers = - values_vec.zip dps_vec v-> dp-> f v dp use_bankers + batch values_vec dps_vec rounding_mode = + values_vec.zip dps_vec v-> dp-> f v dp rounding_mode Batch_Runner.Value batch f - run self (use_bankers : Boolean) (action : Batch_Builder -> Nothing) = + run self (rounding_mode : Rounding_Mode | Boolean) (action : Batch_Builder -> Nothing) = configs = Vector.build vector_builder-> action (Batch_Builder.Value vector_builder . round) values_vec = configs.map c-> c.at 0 dps_vec = configs.map c-> c.at 1 expected_vec = configs.map c-> c.at 2 epsilon_vec = configs.map c-> c.at 3 - got_vec = self.run_batch values_vec dps_vec use_bankers + warning_vec = configs.map c-> c.at 4 + got_vec = self.run_batch values_vec dps_vec rounding_mode got_vec.each_with_index ix-> got-> expected = expected_vec.at ix value = values_vec.at ix dp = dps_vec.at ix epsilon = epsilon_vec.at ix - Test.with_clue "round("+value.to_text+", "+dp.to_text+", use_bankers="+use_bankers.to_text+") -> " <| + warning = warning_vec.at ix + Test.with_clue "round("+value.to_text+", "+dp.to_text+", rounding_mode="+rounding_mode.to_text+") -> " <| got.should_equal expected epsilon + if warning.is_nothing.not then + Problems.expect_warning warning got type Batch_Builder Value vector_builder @@ -39,14 +44,14 @@ type Batch_Builder type Check_Instance Value vector_builder value dp - should_equal self (expected : Number) (epsilon:Float=0) = - self.vector_builder.append [self.value, self.dp, expected, epsilon] + should_equal self (expected : Number) (epsilon:Float=0) (with_warning=Nothing) = + self.vector_builder.append [self.value, self.dp, expected, epsilon, with_warning] ## PRIVATE -add_specs group_builder batch_runner run_advanced = +add_specs is_database group_builder batch_runner run_advanced = pending_advanced = if run_advanced.not then "Advanced edge tests are disabled in this run." group_builder.specify "Can round positive decimals correctly" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun 3.0 . should_equal 3 round_fun 3.00001 . should_equal 3 round_fun 3.3 . should_equal 3 @@ -56,7 +61,7 @@ add_specs group_builder batch_runner run_advanced = round_fun 3.99999 . should_equal 4 group_builder.specify "Can round negative decimals correctly" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun -3.0 . should_equal -3 round_fun -3.00001 . should_equal -3 round_fun -3.3 . should_equal -3 @@ -66,20 +71,20 @@ add_specs group_builder batch_runner run_advanced = round_fun -3.99999 . should_equal -4 group_builder.specify "Explicit and implicit 0 decimal places work the same" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun 3.00001 0 . should_equal 3 round_fun 3.3 0 . should_equal 3 round_fun 3.00001 . should_equal 3 round_fun 3.3 . should_equal 3 group_builder.specify "Can round zero and small decimals correctly" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun 0.0 . should_equal 0 round_fun 0.00001 . should_equal 0 round_fun -0.00001 . should_equal 0 group_builder.specify "Can round positive decimals to a specified number of decimal places" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun 3.0001 2 . should_equal 3.0 round_fun 3.1414 2 . should_equal 3.14 round_fun 3.1415 2 . should_equal 3.14 @@ -93,7 +98,7 @@ add_specs group_builder batch_runner run_advanced = round_fun 3.9999 3 . should_equal 4.0 group_builder.specify "Can round negative decimals to a specified number of decimal places" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun -3.0001 2 . should_equal -3.0 round_fun -3.1414 2 . should_equal -3.14 round_fun -3.1415 2 . should_equal -3.14 @@ -107,7 +112,7 @@ add_specs group_builder batch_runner run_advanced = round_fun -3.9999 3 . should_equal -4.0 group_builder.specify "Can round positive decimals to a specified negative number of decimal places" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun 1234.0 -1 . should_equal 1230 round_fun 1234.0 -2 . should_equal 1200 round_fun 1234.0 -3 . should_equal 1000 @@ -123,7 +128,7 @@ add_specs group_builder batch_runner run_advanced = round_fun 1494.0 -2 . should_equal 1500 group_builder.specify "Can round negative decimals to a specified negative number of decimal places" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun -1234.0 -1 . should_equal -1230 round_fun -1234.0 -2 . should_equal -1200 round_fun -1234.0 -3 . should_equal -1000 @@ -139,7 +144,7 @@ add_specs group_builder batch_runner run_advanced = round_fun -1494.0 -2 . should_equal -1500 group_builder.specify "Banker's rounding handles half-way values correctly" pending=pending_advanced <| - batch_runner.run use_bankers=True round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Bankers round_fun-> round_fun -3.5 . should_equal -4 round_fun -2.5 . should_equal -2 round_fun -1.5 . should_equal -2 @@ -160,7 +165,7 @@ add_specs group_builder batch_runner run_advanced = round_fun -12250.0 -2 . should_equal -12200 group_builder.specify "Banker's rounding handles non-half-way values just like normal rounding" pending=pending_advanced <| - batch_runner.run use_bankers=True round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Bankers round_fun-> round_fun 3.0 . should_equal 3 round_fun 3.00001 . should_equal 3 round_fun 3.3 . should_equal 3 @@ -168,7 +173,7 @@ add_specs group_builder batch_runner run_advanced = round_fun 3.50001 . should_equal 4 round_fun 3.99999 . should_equal 4 - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun -3.0 . should_equal -3 round_fun -3.00001 . should_equal -3 round_fun -3.3 . should_equal -3 @@ -177,7 +182,7 @@ add_specs group_builder batch_runner run_advanced = round_fun -3.99999 . should_equal -4 group_builder.specify "Can round small integers to a specified number of decimal places correctly (value is unchanged)" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun 0 . should_equal 0 round_fun 3 . should_equal 3 round_fun -3 . should_equal -3 @@ -187,7 +192,7 @@ add_specs group_builder batch_runner run_advanced = round_fun -3 1 . should_equal -3 group_builder.specify "Can round integers to a specified number of negative places correctly" <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun 0 -1 . should_equal 0 round_fun 4 -1 . should_equal 0 round_fun 5 -1 . should_equal 10 @@ -216,7 +221,7 @@ add_specs group_builder batch_runner run_advanced = round_fun 3101 -3 . should_equal 3000 group_builder.specify "Can round negative integers to a specified number of negative places correctly" pending=pending_advanced <| - batch_runner.run use_bankers=False round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Half_Up round_fun-> round_fun -4 -1 . should_equal 0 round_fun -5 -1 . should_equal -10 round_fun -6 -1 . should_equal -10 @@ -244,7 +249,7 @@ add_specs group_builder batch_runner run_advanced = round_fun -3101 -3 . should_equal -3000 group_builder.specify "Can round negative integers to a specified number of negative places with banker's rounding correctly" pending=pending_advanced <| - batch_runner.run use_bankers=True round_fun-> + batch_runner.run rounding_mode=Rounding_Mode.Bankers round_fun-> round_fun 12300 -2 . should_equal 12300 round_fun 12301 -2 . should_equal 12300 round_fun 12330 -2 . should_equal 12300 @@ -273,7 +278,10 @@ add_specs group_builder batch_runner run_advanced = round_fun -12250 -2 . should_equal -12200 round_fun -12251 -2 . should_equal -12300 + deprecated_argument_pending = if is_database then "https://github.com/enso-org/enso/issues/12787" else Nothing + group_builder.specify "Accepts deprecated use_bankers boolean argument, with a warning" pending=deprecated_argument_pending <| + batch_runner.run rounding_mode=True round_fun-> + round_fun -3.5 . should_equal -4 with_warning=Deprecated + group_builder.specify "Handles incorrect argument types" <| Test.expect_panic_with (batch_runner.run_one 123 "two") Type_Error - Test.expect_panic_with (batch_runner.run_one 123 use_bankers="no") Type_Error - Test.expect_panic_with (batch_runner.run_one 123 use_bankers=0) Type_Error diff --git a/test/Benchmarks/src/Column_Numeric.enso b/test/Benchmarks/src/Column_Numeric.enso index 756b451d511c..ed7c11e8062a 100644 --- a/test/Benchmarks/src/Column_Numeric.enso +++ b/test/Benchmarks/src/Column_Numeric.enso @@ -110,10 +110,10 @@ collect_benches = Bench.build builder-> group_builder.specify "floor_decimals" <| data.decimals.floor - [True, False].each use_bankers-> + [Rounding_Mode.Half_Up, Rounding_Mode.Bankers].each rounding_mode-> [0, -2, 2].map decimal_places-> - name = create_name "round_decimal_places_" decimal_places use_bankers - fun x = x.round decimal_places use_bankers + name = create_name "round_decimal_places_" decimal_places rounding_mode + fun x = x.round decimal_places rounding_mode group_builder.specify ("ints_" + name) <| fun data.ints @@ -133,12 +133,12 @@ collect_benches = Bench.build builder-> ## Creates a valid name for the benchmark -create_name : Text -> Integer -> Boolean -> Text -create_name prefix dec_places use_bankers = - use_bankers_str = "_use_bankers_" + (use_bankers.to_text.to_case Case.Lower) +create_name : Text -> Integer -> Rounding_Mode -> Text +create_name prefix dec_places rounding_mode = + rounding_mode_str = "_rounding_mode_" + (rounding_mode.to_text.to_case Case.Lower) case dec_places >= 0 of - True -> prefix + dec_places.to_text + use_bankers_str - False -> prefix + "minus_" + (dec_places.abs.to_text) + use_bankers_str + True -> prefix + dec_places.to_text + rounding_mode_str + False -> prefix + "minus_" + (dec_places.abs.to_text) + rounding_mode_str main = collect_benches . run_main diff --git a/test/Benchmarks/src/Numeric.enso b/test/Benchmarks/src/Numeric.enso index f8be613eacf1..9b19f4f3dff5 100644 --- a/test/Benchmarks/src/Numeric.enso +++ b/test/Benchmarks/src/Numeric.enso @@ -42,10 +42,10 @@ collect_benches = Bench.build builder-> fun = pair.at 1 group_builder.specify ("integer_" + name) (data.integers.map fun) - [True, False].each use_bankers-> + [Rounding_Mode.Half_Up, Rounding_Mode.Bankers].each rounding_mode-> [0, -2, 2].each decimal_places-> - name = create_name "round_decimal_places_" decimal_places use_bankers - fun = _.round decimal_places use_bankers + name = create_name "round_decimal_places_" decimal_places rounding_mode + fun = _.round decimal_places rounding_mode group_builder.specify ("float_" + name) (data.floats.map fun) group_builder.specify ("integer_" + name) (data.integers.map fun) @@ -62,11 +62,11 @@ collect_benches = Bench.build builder-> ## Creates a valid name for the benchmark create_name : Text -> Integer -> Boolean -> Text -create_name prefix dec_places use_bankers = - use_bankers_str = "_use_bankers_" + (use_bankers.to_text.to_case Case.Lower) +create_name prefix dec_places rounding_mode = + rounding_mode_str = "_rounding_mode_" + (rounding_mode.to_text.to_case Case.Lower) case dec_places >= 0 of - True -> prefix + dec_places.to_text + use_bankers_str - False -> prefix + "minus_" + (dec_places.abs.to_text) + use_bankers_str + True -> prefix + dec_places.to_text + rounding_mode_str + False -> prefix + "minus_" + (dec_places.abs.to_text) + rounding_mode_str main = collect_benches . run_main diff --git a/test/Snowflake_Tests/src/Snowflake_Spec.enso b/test/Snowflake_Tests/src/Snowflake_Spec.enso index 9d4266e9293d..f21f85982240 100644 --- a/test/Snowflake_Tests/src/Snowflake_Spec.enso +++ b/test/Snowflake_Tests/src/Snowflake_Spec.enso @@ -475,9 +475,9 @@ snowflake_specific_spec suite_builder default_connection db_name setup = col . cast Value_Type.Integer . round 1 . value_type . should_equal (Value_Type.Decimal 38 0) col . cast Value_Type.Decimal . round 1 . value_type . should_equal (Value_Type.Decimal 38 1) - col . cast Value_Type.Float . round use_bankers=True . value_type . should_equal Value_Type.Float - col . cast Value_Type.Integer . round use_bankers=True . value_type . should_equal Value_Type.Float - col . cast Value_Type.Decimal . round use_bankers=True . value_type . should_equal Value_Type.Float + col . cast Value_Type.Float . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float + col . cast Value_Type.Integer . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float + col . cast Value_Type.Decimal . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float col . cast Value_Type.Float . ceil . value_type . should_equal Value_Type.Float col . cast Value_Type.Integer . ceil . value_type . should_equal (Value_Type.Decimal 38 0) @@ -496,7 +496,7 @@ snowflake_specific_spec suite_builder default_connection db_name setup = result = table.at "x" |> op result.to_vector.at 0 - do_round n dp=0 use_bankers=False = do_op n (_.round dp use_bankers) + do_round n dp=0 rounding_mode=Rounding_Mode.Half_Up = do_op n (_.round dp rounding_mode) group_builder.specify "Can round correctly near the precision limit" <| do_round 1.2222222222222225 15 . should_equal 1.222222222222223 @@ -505,10 +505,10 @@ snowflake_specific_spec suite_builder default_connection db_name setup = do_round -1.2222222222222235 15 . should_equal -1.222222222222224 group_builder.specify "Can round correctly near the precision limit, using banker's rounding" <| - do_round 1.2222222222222225 15 use_bankers=True . should_equal 1.222222222222222 - do_round -1.2222222222222225 15 use_bankers=True . should_equal -1.222222222222222 - do_round 1.2222222222222235 15 use_bankers=True . should_equal 1.222222222222224 - do_round -1.2222222222222235 15 use_bankers=True . should_equal -1.222222222222224 + do_round 1.2222222222222225 15 rounding_mode=Rounding_Mode.Bankers . should_equal 1.222222222222222 + do_round -1.2222222222222225 15 rounding_mode=Rounding_Mode.Bankers . should_equal -1.222222222222222 + do_round 1.2222222222222235 15 rounding_mode=Rounding_Mode.Bankers . should_equal 1.222222222222224 + do_round -1.2222222222222235 15 rounding_mode=Rounding_Mode.Bankers . should_equal -1.222222222222224 group_builder.specify "Can handle NaN/Infinity" <| ops = [.round, .truncate, .ceil, .floor] 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 0c6491e667e5..cbca618caed1 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 @@ -156,25 +156,25 @@ add_column_operation_specs suite_builder setup = pending_datetime = if (setup.flagged ..Date_Time).not then "Date/Time operations are not supported by this backend." suite_builder.group prefix+"(Column_Operations_Spec) Rounding numeric tests" group_builder-> - round_one n dp=0 use_bankers=False = + round_one n dp=0 rounding_mode=Rounding_Mode.Half_Up = table = light_table_builder [["x", [n]]] - result = table.at "x" . round dp use_bankers + result = table.at "x" . round dp rounding_mode result.to_vector.at 0 - round_batch n_vec dp_vec use_bankers = + round_batch n_vec dp_vec rounding_mode = name_for ix = "x"+ix.to_text n_columns = n_vec.map_with_index ix-> n-> [name_for ix, [n]] table = light_table_builder n_columns new_columns = dp_vec.map_with_index ix-> dp-> n_col = table.at (name_for ix) - n_col.round dp use_bankers . rename (name_for ix) + n_col.round dp rounding_mode . rename (name_for ix) # In new table input columns are replaced with results of output new_table = new_columns.fold table acc-> col-> acc.set col (set_mode=..Update) # We read all operations in a single query materialized = new_table.read Runtime.assert (materialized.row_count == 1) materialized.first_row.to_vector - Round_Spec.add_specs group_builder (Round_Spec.Batch_Runner.Value round_batch round_one) run_advanced=setup.test_selection.run_advanced_edge_case_tests + Round_Spec.add_specs True group_builder (Round_Spec.Batch_Runner.Value round_batch round_one) run_advanced=setup.test_selection.run_advanced_edge_case_tests group_builder.specify "Decimal places out of range" <| 3.1 . round 16 . should_fail_with Illegal_Argument @@ -188,8 +188,8 @@ add_column_operation_specs suite_builder setup = + [[-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) + round_batch (cases_transposed.at 0) (cases_transposed.at 1) rounding_mode=Rounding_Mode.Half_Up . should_equal (cases_transposed.at 2) + round_batch (bankers_cases_transposed.at 0) (bankers_cases_transposed.at 1) rounding_mode=Rounding_Mode.Bankers . 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 diff --git a/test/Table_Tests/src/Database/Common/IR_Spec.enso b/test/Table_Tests/src/Database/Common/IR_Spec.enso index 1b51c4278ab3..079fbc71e74a 100644 --- a/test/Table_Tests/src/Database/Common/IR_Spec.enso +++ b/test/Table_Tests/src/Database/Common/IR_Spec.enso @@ -57,13 +57,13 @@ add_specs (suite_builder : Suite_Builder) setup (prefix : Text) default_connecti group_builder.specify "should be able to traverse a Query to reconstruct it" <| col = data.t1 . at "a" - a = col.round * col.round use_bankers=True + a = col.round * col.round rounding_mode=Rounding_Mode.Bankers a_query = a.to_table.to_select_query a_query.traverse (x->x) . should_equal a_query group_builder.specify "should be able to traverse a Query to count nodes" <| col = data.t1 . at "a" - a = col.round * col.round use_bankers=True + a = col.round * col.round rounding_mode=Rounding_Mode.Bankers a_sub = (a . rename "p" . to_table . as_subquery . at "p") b = a_sub * a_sub b_sub = (b . rename "p" . to_table . as_subquery . at "p") @@ -85,8 +85,8 @@ add_specs (suite_builder : Suite_Builder) setup (prefix : Text) default_connecti ## Complex query that optionally enables let bindings at each step big = wm b0 (x * y) xty-> wm b1 (x + y + 10) xpyp10-> - wm b2 (xty.round 1 use_bankers=True) r0-> - wm b3 (xpyp10.round use_bankers=True) r1-> + wm b2 (xty.round 1 rounding_mode=Rounding_Mode.Bankers) r0-> + wm b3 (xpyp10.round rounding_mode=Rounding_Mode.Bankers) r1-> r0 + r1 big.to_vector results.flatten.flatten.flatten.distinct.length . should_equal 1 diff --git a/test/Table_Tests/src/Database/Postgres_Spec.enso b/test/Table_Tests/src/Database/Postgres_Spec.enso index dbb061caf417..799c6e43e648 100644 --- a/test/Table_Tests/src/Database/Postgres_Spec.enso +++ b/test/Table_Tests/src/Database/Postgres_Spec.enso @@ -646,9 +646,9 @@ postgres_specific_spec suite_builder create_connection_fn db_name setup = col . cast Value_Type.Integer . round 1 . value_type . should_equal Value_Type.Decimal col . cast Value_Type.Decimal . round 1 . value_type . should_equal Value_Type.Decimal - col . cast Value_Type.Float . round use_bankers=True . value_type . should_equal Value_Type.Float - col . cast Value_Type.Integer . round use_bankers=True . value_type . should_equal Value_Type.Float - col . cast Value_Type.Decimal . round use_bankers=True . value_type . should_equal Value_Type.Decimal + col . cast Value_Type.Float . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float + col . cast Value_Type.Integer . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float + col . cast Value_Type.Decimal . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Decimal col . cast Value_Type.Float . ceil . value_type . should_equal Value_Type.Float col . cast Value_Type.Integer . ceil . value_type . should_equal Value_Type.Float @@ -667,7 +667,7 @@ postgres_specific_spec suite_builder create_connection_fn db_name setup = result = table.at "x" |> op result.to_vector.at 0 - do_round data n dp=0 use_bankers=False = do_op data n (_.round dp use_bankers) + do_round data n dp=0 rounding_mode=Rounding_Mode.Half_Up = do_op data n (_.round dp rounding_mode) ## column expressions group_builder.specify "Can round correctly near the precision limit" <| @@ -678,10 +678,10 @@ postgres_specific_spec suite_builder create_connection_fn db_name setup = ## column expressions group_builder.specify "Can round correctly near the precision limit, using banker's rounding" <| - do_round data 1.2222222222222225 15 use_bankers=True . should_equal 1.222222222222222 - do_round data -1.2222222222222225 15 use_bankers=True . should_equal -1.222222222222222 - do_round data 1.2222222222222235 15 use_bankers=True . should_equal 1.222222222222224 - do_round data -1.2222222222222235 15 use_bankers=True . should_equal -1.222222222222224 + do_round data 1.2222222222222225 15 rounding_mode=Rounding_Mode.Bankers . should_equal 1.222222222222222 + do_round data -1.2222222222222225 15 rounding_mode=Rounding_Mode.Bankers . should_equal -1.222222222222222 + do_round data 1.2222222222222235 15 rounding_mode=Rounding_Mode.Bankers . should_equal 1.222222222222224 + do_round data -1.2222222222222235 15 rounding_mode=Rounding_Mode.Bankers . should_equal -1.222222222222224 ## input/output group_builder.specify "Can handle NaN/Infinity" <| diff --git a/test/Table_Tests/src/Database/SQLite_Spec.enso b/test/Table_Tests/src/Database/SQLite_Spec.enso index 61db0a520a8d..6394ad69fa81 100644 --- a/test/Table_Tests/src/Database/SQLite_Spec.enso +++ b/test/Table_Tests/src/Database/SQLite_Spec.enso @@ -246,9 +246,9 @@ sqlite_specific_spec suite_builder prefix create_connection_func setup = col . cast Value_Type.Integer . round 1 . value_type . should_equal Value_Type.Float col . cast Value_Type.Decimal . round 1 . value_type . should_equal Value_Type.Float - col . cast Value_Type.Float . round use_bankers=True . value_type . should_equal Value_Type.Float - col . cast Value_Type.Integer . round use_bankers=True . value_type . should_equal Value_Type.Float - col . cast Value_Type.Decimal . round use_bankers=True . value_type . should_equal Value_Type.Float + col . cast Value_Type.Float . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float + col . cast Value_Type.Integer . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float + col . cast Value_Type.Decimal . round rounding_mode=Rounding_Mode.Bankers . value_type . should_equal Value_Type.Float col . cast Value_Type.Float . ceil . value_type . should_equal Value_Type.Float col . cast Value_Type.Integer . ceil . value_type . should_equal Value_Type.Integer @@ -267,7 +267,7 @@ sqlite_specific_spec suite_builder prefix create_connection_func setup = result = table.at "x" |> op result.to_vector.at 0 - do_round data n dp=0 use_bankers=False = do_op data n (_.round dp use_bankers) + do_round data n dp=0 rounding_mode=Rounding_Mode.Half_Up = do_op data n (_.round dp rounding_mode) group_builder.specify "Can round correctly near the precision limit" <| # This value varies depending on the version of SQLite. @@ -277,10 +277,10 @@ sqlite_specific_spec suite_builder prefix create_connection_func setup = do_round data -1.2222222222222235 15 . should_equal -1.222222222222223 group_builder.specify "Can round correctly near the precision limit, using banker's rounding" <| - do_round data 1.2222222222222225 15 use_bankers=True . should_equal 1.222222222222222 - do_round data -1.2222222222222225 15 use_bankers=True . should_equal -1.222222222222222 - do_round data 1.2222222222222235 15 use_bankers=True . should_equal 1.222222222222224 - do_round data -1.2222222222222235 15 use_bankers=True . should_equal -1.222222222222224 + do_round data 1.2222222222222225 15 rounding_mode=Rounding_Mode.Bankers . should_equal 1.222222222222222 + do_round data -1.2222222222222225 15 rounding_mode=Rounding_Mode.Bankers . should_equal -1.222222222222222 + do_round data 1.2222222222222235 15 rounding_mode=Rounding_Mode.Bankers . should_equal 1.222222222222224 + do_round data -1.2222222222222235 15 rounding_mode=Rounding_Mode.Bankers . should_equal -1.222222222222224 group_builder.specify "Can handle NaN/Infinity" <| ## SQLite does not support NaN diff --git a/test/Table_Tests/src/In_Memory/Column_Spec.enso b/test/Table_Tests/src/In_Memory/Column_Spec.enso index c023c48ee73d..8d886c4ec724 100644 --- a/test/Table_Tests/src/In_Memory/Column_Spec.enso +++ b/test/Table_Tests/src/In_Memory/Column_Spec.enso @@ -311,7 +311,7 @@ add_specs suite_builder = Column.from_vector "foo" [1.2, 2.3, 2.5, 3.6] . round . should_equal (Column.from_vector "round([foo])" [1, 2, 3, 4]) Column.from_vector "foo" [1.25, 2.33, 3.57] . round 1 . should_equal <| Column.from_vector "round([foo])" [1.3, 2.3, 3.6] Column.from_vector "foo" [12.0, 24.0, 25.0, 29.0] . round -1 . should_equal <| Column.from_vector "round([foo])" [10, 20, 30, 30] - Column.from_vector "foo" [1.5, 2.5, 3.5] . round use_bankers=True . should_equal <| Column.from_vector "round([foo])" [2, 2, 4] + Column.from_vector "foo" [1.5, 2.5, 3.5] . round rounding_mode=Rounding_Mode.Bankers . should_equal <| Column.from_vector "round([foo])" [2, 2, 4] Column.from_vector "foo" [-1.2, -2.3, -2.5, -3.6] . round . should_equal (Column.from_vector "round([foo])" [-1, -2, -3, -4]) Column.from_vector "foo" [-1.25, -2.33, -2.45, -3.57] . round 1 . should_equal <| Column.from_vector "round([foo])" [-1.3, -2.3, -2.5, -3.6] @@ -326,7 +326,7 @@ add_specs suite_builder = group_builder.specify "should be able to round a column of integers" <| Column.from_vector "foo" [12, 24, 25, 29] . round . should_equal <| Column.from_vector "round([foo])" [12, 24, 25, 29] Column.from_vector "foo" [12, 24, 25, 29] . round -1 . should_equal <| Column.from_vector "round([foo])" [10, 20, 30, 30] - Column.from_vector "foo" [15, 25, 35] . round -1 use_bankers=True . should_equal <| Column.from_vector "round([foo])" [20, 20, 40] + Column.from_vector "foo" [15, 25, 35] . round -1 rounding_mode=Rounding_Mode.Bankers . should_equal <| Column.from_vector "round([foo])" [20, 20, 40] group_builder.specify "integer rounding should return the correct column type" <| col = Column.from_vector "foo" [12, 24, 25, 29] @@ -350,7 +350,7 @@ add_specs suite_builder = group_builder.specify "should handle type errors" <| col = Column.from_vector "foo" [12, 23, 45] - Test.expect_panic_with (col.round use_bankers="string") Type_Error + Test.expect_panic_with (col.round rounding_mode="string") Type_Error Test.expect_panic_with (col.round decimal_places="string") Type_Error suite_builder.group "truncate" group_builder-> @@ -394,7 +394,7 @@ add_specs suite_builder = col = Column.from_vector "x" [n] result = op col result.to_vector.at 0 - do_round n dp=0 use_bankers=False = do_op n (_.round dp use_bankers) + do_round n dp=0 rounding_mode=Rounding_Mode.Half_Up = do_op n (_.round dp rounding_mode) group_builder.specify "round returns the correct type" <| do_round 231.2 1 . should_be_a Float