-
Notifications
You must be signed in to change notification settings - Fork 33
[RFC] Commonize further code between Fixed
and Normed
#168
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,28 @@ nbitsfrac(::Type{X}) where {T, f, X <: FixedPoint{T,f}} = f | |
rawtype(::Type{X}) where {T, X <: FixedPoint{T}} = T | ||
|
||
# construction using the (approximate) intended value, i.e., N0f8 | ||
*(x::Real, ::Type{X}) where {X<:FixedPoint} = X(x) | ||
*(x::Real, ::Type{X}) where {X <: FixedPoint} = _convert(X, x) | ||
|
||
# constructor-style conversions | ||
(::Type{X})(x::Real) where {X <: FixedPoint} = _convert(X, x) | ||
|
||
function (::Type{<:FixedPoint})(x::AbstractChar) | ||
throw(ArgumentError("FixedPoint (Fixed or Normed) cannot be constructed from a Char")) | ||
end | ||
(::Type{X})(x::Complex) where {X <: FixedPoint} = X(convert(real(typeof(x)), x)) | ||
function (::Type{X})(x::Base.TwicePrecision) where {X <: FixedPoint} | ||
floattype(X) === BigFloat ? X(big(x)) : X(convert(floattype(X), x)) | ||
end | ||
|
||
# conversions | ||
function Base.Bool(x::FixedPoint) | ||
x == zero(x) ? false : x == oneunit(x) ? true : throw(InexactError(:Bool, Bool, x)) | ||
end | ||
function (::Type{Ti})(x::FixedPoint) where {Ti <: Integer} | ||
isinteger(x) || throw(InexactError(:Integer, typeof(x), x)) | ||
floor(Ti, x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FixedPointNumbers doesn't know where or what errors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course, when |
||
end | ||
Base.Rational{Ti}(x::FixedPoint) where {Ti <: Integer} = Rational{Ti}(Rational(x)) | ||
|
||
""" | ||
isapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y))) | ||
|
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.
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 nowAny
but IIRC it used to beInteger
.)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.
I think the rawtype
T
should not be anAbstractChar
type. However, whether to allow conversion from aChar
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.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.
FYI:
JuliaLang/julia#8816
JuliaLang/julia#26286
It seems that
AbstractChar
was added long after the supertype ofChar
had been changed.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.
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
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.
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.