-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
weird construction of rationals #49757
Comments
The difference in this case is that the promotion happens anyway. That is, in this case the type parameter is simply unnecessarily imprecise. |
So by @timholy logic we should throw an error in an example like |
Huge no-no. It's important to be able to construct precise types. Imagine I'm passing that object into a type-unstable function and I've provided a typeassert that insists on that precise type? That's perfectly legal code, might be out there in the wild, but suddenly we'd have no way of constructing it. Moreover, in lots of places we use julia> using FixedPointNumbers, ColorTypes
julia> col = RGB{N0f8}(1, 0.2, 0.4)
RGB{N0f8}(1.0,0.2,0.4)
julia> dump(col)
RGB{N0f8}
r: N0f8
i: UInt8 0xff
g: N0f8
i: UInt8 0x33
b: N0f8
i: UInt8 0x66 I can use the easy-to-type floating-point literals or mixed types for the three arguments (as illustrated here) and still easily get the precise 24-bit color I want. The type of the requested output takes precedence over the type of the inputs, because we're using constructor-syntax. From what I can tell, you're thinking of julia> struct MyType{T}
x::T
end
julia> Union{MyType{Int},MyType{Float64}}(x::Real) = MyType(int_or_f64(x))
julia> int_or_f64(x::Integer) = Int(x)
int_or_f64 (generic function with 1 method)
julia> int_or_f64(x::Real) = Float64(x)
int_or_f64 (generic function with 2 methods)
julia> Union{MyType{Int},MyType{Float64}}(0xff)
MyType{Int64}(255) Of course you could also call this |
Definitely not. It should return a |
What about the fact that |
That's ok - the constructor only guarantees that the numerator/denominator are |
OK, closing then. |
Should a non-concrete type parameter be kept even though the numerator and denominator get promoted?
For example, it seems like in this case
typeof(q)
should returnInt8
, because there's a promotion of the members anyway.I think this could be fixed by merely changing this line:
julia/base/rational.jl
Line 36 in 0b5ec1f
to
return checked_den(T(num), T(den))
The text was updated successfully, but these errors were encountered: