-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[Lang] Make floor, ceil and round accept a dtype optional argument #5307
Conversation
✅ Deploy Preview for docsite-preview ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
dtype
optional argumentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OOC: what's the motivation for this change?
A common need in simulations and renderings is that, for a point |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Would you mind add an example with dtype specified in the docstring, as well as some test cases like test_random_int
?
I doubt whether this change is necessary. Does |
Agree. What's worse is that the range of |
I'd actually vote for a function like
Sometimes using
IMHO this is actually not making things worse :-) You need such a cast anyways in many use cases. |
I think the point is that |
Yeah, but users can do some custom checks to be safe. For example: template <std::integral T>
std::optional<T> ifloor(double num) {
auto double_result = std::floor(num);
auto max = std::static_cast<double>(<std::numeric_limits<T>::max); // may have precision issues, just for demonstration.
auto min = std::static_cast<double>(<std::numeric_limits<T>::min);
return double_result <= max && double_resuld >= min ? std::static_cast<T>(double_result) : std::nullopt;
} Of course we can do something similar inside the API to avoid UB, but I don't think there is a generally accepted logic here. IMHO that's why
I do agree it won't overflow and we just cast it immediately in most cases. My point is that there should be as few APIs as possible with potentially undefined behavior, especially in the standard library. It's probably ok if thinking of it as a syntactic sugar and it does save some typing in many cases. But if UB is expected, at least it shall be documented. |
I would like to say the syntax UB isn't a matter here if we guarantee the float version of this function is well-defined. Like many C++ standard functions, unsafe versions are provided, like |
That's true, but I believe the user will always get a chance to use |
This change can indeed make user code slightly shorter, but IMHO a function should have single responsibility. If we decide to add a
The syntax
Could you provide an example running into the bug? IIRC the current implementation returns a value of the same type as the input value. |
Probably we only do this for |
Maybe |
I think this is a valid point. If
I agree. Now giving it more thoughts, I think one possibility that C
So my current opinion is that we don't have to abide to C's API design, but we should do the same for |
Co-authored-by: Yuanming Hu <[email protected]>
Co-authored-by: Yuanming Hu <[email protected]>
for more information, see https://pre-commit.ci
Added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! The implementation LGTM, but could you update the doc and add a few test cases?
for more information, see https://pre-commit.ci
test script added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM in general. Please also update https://docs.taichi.graphics/docs/operator#other-arithmetic-functions.
for more information, see https://pre-commit.ci
Co-authored-by: Yuanming Hu <[email protected]>
Currently the integer returned by
floor
function has float type, this PR adds an option to the returned value type.