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

Small typo #20

Closed
wants to merge 1 commit into from
Closed

Small typo #20

wants to merge 1 commit into from

Conversation

mgautam98
Copy link
Contributor

Small typo in example

@johnnychen94
Copy link
Member

johnnychen94 commented Jun 30, 2020

Ha, actually this isn't a typo; the lower-case ones preserve the colorant types.

julia> using TestImages

julia> img = testimage("cameraman");

julia> Float64.(img) |> eltype
Float64

julia> float64.(img) |> eltype
Gray{Float64}

julia> img = testimage("lena");

julia> Float64.(img) |> eltype
ERROR: MethodError: no method matching Float64(::RGB{Normed{UInt8,8}})
Closest candidates are:
  Float64(::Int8) at float.jl:60
  Float64(::Int16) at float.jl:60
  Float64(::Int32) at float.jl:60
  ...
Stacktrace:
 [1] _broadcast_getindex_evalf at ./broadcast.jl:631 [inlined]
 [2] _broadcast_getindex at ./broadcast.jl:604 [inlined]
 [3] getindex at ./broadcast.jl:564 [inlined]
 [4] copy at ./broadcast.jl:854 [inlined]
 [5] ...
 [6] top-level scope at REPL[10]:1

julia> float64.(img) |> eltype
RGB{Float64}

There's also float, float32, n0f8 and etc defined in ImageCore.

Also, float(RGB{N0f8}) == RGB{Float32}, and floattype(RGB{N0f8}} == RGB{Float32}. The difference between floattype and float

It might be a little confusing at the beginning, here's the most frequent usage of them:

# MappedArrays.of_eltype
of_eltype(float(eltype(img)), img) # lazy type promotion

# this is more generic than `Gray{Float32}.(img)`
float.(img)

cc: @jiviteshjain as you might be unaware of these small details, either.

@mgautam98
Copy link
Contributor Author

Oops! Thanks for detailed information.

@timholy
Copy link
Member

timholy commented Jun 30, 2020

Just to make sure the logic is clear, since Float64 is a type, Float64(x) is a constructor call that you should expect to return an object of type Float64. (There's nothing built-in to the language to enforce that, although a proposal to add it and other things like it is being actively discussed, see JuliaLang/julia#36463 which comes at the problem from the standpoint of improving inference but in Julia 2.0 it would also enforce consistency). In contrast, float64 "just" a function with no expected relationship to a particular type, so is under no such constraints.

@mgautam98 mgautam98 deleted the typo branch June 30, 2020 15:08
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.

3 participants