ColorVectorSpace.jl icon indicating copy to clipboard operation
ColorVectorSpace.jl copied to clipboard

auto promote to more bits

Open bjarthur opened this issue 9 years ago • 4 comments

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 avatar Mar 16 '16 12:03 bjarthur

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.

bjarthur avatar Mar 23 '16 11:03 bjarthur

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

timholy avatar Mar 23 '16 12:03 timholy

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 avatar Mar 27 '16 11:03 mronian

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}

mronian avatar Mar 27 '16 21:03 mronian