I see...  It's actually a Uint, not a Uint8 that is passed to f(), but the 
result is the same.   I'm running into a lot of traps like this.  Consider 
the following analogous C code:

int f () {
    unsigned int s = 4;
    int a;
    a = abs((s % 3) - 3);
    printf("a: %d", a);
    return a;
}

The C compiler has no trouble evaluating the abs(...) the way it's intended 
even though 's' is clearly typed as an unsigned int.  What's going on in 
Julia?  It's like in Julia world we aren't allowed to ask what 2 - 3 
evaluates to if 2 is an unsigned type, regardless of what type we would 
store the result in?


On Saturday, September 5, 2015 at 10:57:01 PM UTC-4, Seth wrote:
>
> You're passing a UInt8 to f() which results in a mod of 
> 0x0000000000000001, from which 3 is subtracted resulting 
> in 0xfffffffffffffffe return value. Casting that to an Int, you get -2.
>
> Try f(UInt8(4)) to see this in action.
>
> In 0.4, your test_abs() example results in an InexactError.
>
> On Saturday, September 5, 2015 at 7:31:35 PM UTC-7, Corey Moncure wrote:
>>
>> Can someone help me understand what's going on here?
>> Maybe I've been sitting at my desk too long?
>>
>> julia> arr = hex2bytes("14fb9c03")
>> 4-element Array{Uint8,1}:
>>  0x14
>>  0xfb
>>  0x9c
>>  0x03
>>
>> julia> f(x) = abs((x % 3) - 3)
>> f (generic function with 1 method)
>>
>> julia> function test_abs(bytes_input::Array{Uint8})
>>        input_len::Uint = sizeof(bytes_input)
>>        a::Int = f(input_len)
>>        println("a: ", a, " input len: ", input_len)
>>        end
>> test_abs (generic function with 1 method)
>>
>> julia> test_abs(arr)
>> a: -2 input len: 4                  <-- minus 2????
>>
>> julia> sizeof(arr)
>> 4
>>
>> julia> f(4)
>> 2                                   <-- expected
>>
>>
>>
>>
>>

Reply via email to