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

auto promote to more bits #30

Open
bjarthur opened this issue Mar 16, 2016 · 4 comments
Open

auto promote to more bits #30

bjarthur opened this issue Mar 16, 2016 · 4 comments

Comments

@bjarthur
Copy link
Member

suming over an Array{UInt8} automatically promotes to UInt64 to prevent overflow. yet doing so over an Array{Gray{UFixed{UInt8}}} does not. would be nice if these behaved similarly.

julia> using ColorVectorSpace

julia> dUInt8 = rand(UInt8,3,3,3);

julia> typeof(sum(dUInt8, 3))
Array{UInt64,3}

julia> dGreyUInt8 = convert(Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},3}, dUInt8);

julia> typeof(dGreyUInt8)
Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},3}

julia> typeof(sum(dGreyUInt8, 3))
Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},3}
@bjarthur
Copy link
Member Author

ahah, so i realize now that adding more bits to UFixed will not help with overflow. i guess the solution is to convert to Float.

@timholy timholy reopened this Mar 23, 2016
@timholy
Copy link
Member

timholy commented Mar 23, 2016

Seems like something should be done, so I'm reopening. Don't have time now, though 😄.

@mronian
Copy link
Contributor

mronian commented Mar 27, 2016

The type is promoted even if there is no overflow. For example -

julia> AZero = zeros(UInt8, 3, 3, 3);

julia> typeof(sum(AZero))
UInt32

julia> ARnd = rand(UInt8, 3, 3, 3);

julia> typeof(sum(ARnd))
UInt32

julia> A1DZero = zeros(UInt8,3)
3-element Array{UInt8,1}:
 0x00
 0x00
 0x00

julia> typeof(sum(A1DZero))
UInt32

This happens only for arrays though -

julia> typeof(sum(AZero))
Int32

julia> a = UInt8(200)
0xc8

julia> b = UInt8(150)
0x96

julia> typeof(a+b)
UInt8

julia> a+b
0x5e

julia> Int(a+b)
94

So we could just promote the type everytime an array is summed over.

@mronian
Copy link
Contributor

mronian commented Mar 27, 2016

The sum does check for overflow if you are not summing in any particular dimension. It fails only if you give the dimension. For example -

julia> AGrayZero = zeros(ColorTypes.Gray{U8},3);

julia> typeof(AGrayZero)
Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},1}

julia> typeof(sum(AGrayZero))
ColorTypes.Gray{Float64}

julia> typeof(sum(AGrayZero,1))
Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},1}

@bjarthur In your example too -

julia> typeof(sum(dGreyUInt8, 3))
Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},3}

julia> typeof(sum(dGreyUInt8))
ColorTypes.Gray{Float64}

Also, in the normal case with dUInt8, summing with and without the dimension promotes the answer to different types -

julia> typeof(sum(dUInt8))
UInt32

julia> typeof(sum(dUInt8,1))
Array{UInt64,3}

julia> typeof(sum(dUInt8,2))
Array{UInt64,3}

julia> typeof(sum(dUInt8,3))
Array{UInt64,3}

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

No branches or pull requests

3 participants