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

[RFC] Commonize further code between Fixed and Normed #168

Merged
merged 3 commits into from
Jan 16, 2020

Conversation

kimikage
Copy link
Collaborator

@kimikage kimikage commented Jan 15, 2020

These commits fix #154 and commonize:

  • Bool and Integer conversions
  • Rational conversions
  • constructor-style conversions

This PR introduces some breaking changes:

  • Bool throws the InexactError for the inputs other than zero/one.
  • The return type of Integer(::Normed{T}) is now T.
  • Integer(::Fixed) throws the InexactError instead of MethodError.
  • The return type of Rational(::FixedPoint{T}) is now Rational{T}, except Fixed{Int8,8}, Q0f7, Q0f15 and so on.

This includes following breaking changes:
- `Bool` throws the InexactError for inputs other than zero/one.
- The return type of `Integer(::Normed{T})` is now `T`.
- `Integer(::Fixed)` throws the InexactError instead of MethodError.
This includes a breaking change in the return type of `Rational(::Fixed)`.
@kimikage
Copy link
Collaborator Author

Regarding the changing the return types, users can use some concrete type names instead (e.g. Int(), Int32(), Rational{Int32}()).

@codecov-io
Copy link

Codecov Report

Merging #168 into master will increase coverage by 0.52%.
The diff coverage is 93.1%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #168      +/-   ##
==========================================
+ Coverage   88.12%   88.64%   +0.52%     
==========================================
  Files           5        5              
  Lines         379      370       -9     
==========================================
- Hits          334      328       -6     
+ Misses         45       42       -3
Impacted Files Coverage Δ
src/fixed.jl 89.33% <100%> (+1.38%) ⬆️
src/FixedPointNumbers.jl 86.2% <100%> (+1.39%) ⬆️
src/normed.jl 89.88% <84.61%> (+0.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0ae5b82...497f4ba. Read the comment docs.

Copy link
Member

@timholy timholy 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!

# constructor-style conversions
(::Type{X})(x::Real) where {X <: FixedPoint} = _convert(X, x)

function (::Type{<:FixedPoint})(x::AbstractChar)
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 if we still need this. Do you fail the ambiguity check if you delete it? (I ask because the supertype of AbstractChar is now Any but IIRC it used to be Integer.)

Copy link
Collaborator Author

@kimikage kimikage Jan 16, 2020

Choose a reason for hiding this comment

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

I think the rawtype T should not be an AbstractChar type. However, whether to allow conversion from a Char is up to the implementation, regardless of what its supertype is. I left this fool-proof just because I had no reason to remove it. So, Let's get rid of it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FYI:
JuliaLang/julia#8816
JuliaLang/julia#26286
It seems that AbstractChar was added long after the supertype of Char had been changed.

Copy link
Member

Choose a reason for hiding this comment

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

My guess is that this was added purely as a means to avoid an ambiguity. In the old days, ambiguities printed as warnings when you loaded the package, which was really annoying.

JuliaLang/julia#6190
JuliaLang/julia#16125

Copy link
Collaborator Author

@kimikage kimikage Jan 16, 2020

Choose a reason for hiding this comment

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

The trigger is certainly the ambiguities. (cf. PR #105)
What I want to say in the comment above is that this may be intentionally left as a fool-proof.
I thought I would get a MethodError, but the conversion is done without errors.

julia> Normed{UInt32,16}('a')
97.0N16f16

end
function (::Type{Ti})(x::FixedPoint) where {Ti <: Integer}
isinteger(x) || throw(InexactError(:Integer, typeof(x), x))
floor(Ti, x)
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to combine these (compute the output first and then check for equality)? Or does that miss some corner cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FixedPointNumbers doesn't know where or what errors floor(Ti, x) throws. So, it is better to check first for a clear error message.
I don't want to optimize the methods which can throw errors, in principle.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course, when isinteger uses floor, it can be optimized simply. It was obvious to me, so I forgot to write a TODO comment. 😄

@kimikage
Copy link
Collaborator Author

For now, I will merge this as is.
Even if we improve the implementations again later, these changes will make the improvement easier.

@kimikage kimikage merged commit 05fd7e7 into JuliaMath:master Jan 16, 2020
@kimikage kimikage deleted the commonize2 branch January 16, 2020 11:12
@kimikage kimikage mentioned this pull request Feb 3, 2020
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.

Inconsistent Integer results
3 participants