Skip to content

Commit d5ee5e8

Browse files
Use Rounding_Mode instead of use_bankers flag (#12641)
1 parent 569e52e commit d5ee5e8

File tree

28 files changed

+196
-169
lines changed

28 files changed

+196
-169
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
- [Changed in-memory `Column.zip` to match `Table.zip`, removing the `function`
141141
parameter][12626]
142142
- [Added Regex_match for Postgres][12663]
143+
- [Replace `use_bankers` flag with `Rounding_Mode` in all `round`
144+
methods][12641]
143145

144146
[11926]: https://github.com/enso-org/enso/pull/11926
145147
[12031]: https://github.com/enso-org/enso/pull/12031
@@ -163,6 +165,7 @@
163165
[12590]: https://github.com/enso-org/enso/pull/12590
164166
[12626]: https://github.com/enso-org/enso/pull/12626
165167
[12663]: https://github.com/enso-org/enso/pull/12663
168+
[12641]: https://github.com/enso-org/enso/pull/12641
166169

167170
#### Enso Language & Runtime
168171

distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Decimal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
- pow self exp:Standard.Base.Data.Numbers.Integer -> Standard.Base.Data.Decimal.Decimal
3232
- precision self -> Standard.Base.Data.Numbers.Integer
3333
- remainder self that:Standard.Base.Data.Decimal.Decimal -> Standard.Base.Data.Decimal.Decimal
34-
- round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Data.Decimal.Decimal
34+
- round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Data.Decimal.Decimal
3535
- scale self -> Standard.Base.Data.Numbers.Integer
3636
- set_scale self new_scale:Standard.Base.Data.Numbers.Integer -> Standard.Base.Data.Decimal.Decimal
3737
- signum self -> Standard.Base.Data.Numbers.Integer

distribution/lib/Standard/Base/0.0.0-dev/docs/api/Data/Numbers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- min_value -> Standard.Base.Any.Any
1919
- negate self -> Standard.Base.Data.Numbers.Float
2020
- 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
21-
- round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any
21+
- round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Data.Numbers.Float
2222
- to_decimal self -> Standard.Base.Data.Decimal.Decimal
2323
- to_float self -> Standard.Base.Data.Numbers.Float
2424
- truncate self -> Standard.Base.Data.Numbers.Integer
@@ -47,7 +47,7 @@
4747
- floor self -> Standard.Base.Data.Numbers.Integer
4848
- negate self -> Standard.Base.Data.Numbers.Integer
4949
- parse text:Standard.Base.Data.Text.Text radix:Standard.Base.Data.Numbers.Integer= -> Standard.Base.Any.Any
50-
- round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any
50+
- round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Any.Any
5151
- to_decimal self -> Standard.Base.Data.Decimal.Decimal
5252
- to_float self -> Standard.Base.Data.Numbers.Float
5353
- truncate self -> Standard.Base.Data.Numbers.Integer
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Enso Signatures 1.0
22
## module Standard.Base.Data.Numeric.Rounding_Mode
33
- type Rounding_Mode
4-
- Value rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.RoundingMode
5-
- bankers -> Standard.Base.Any.Any
6-
- half_up -> Standard.Base.Any.Any
4+
- Bankers
5+
- Half_Up
6+
- Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode.from that:Standard.Base.Data.Boolean.Boolean -> Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode

distribution/lib/Standard/Base/0.0.0-dev/src/Data/Decimal.enso

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -991,15 +991,16 @@ type Decimal
991991
Round to a specified number of decimal places.
992992

993993
By default, rounding uses "symmetric round-half-up", also known as
994-
"half-up." If use_bankers=True, then it uses "round-half-even", also
994+
"half-up." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also
995995
known as "banker's rounding".
996996

997997
Arguments:
998998
- decimal_places: The number of decimal places to round to. Can be
999999
negative, which results in rounding to positive integer powers of 10.
10001000
Must be between Java `Integer.MIN_VALUE` and `Integer.MAX_VALUE`
10011001
(-2147483648 and 2147483647) (inclusive).
1002-
- use_bankers: Rounds mid-point to nearest even number.
1002+
- rounding_mode: specifies how to break ties for the least significant
1003+
digit.
10031004

10041005
! Error Conditions
10051006

@@ -1032,12 +1033,12 @@ type Decimal
10321033
> Example
10331034
Use Banker's Rounding.
10341035

1035-
Decimal.new "2.5" . round use_bankers=True
1036+
Decimal.new "2.5" . round rounding_mode=Rounding_Mode.Bankers
10361037
# => 2
1037-
round : Integer -> Boolean -> Decimal
1038-
round self (decimal_places:Integer=0) (use_bankers:Boolean=False) -> Decimal =
1038+
round : Integer -> Rounding_Mode -> Decimal
1039+
round self (decimal_places:Integer=0) (rounding_mode : Rounding_Mode = ..Half_Up) -> Decimal =
10391040
out_of_range = decimal_places > Java_Integer.MAX_VALUE || decimal_places < Java_Integer.MIN_VALUE
1040-
if out_of_range.not then Decimal.Value (Decimal_Utils.round self.big_decimal decimal_places use_bankers) else
1041+
if out_of_range.not then Decimal.Value (Decimal_Utils.round self.big_decimal decimal_places (rounding_mode == Rounding_Mode.Bankers)) else
10411042
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
10421043
Error.throw (Out_Of_Range.Error decimal_places message)
10431044

distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import project.Any.Any
22
import project.Data.Decimal.Decimal
33
import project.Data.Locale.Locale
4+
import project.Data.Numeric.Rounding_Mode.Rounding_Mode
45
import project.Data.Ordering.Comparable
56
import project.Data.Text.Text
67
import project.Error.Error
@@ -659,7 +660,7 @@ type Float
659660
Round to a specified number of decimal places.
660661

661662
By default, rounding uses "symmetric round-half-up", also known as
662-
"half-up." If use_bankers=True, then it uses "round-half-even", also
663+
"half-up." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also
663664
known as "banker's rounding".
664665

665666
If `decimal_places` > 0, `round` returns a `Float`; otherwise, it
@@ -672,7 +673,8 @@ type Float
672673
- decimal_places: The number of decimal places to round to. Can be
673674
negative, which results in rounding to positive integer powers of 10.
674675
Must be between -15 and 15 (inclusive).
675-
- use_bankers: Rounds mid-point to nearest even number.
676+
- rounding_mode: specifies how to break ties for the least significant
677+
digit.
676678

677679
! Error Conditions
678680

@@ -705,12 +707,12 @@ type Float
705707
> Example
706708
Use Banker's Rounding.
707709

708-
2.5 . round use_bankers=True == 2
709-
round : Integer -> Boolean -> Integer | Float ! Illegal_Argument
710-
round self (decimal_places:Integer=0) (use_bankers:Boolean=False) =
710+
2.5 . round rounding_mode=Rounding_Mode.Bankers == 2
711+
round : Integer -> Rounding_Mode -> Integer | Float ! Illegal_Argument
712+
round self (decimal_places:Integer=0) (rounding_mode : Rounding_Mode = ..Half_Up) -> Float =
711713
report_unsupported cp = Error.throw (Illegal_Argument.Error cp.payload.message)
712714
Panic.catch Unsupported_Argument_Types handler=report_unsupported
713-
round_float_builtin self decimal_places use_bankers
715+
round_float_builtin self decimal_places (rounding_mode == Rounding_Mode.Bankers)
714716

715717
## GROUP Operators
716718
ICON operators
@@ -1074,14 +1076,15 @@ type Integer
10741076
argument. For negative decimal places, see below.
10751077

10761078
By default, rounding uses "symmetric round-half-up", also known as
1077-
"half-up." If use_bankers=True, then it uses "round-half-even", also
1079+
"half-up." If rounding_mode=Rounding_Mode.Bankers, then it uses "round-half-even", also
10781080
known as "banker's rounding".
10791081

10801082
Arguments:
10811083
- decimal_places: The number of decimal places to round to. Can be
10821084
negative, which results in rounding to positive integer powers of 10.
10831085
Must be between -15 and 15 (inclusive).
1084-
- use_bankers: Rounds mid-point to nearest even number.
1086+
- rounding_mode: specifies how to break ties for the least significant
1087+
digit.
10851088

10861089
! Error Conditions
10871090
If `self` is outside the range -99999999999999..99999999999999
@@ -1108,14 +1111,14 @@ type Integer
11081111
> Example
11091112
Round to the nearest hundred, using Banker's Rounding.
11101113

1111-
12250 . round -2 use_bankers=True == 12200
1112-
round self (decimal_places:Integer=0) (use_bankers:Boolean=False) -> Integer ! Illegal_Argument =
1114+
12250 . round -2 rounding_mode=Rounding_Mode.Bankers == 12200
1115+
round self (decimal_places:Integer=0) (rounding_mode : Rounding_Mode = ..Half_Up) -> Integer ! Illegal_Argument =
11131116
## We reject values outside the range of `long` here, so we don't also
11141117
do this check in the Java.
11151118
Rounding_Helpers.check_round_input self <|
11161119
report_unsupported cp = Error.throw (Illegal_Argument.Error cp.payload.message)
11171120
Panic.catch Unsupported_Argument_Types handler=report_unsupported
1118-
round_integer_builtin self decimal_places use_bankers
1121+
round_integer_builtin self decimal_places (rounding_mode == Rounding_Mode.Bankers)
11191122

11201123
## GROUP Operators
11211124
ICON operators

distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numeric/Math_Context.enso

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Math_Context
4242
specifies exact operations.
4343
- rounding_mode: The rounding mode to use.
4444
new : Integer | Unlimited -> Rounding_Mode -> Math_Context
45-
new (precision : Integer | Unlimited) (rounding_mode : Rounding_Mode = Rounding_Mode.half_up) =
45+
new (precision : Integer | Unlimited) (rounding_mode : Rounding_Mode = Rounding_Mode.Half_Up) =
4646
case precision of
47-
_ : Integer -> Math_Context.Value (MathContext.new precision rounding_mode.rounding_mode)
48-
_ : Unlimited -> Math_Context.Value (MathContext.new 0 rounding_mode.rounding_mode)
47+
_ : Integer -> Math_Context.Value (MathContext.new precision rounding_mode.to_java_rounding_mode)
48+
_ : Unlimited -> Math_Context.Value (MathContext.new 0 rounding_mode.to_java_rounding_mode)
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from Standard.Base import all
2+
import project.Errors.Deprecated.Deprecated
3+
14
polyglot java import java.math.RoundingMode
25

36
## A wrapper around the Java
@@ -7,17 +10,24 @@ polyglot java import java.math.RoundingMode
710
`Rounding_Mode` is used to specify the method to use for rounding a value in
811
operations on `Decimal`s.` See `Decimal` for more details.
912
type Rounding_Mode
10-
## PRIVATE
11-
Value (rounding_mode : RoundingMode)
12-
1313
## ICON math
1414
Round towards positive infinity for positive numbers, and negative
1515
infinity for negative numbers. (This is the default.)
16-
half_up : Rounding_Mode
17-
half_up = Rounding_Mode.Value RoundingMode.HALF_UP
16+
Half_Up
1817

1918
## ICON math
2019
Rownd towards the nearest neighbor, with ties broken by rounding towards
2120
the nearest even neighbor.
22-
bankers : Rounding_Mode
23-
bankers = Rounding_Mode.Value RoundingMode.HALF_EVEN
21+
Bankers
22+
23+
private to_java_rounding_mode self -> RoundingMode =
24+
case self of
25+
Rounding_Mode.Half_Up -> RoundingMode.HALF_UP
26+
Rounding_Mode.Bankers -> RoundingMode.HALF_EVEN
27+
28+
## PRIVATE
29+
Convert deprecated `use_bankers : Boolean` arguments (passed as
30+
`rounding_mode`) to `Rounding_Mode`.
31+
Rounding_Mode.from (that:Boolean) =
32+
rounding_mode_resolved = if that then Rounding_Mode.Bankers else Rounding_Mode.Half_Up
33+
Warning.attach (Deprecated.Warning "Standard.Base.Data.Numeric" "Rounding_Mode" "use_bankers:Boolean is deprecated, use Rounding_Mode instead") rounding_mode_resolved

distribution/lib/Standard/Database/0.0.0-dev/docs/api/DB_Column.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@
7777
- read self max_rows:Standard.Table.Rows_To_Read.Rows_To_Read= -> Standard.Table.Column.Column
7878
- 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
7979
- rename self name:Standard.Base.Data.Text.Text -> Standard.Base.Any.Any
80-
- round self decimal_places:Standard.Base.Data.Numbers.Integer= use_bankers:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any
81-
- round_builtin self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any
82-
- round_decimal self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any
83-
- round_float self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any
84-
- round_integer self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any
80+
- round self decimal_places:Standard.Base.Data.Numbers.Integer= rounding_mode:Standard.Base.Data.Numeric.Rounding_Mode.Rounding_Mode= -> Standard.Base.Any.Any
81+
- round_builtin self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any
82+
- round_decimal self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any
83+
- round_float self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any
84+
- round_integer self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any
8585
- second self -> Standard.Base.Any.Any
8686
- short_circuit_special_floating_point self exp:Standard.Base.Any.Any -> Standard.Base.Any.Any
8787
- should_be_selected_by_type self value_type:Standard.Table.Value_Type.Value_Type -> Standard.Base.Data.Boolean.Boolean
88-
- should_use_builtin_round self decimal_places:Standard.Base.Any.Any use_bankers:Standard.Base.Any.Any -> Standard.Base.Any.Any
88+
- should_use_builtin_round self decimal_places:Standard.Base.Any.Any rounding_mode:Standard.Base.Any.Any -> Standard.Base.Any.Any
8989
- sort self order:Standard.Base.Data.Sort_Direction.Sort_Direction= -> Standard.Database.DB_Column.DB_Column
9090
- sql_type self -> Standard.Base.Any.Any
9191
- 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

0 commit comments

Comments
 (0)