Le dimanche 06 septembre 2015 à 08:50 -0400, Keno Fischer a écrit :
> Note however, that this behavior has changed in 0.4 and you need to
> be more explicit in converting UInts to Ints:
> 
> 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)
> ERROR: InexactError()
>  in test_abs at ./none:3
...and getting this error message is terribly reassuring! Seeing that C
-like madness in Julia made me shudder for a few minutes.


Regards

> On Sun, Sep 6, 2015 at 7:53 AM, Yichao Yu <yyc1...@gmail.com> wrote:
> > On Sat, Sep 5, 2015 at 11:57 PM,  <ele...@gmail.com> wrote:
> > >
> > > Subtlty of C, literal integer constants are signed, so s is
> > converted to
> > > signed before the % and so the whole abs is signed.
> > 
> > And in any case abs in c is `int abs(int);` so the argument is
> > always
> > converted to signed integer before passing to `abs`.
> > 
> > >
> > >
> > > On Sunday, September 6, 2015 at 1:23:46 PM UTC+10, Corey Moncure
> > wrote:
> > >>
> > >> 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