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

Various Decimal usability tweaks #10517

Merged
merged 19 commits into from
Jul 16, 2024
Merged

Various Decimal usability tweaks #10517

merged 19 commits into from
Jul 16, 2024

Conversation

GregoryTravis
Copy link
Contributor

@GregoryTravis GregoryTravis commented Jul 10, 2024

  • Rename Decimal.from_string to Decimal.from_text.
  • Add a dec function to top level namespace
  • Integers are automatically converted to decimals when used in decimal operations
  • Arithmetic Decimal operations between a decimal and a float should throw an error
    Equality
  • Should promote the float to a decimal and attach a warning (saying best to use decimals
  • Comparison Perform the comparison between the decimal and the float; Attach same warning as equality.

Checklist

Please ensure that the following checklist has been satisfied before submitting the PR:

  • The documentation has been updated, if necessary.
  • Screenshots/screencasts have been attached, if there are any visual changes. For interactive or animated visual changes, a screencast is preferred.
  • All code follows the
    Scala,
    Java,
    TypeScript,
    and
    Rust
    style guides. In case you are using a language not listed above, follow the Rust style guide.
  • Unit tests have been written where possible.

@@ -89,7 +89,7 @@ type Decimal
Value (big_decimal : BigDecimal)

## ICON input_number
Construct a `Decimal` from a string or integer.
Construct a `Decimal` from a string, integer or float.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Construct a `Decimal` from a string, integer or float.
Construct a `Decimal` from a Text, Integer or Float.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -114,11 +114,21 @@ type Decimal
Create a `Decimal` from a string.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Create a `Decimal` from a string.
Create a `Decimal` from a Text.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


c = dec 12.345
dec : Text | Integer | Float -> Math_Context | Nothing -> Decimal ! Arithmetic_Error | Number_Parse_Error
dec (x : Text | Integer | Float) (mc : Math_Context | Nothing = Nothing) -> Decimal ! Arithmetic_Error | Number_Parse_Error =
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what we should do with Math_Context | Nothing - won't play nicely with the widgets.
One to think about in the decimal refinement.

Copy link
Member

Choose a reason for hiding this comment

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

Shall we just add 2 constructors to Math_Context? e.g. Custom and Default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@GregoryTravis GregoryTravis mentioned this pull request Jul 10, 2024
5 tasks

## PRIVATE
internal_representation : [Integer]
internal_representation self = [self.unscaled_value, self.precision, self.scale]
internal_representation self -> [Integer] = [self.unscaled_value, self.precision, self.scale]
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is still valid. Probably accepted by the compiler, but I have no idea what it will do.

I think we should use

Suggested change
internal_representation self -> [Integer] = [self.unscaled_value, self.precision, self.scale]
internal_representation self -> Vector Integer = [self.unscaled_value, self.precision, self.scale]

instead, as that's what is used most of the time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what happens when you write in other languages on the weekend.

to_text_without_scientific_notation self -> Text = self.big_decimal.toPlainString

## ICON input_number
Construct a `Decimal` from a string, integer or float.
Copy link
Member

Choose a reason for hiding this comment

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

I assume as @jdunkerley suggested we probably also want to replace string with Text here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -97,15 +105,15 @@ add_specs suite_builder =
mc4 = Math_Context.new 4
mc5 = Math_Context.new 5

Problems.not_expect_warning (Decimal.new "123.25")
Problems.not_expect_warning Any (Decimal.new "123.25")
Copy link
Member

Choose a reason for hiding this comment

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

not_expect_warning is especially useful if we do not expect a warning of type X but we acknowledge there may be other kinds of warnings.

If you expect no warnings at all, I'd recommend Problems.assume_no_problems

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 355 to 358
Problems.expect_only_warning Loss_Of_Numeric_Precision (d < d)
Problems.expect_only_warning Loss_Of_Numeric_Precision (d > d)
Problems.expect_only_warning Loss_Of_Numeric_Precision (d <= d)
Problems.expect_only_warning Loss_Of_Numeric_Precision (d >= d)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have these warnings here? I don't think I understand. I'm comparing a Decimal with Decimal - why would that give me a warning???

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is quite wrong and I missed it because of other problems with the test and with comparison. I marked this pending and it will be completed in #10163.

@GregoryTravis GregoryTravis added the CI: No changelog needed Do not require a changelog entry for this PR. label Jul 11, 2024
@GregoryTravis GregoryTravis added the CI: Ready to merge This PR is eligible for automatic merge label Jul 16, 2024
@mergify mergify bot merged commit 0268cbb into develop Jul 16, 2024
36 checks passed
@mergify mergify bot deleted the wip/gmt/10480-tweaks branch July 16, 2024 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: No changelog needed Do not require a changelog entry for this PR. CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants