On Tue, Apr 5, 2016 at 10:45 AM, Didier Verna <did...@didierverna.net> wrote:
>
>   Hello,
>
> the manual says: "In Julia, exceeding the maximum representable value of
> a given type results in a wraparound behavior:", but that seems to be
> the case only for the native type and above:
>
> julia> typeof(typemax(Int64)  + 1) -> Int64
> julia> typeof(typemax(Int128) + 1) -> Int128
>
> but:
>
> julia> typeof(typemax(Int8)   + 1) -> Int64
> julia> typeof(typemax(Int16)  + 1) -> Int64
> julia> typeof(typemax(Int32)  + 1) -> Int64
>
> These last 3 results seem inconsistent (and the manual incorrect)
> because for example, typeof(typemax(Int8)) -> Int8 and not Int64, so no,
> Julia doesn't do modular arithmetics all the time.

The literal `1` has type `Int`. The promotion rules for `Int8` and
`Int` state that, before the addition, `Int8` is converted to `Int`.
(On your system, it seems that `Int` is `Int64`.)

You can avoid this by writing `Int8(1)` instead of `1`; this should
recover the wrap-around behaviour.

Note that this wrap-around exists only for some simple operations (+ -
* abs); e.g. for division, there is an error instead.

-erik

-- 
Erik Schnetter <schnet...@gmail.com>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to