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

[stdlib] Roundable trait with ndigits param in the round operation and Round to nearest even logic #2752

Closed

Conversation

msaelices
Copy link
Contributor

@msaelices msaelices commented May 19, 2024

  • Add Roundable.__round__(ndigits) method
  • Implement the method in all the types that adhere to Roundable
  • Round to the next even number when the diff is exactly 0.5, recommended way in IEEE 754, which also Python adheres to.
  • Tests

@msaelices msaelices changed the title Roundable trait with ndigits param in the round operation Roundable trait with ndigits param in the round operation and Round to nearest even logic May 19, 2024
As the round() and Roundable trait was moved previously there

Signed-off-by: Manuel Saelices <[email protected]>
@laszlokindrat laszlokindrat self-assigned this May 20, 2024
@laszlokindrat laszlokindrat changed the title Roundable trait with ndigits param in the round operation and Round to nearest even logic [stdlib] Roundable trait with ndigits param in the round operation and Round to nearest even logic May 20, 2024
Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! I have a few concerns with the Int/IntLiteral stuff, but looking good otherwise!

stdlib/src/builtin/float_literal.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/math.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/math.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/int_literal.mojo Outdated Show resolved Hide resolved
stdlib/test/builtin/test_float_literal.mojo Show resolved Hide resolved
@laszlokindrat
Copy link
Contributor

laszlokindrat commented May 27, 2024

@msaelices Are you still working on this? This is a very nice improvement, and I would love to bring it in!

@msaelices
Copy link
Contributor Author

@msaelices Are you still working on this? This is a very nice improvement, and I would love to bring it in!

Sure. I'm working on this now.

@msaelices
Copy link
Contributor Author

@laszlokindrat could you please take another look?

Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

Great progress, thanks for addressing my reviews! It seems you could split this up into several independent patches for the different types. That would make it easier to land these. Then the patch for the trait could be added after they all landed, and it should be simple. It's up to you, but I think it would speed things up :)

stdlib/src/builtin/float_literal.mojo Show resolved Hide resolved
stdlib/src/builtin/float_literal.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/float_literal.mojo Show resolved Hide resolved
@@ -269,6 +269,47 @@ struct IntLiteral(
"""
return self

@always_inline("nodebug")
fn __divmod__(self, rhs: Self) -> Tuple[Self, Self]:
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking: maybe in a followup: Maybe it's time for a DivModable trait or something. Then we could hook these up into the builtin divmod function.

@@ -1142,6 +1142,18 @@ struct SIMD[type: DType, size: Int = simdwidthof[type]()](
"""
return llvm_intrinsic["llvm.round", Self, has_side_effect=False](self)

@always_inline("nodebug")
fn __round__(self, ndigits: Int) -> Self:
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking: SIMD might benefit from having a separate overload that does this elementwise between two vectors. Might get tricky for the different dtypes, though. Anyway, just a followup idea.

stdlib/src/builtin/int_literal.mojo Show resolved Hide resolved
stdlib/src/builtin/int_literal.mojo Show resolved Hide resolved
@msaelices
Copy link
Contributor Author

Great progress, thanks for addressing my reviews! It seems you could split this up into several independent patches for the different types. That would make it easier to land these. Then, the patch for the trait could be added after they all landed, and it should be simple. It's up to you, but I think it would speed things up :)

I think I prefer not to split the PR. It would be harder for me to cherry-pick all the commits or redo my work and track all of them.

@msaelices msaelices force-pushed the roundable-with-ndigits-param-v2 branch from 9247ca1 to 6039f4e Compare May 28, 2024 22:03
@msaelices
Copy link
Contributor Author

@laszlokindrat Could you please TAL? 🏓

Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

Very nice, thank you for working on this!

@laszlokindrat
Copy link
Contributor

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label May 29, 2024
@laszlokindrat
Copy link
Contributor

!sync

1 similar comment
@laszlokindrat
Copy link
Contributor

!sync

@modularbot
Copy link
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added the merged-internally Indicates that this pull request has been merged internally label May 30, 2024
@msaelices msaelices closed this May 30, 2024
@msaelices msaelices deleted the roundable-with-ndigits-param-v2 branch May 30, 2024 20:41
modularbot pushed a commit that referenced this pull request May 31, 2024
…peration and Round to nearest even logic (#40794)

[External] [stdlib] Roundable trait with ndigits param in the round
operation and Round to nearest even logic

* Add `Roundable.__round__(ndigits)` method
* Implement the method in all the types that adhere to `Roundable`
* Round to the next even number when the diff is exactly 0.5,
recommended way in [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754),
which also Python adheres to.
* Tests

---------

Co-authored-by: Manuel Saelices <[email protected]>
Closes #2752
MODULAR_ORIG_COMMIT_REV_ID: f3349ff968cdb519bd505427a6aea8afe1ae1bb0
@modularbot modularbot added the merged-externally Merged externally in public mojo repo label May 31, 2024
@modularbot
Copy link
Collaborator

Landed in 4edfc69! Thank you for your contribution 🎉

modularbot pushed a commit that referenced this pull request Jun 7, 2024
…peration and Round to nearest even logic (#40794)

[External] [stdlib] Roundable trait with ndigits param in the round
operation and Round to nearest even logic

* Add `Roundable.__round__(ndigits)` method
* Implement the method in all the types that adhere to `Roundable`
* Round to the next even number when the diff is exactly 0.5,
recommended way in [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754),
which also Python adheres to.
* Tests

---------

Co-authored-by: Manuel Saelices <[email protected]>
Closes #2752
MODULAR_ORIG_COMMIT_REV_ID: f3349ff968cdb519bd505427a6aea8afe1ae1bb0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants