Add Decimal.min and .max (#9663)

* min and max

* changelog

* wip

* scale approach mostly works

* Revert "scale approach mostly works"

This reverts commit 88e6073f7a.

* review

* review

* return value type
This commit is contained in:
GregoryTravis 2024-04-11 09:58:10 -04:00 committed by GitHub
parent 9ac85b93b7
commit 676e989f7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 111 additions and 0 deletions

View File

@ -647,6 +647,7 @@
- [Made `Integer.%` consistent across all `Integer` values.][9589]
- [Added `Decimal.parse` and `.format`.][9637]
- [Added `Decimal.abs`, `.negate` and `.signum`.][9641]
- [Added `Decimal.min` and `.max`.][9663]
[debug-shortcuts]:
https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug
@ -944,6 +945,7 @@
[9589]: https://github.com/enso-org/enso/pull/9589
[9637]: https://github.com/enso-org/enso/pull/9637
[9641]: https://github.com/enso-org/enso/pull/9641
[9663]: https://github.com/enso-org/enso/pull/9663
#### Enso Compiler

View File

@ -706,6 +706,36 @@ type Decimal
signum : Integer
signum self -> Integer = self.big_decimal.signum
## GROUP Math
ICON sigma
Returns the smaller value of `self` and `that`.
Arguments:
- that: The number to compare `self` against.
> Example
Find the minimum of 12 and 13.
Decimal.new "12" . min (Decimal.new "13")
# => Decimal.new "12"
min : Decimal -> Decimal
min self (that : Decimal) -> Decimal = if self < that then self else that
## GROUP Math
ICON math
Returns the larger value of `self` and `that`.
Arguments:
- that: The number to compare `self` against.
> Example
Find the maximum of 12 and 13.
Decimal.new "12" . max (Decimal.new "13")
# => Decimal.new "13"
max : Decimal -> Decimal
max self (that : Decimal) -> Decimal = if self > that then self else that
## GROUP CONVERSIONS
Convert this to an `Integer`.

View File

@ -708,6 +708,85 @@ add_specs suite_builder =
Decimal.new "-12.345E97" . signum . should_equal -1
Decimal.new "0" . signum . should_equal 0
suite_builder.group "min/max" group_builder->
group_builder.specify "should calculate min and max correctly" <|
Decimal.new "12" . min (Decimal.new "13") . should_equal (Decimal.new "12")
Decimal.new "12" . min (Decimal.new "11") . should_equal (Decimal.new "11")
Decimal.new "-12" . min (Decimal.new "-13") . should_equal (Decimal.new "-13")
Decimal.new "-12" . min (Decimal.new "-11") . should_equal (Decimal.new "-12")
Decimal.new "12.1" . min (Decimal.new "12.3") . should_equal (Decimal.new "12.1")
Decimal.new "12.2" . min (Decimal.new "11.7") . should_equal (Decimal.new "11.7")
Decimal.new "-12.1" . min (Decimal.new "-12.3") . should_equal (Decimal.new "-12.3")
Decimal.new "-12.2" . min (Decimal.new "-11.7") . should_equal (Decimal.new "-12.2")
Decimal.new "0" . min (Decimal.new "-1") . should_equal (Decimal.new "-1")
Decimal.new "0" . min (Decimal.new "1") . should_equal (Decimal.new "0")
Decimal.new "-1" . min (Decimal.new "0") . should_equal (Decimal.new "-1")
Decimal.new "1" . min (Decimal.new "0") . should_equal (Decimal.new "0")
Decimal.new "12E73" . min (Decimal.new "13E60") . should_equal (Decimal.new "13E60")
Decimal.new "12E73" . min (Decimal.new "13E80") . should_equal (Decimal.new "12E73")
Decimal.new "-12E73" . min (Decimal.new "-13E60") . should_equal (Decimal.new "-12E73")
Decimal.new "-12E73" . min (Decimal.new "-13E80") . should_equal (Decimal.new "-13E80")
Decimal.new "12" . max (Decimal.new "13") . should_equal (Decimal.new "13")
Decimal.new "12" . max (Decimal.new "11") . should_equal (Decimal.new "12")
Decimal.new "-12" . max (Decimal.new "-13") . should_equal (Decimal.new "-12")
Decimal.new "-12" . max (Decimal.new "-11") . should_equal (Decimal.new "-11")
Decimal.new "12.1" . max (Decimal.new "12.3") . should_equal (Decimal.new "12.3")
Decimal.new "12.2" . max (Decimal.new "11.7") . should_equal (Decimal.new "12.2")
Decimal.new "-12.1" . max (Decimal.new "-12.3") . should_equal (Decimal.new "-12.1")
Decimal.new "-12.2" . max (Decimal.new "-11.7") . should_equal (Decimal.new "-11.7")
Decimal.new "0" . max (Decimal.new "-1") . should_equal (Decimal.new "0")
Decimal.new "0" . max (Decimal.new "1") . should_equal (Decimal.new "1")
Decimal.new "-1" . max (Decimal.new "0") . should_equal (Decimal.new "0")
Decimal.new "1" . max (Decimal.new "0") . should_equal (Decimal.new "1")
Decimal.new "12E73" . max (Decimal.new "13E60") . should_equal (Decimal.new "12E73")
Decimal.new "12E73" . max (Decimal.new "13E80") . should_equal (Decimal.new "13E80")
Decimal.new "-12E73" . max (Decimal.new "-13E60") . should_equal (Decimal.new "-13E60")
Decimal.new "-12E73" . max (Decimal.new "-13E80") . should_equal (Decimal.new "-12E73")
Decimal.new "12" . min (Decimal.new "12.3") . should_equal (Decimal.new "12")
Decimal.new "12.2" . min (Decimal.new "11") . should_equal (Decimal.new "11")
Decimal.new "-12" . min (Decimal.new "-12.3") . should_equal (Decimal.new "-12.3")
Decimal.new "-12.2" . min (Decimal.new "-11") . should_equal (Decimal.new "-12.2")
group_builder.specify "should calculate min and max correctly, with mixed Decimal and Int/Float" <|
Decimal.new "12" . min 13 . should_equal (Decimal.new "12")
12 . min (Decimal.new "13") . should_equal (Decimal.new "12")
Decimal.new "12.1" . min 12.3 . should_equal (Decimal.new "12.1")
12.1 . min (Decimal.new "12.3") . should_equal (Decimal.new "12.1")
Decimal.new "0" . min (Decimal.new "-1") . should_equal (Decimal.new "-1")
0 . min (Decimal.new "-1") . should_equal (Decimal.new "-1")
Decimal.new "12" . max 13 . should_equal (Decimal.new "13")
12 . max (Decimal.new "13") . should_equal (Decimal.new "13")
Decimal.new "12.1" . max 12.3 . should_equal (Decimal.new "12.3")
12.1 . max (Decimal.new "12.3") . should_equal (Decimal.new "12.3")
Decimal.new "0" . max -1 . should_equal (Decimal.new "0")
0 . max (Decimal.new "-1") . should_equal (Decimal.new "0")
Decimal.new "12" . min 12.3 . should_equal (Decimal.new "12")
12 . min (Decimal.new "12.3") . should_equal (Decimal.new "12")
Decimal.new "12" . max 12.3 . should_equal (Decimal.new "12.3")
12 . max (Decimal.new "12.3") . should_equal (Decimal.new "12.3")
group_builder.specify "should calculate min and max correctly, with Math.min/max" <|
Math.min (Decimal.new "12") (Decimal.new "13") . should_equal (Decimal.new "12")
Math.max (Decimal.new "12") (Decimal.new "13") . should_equal (Decimal.new "13")
Math.min (Decimal.new "12E70") (Decimal.new "13E70") . should_equal (Decimal.new "12E70")
Math.max (Decimal.new "12E70") (Decimal.new "13E70") . should_equal (Decimal.new "13E70")
main =
suite = Test.build suite_builder->
add_specs suite_builder