Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer.to_decimal and Float.to_decimal #9716

Merged
merged 9 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@
- [Added `Decimal.min` and `.max`.][9663]
- [Added `Decimal.round`.][9672]
- [Implemented write support for Enso Cloud files.][9686]
- [Added `Integer.to_decimal` and `Float.to_decimal`.][9716]

[debug-shortcuts]:
https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug
Expand Down Expand Up @@ -950,6 +951,7 @@
[9663]: https://github.com/enso-org/enso/pull/9663
[9672]: https://github.com/enso-org/enso/pull/9672
[9686]: https://github.com/enso-org/enso/pull/9686
[9716]: https://github.com/enso-org/enso/pull/9716

#### Enso Compiler

Expand Down
23 changes: 23 additions & 0 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import project.Any.Any
import project.Data.Decimal.Decimal
import project.Data.Locale.Locale
import project.Data.Ordering.Comparable
import project.Data.Text.Text
Expand Down Expand Up @@ -731,6 +732,17 @@ type Float
5.0.to_float
to_float self -> Float = self

## ICON convert
Convert this to a `Decimal`.

> Example
Convert 12.3 to a Decimal.

12.3 . to_decimal
# => Decimal.new "12.3"
to_decimal : Decimal
to_decimal self -> Decimal = Decimal.from_float self

## ALIAS from text
GROUP Conversions
ICON convert
Expand Down Expand Up @@ -1080,6 +1092,17 @@ type Integer
5.to_float
to_float self -> Float = integer_to_float self

## ICON convert
Convert this to a `Decimal`.

> Example
Convert 12 to a Decimal.

12 . to_decimal
# => Decimal.new "12"
to_decimal : Decimal
to_decimal self -> Decimal = Decimal.from_integer self

## GROUP Bitwise
ICON math
Computes the bitwise and (conjunction) operation between this and
Expand Down
12 changes: 12 additions & 0 deletions test/Base_Tests/src/Data/Decimal_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,18 @@ add_specs suite_builder =
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")

suite_builder.group "Integer/Float .to_decimal" group_builder->
group_builder.specify ".to_decimal should convert to Decimal" <|
12 . to_decimal . should_be_a Decimal
12.3 . to_decimal . should_be_a Decimal

group_builder.specify "Float.to_decimal should attach a warning" <|
Problems.expect_warning Loss_Of_Numeric_Precision (12.3 . to_decimal)
Comment on lines +951 to +952
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if the explicit conversion should still have the warning? I guess if you do it explicitly maybe it's not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdunkerley has said that because people are often unfamiliar with the fact that Float is approximate (for example, not being able to represent 0.1 exactly), that we need these warnings always. But I think it's something we can discuss.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that's fair enough


group_builder.specify "examples" <|
12 . to_decimal . should_equal (Decimal.new "12")
12.3 . to_decimal . should_equal (Decimal.new "12.3")

main =
suite = Test.build suite_builder->
add_specs suite_builder
Expand Down
Loading