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

Add rounding functions for Fixed (Fixes #153) #158

Merged
merged 3 commits into from
Jan 11, 2020

Conversation

kimikage
Copy link
Collaborator

@kimikage kimikage commented Dec 30, 2019

This is a part of #151 and fixes #153.

succeeding PRs: this -> isinteger(#120) -> rem(#150) -> Integer(#154) -> Rational(#157)

Edit:
Let's decide #159 first. Done.

This also simplifies the test for rounding functions.
@timholy
Copy link
Member

timholy commented Jan 11, 2020

Let's merge this before #163

@kimikage
Copy link
Collaborator Author

Though I merged PR #159, this PR supports Fixed{Int8,8} and so on, i.e. I reduced the test cases for f == 8sizeof(T) and added TODO comments.
Due to inlining, the depwarn is somewhat noisy. (Sorry, I overlooked this.)
In addition, I added support for specifying RoundingMode for round.

Comment on lines +106 to +109
function floor(x::Fixed{T,f}) where {T, f}
f == bitwidth(T) && x.i < 0 && throw_converterror(Fixed{T,f}, -1) # TODO: remove this line
Fixed{T,f}(x.i & intmask(x), 0)
end
Copy link
Collaborator Author

@kimikage kimikage Jan 11, 2020

Choose a reason for hiding this comment

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

I would like to confirm just in case, your implementation is much more complex.

function round(x::Fixed{T,f}, ::RoundingMode{:Down}) where {T,f}
mask = abs(rawminusone(Fixed{T,f}) + oneunit(T))
xraw = reinterpret(x)
return Fixed{T,f}((xraw & ~mask) + (signbit(xraw) & (xraw & mask) != 0 ? rawminusone(Fixed{T,f}) : zero(xraw)), 0)
end

Am I overlooking something?

(I'm sleepy now, so the decision for merging is pending. Feel free to merge this.:sleepy:)

Copy link
Member

Choose a reason for hiding this comment

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

No, you're absolutely right, the simpler one looks correct. 👍

@timholy timholy merged commit 402e637 into JuliaMath:master Jan 11, 2020
@kimikage kimikage deleted the round branch February 15, 2020 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding rounding functions for Fixed
2 participants