# Question about abs()

**URL:** https://discourse.julialang.org/t/question-about-abs/10198
**Category:** General Usage
**Tags:** integer-overflow
**Created:** [April 6, 2018, 12:01pm UTC](https://discourse.julialang.org/t/question-about-abs/10198 "2018-04-06T12:01:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![s920128](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s920128/32/3901_2.png) [@s920128](https://discourse.julialang.org/u/s920128)
#### Post date: [April 6, 2018, 12:01pm UTC](https://discourse.julialang.org/t/question-about-abs/10198/1 "2018-04-06T12:01:32Z")

</div>

I use abs(-9223372036854775808) in Julia  
but output is -9223372036854775808  
Is this a bug?

---

<div class="post-metadata">

### Author: ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)
#### Post date: [April 6, 2018, 12:20pm UTC](https://discourse.julialang.org/t/question-about-abs/10198/2 "2018-04-06T12:20:18Z")

</div>

No, this is an integer overflow problem because `-9223372036854775808` is the minimum number representable as a 64-bit signed integer. That is, it is equal to `typemin(Int64)`. The help for `abs` calls this out:

```julia
help?> abs
search: abs abs2 abspath AbstractSet abstract type AbstractChar AbstractDict AbstractFloat AbstractArray

  abs(x)

  The absolute value of x.

  When abs is applied to signed integers, overflow may occur, resulting in the return of a negative value.
  This overflow occurs only when abs is applied to the minimum representable value of a signed integer. That
  is, when x == typemin(typeof(x)), abs(x) == x < 0, not -x as might be expected.

  julia> abs(-3)
  3
  
  julia> abs(1 + im)
  1.4142135623730951
  
  julia> abs(typemin(Int64))
  -9223372036854775808

```

If you are dealing with integers of this magnitude then you should use BigInts:

```julia
julia> abs(big"-9223372036854775808")
9223372036854775808

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 6, 2018, 12:21pm UTC](https://discourse.julialang.org/t/question-about-abs/10198/3 "2018-04-06T12:21:45Z")

</div>

This is a consequence of [two’s complement arithmetic](https://en.wikipedia.org/wiki/Two%27s_complement), but possibly it should throw an `InexactError`.

In general, though, we don’t check for overflow in integer arithmetic for performance reasons:

```julia
julia> Int8(127) + Int8(1)
-128

julia> -Int8(-128)
-128

```

so it makes sense that `abs` is no different. If you are doing integer arithmetic that runs into this, you should generally use a larger integer type. If your numbers are so large that you are overflowing 64-bit integers, then you are probably doing some type of number theory or combinatorics (rather than counting real things like bytes or loop iterations) and you should use `BigInt`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 6, 2018, 12:55pm UTC](https://discourse.julialang.org/t/question-about-abs/10198/4 "2018-04-06T12:55:41Z")

</div>

> [@swt30](#):
>
> `help?> abs`  
> …  
> when `x == typemin(typeof(x))`, `abs(x) == x < 0`, not `-x` as might be expected.

This actually seems inaccurate, because the very same phenomenon occurs with minus as with `abs`:

```julia
julia> x = typemin(Int)
-9223372036854775808

julia> -x
-9223372036854775808

julia> abs(x) == -x == x
true

```

---

<div class="post-metadata">

### Author: ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)
#### Post date: [April 6, 2018, 1:16pm UTC](https://discourse.julialang.org/t/question-about-abs/10198/5 "2018-04-06T13:16:17Z")

</div>

Yes, another overflow: `x == typemin(T) == typemax(T) + one(T) == -x` for any machine integer type. It depends if you read `-x` in those docs as meaning a machine operation on a machine integer or a mathematical operation on a mathematical integer—could probably be clarified!

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [April 6, 2018, 1:42pm UTC](https://discourse.julialang.org/t/question-about-abs/10198/6 "2018-04-06T13:42:37Z")

</div>

> [@swt30](#):
>
> If you are dealing with integers of this magnitude then you should use BigInts:

Just in case you can be sure your numbers would always fit in 128 bits, then `Int128` will be much faster and take much less memory than `BigInt`.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 6, 2018, 2:34pm UTC](https://discourse.julialang.org/t/question-about-abs/10198/7 "2018-04-06T14:34:22Z")

</div>

> [@stevengj](#):
>
> so it makes sense that `abs` is no different

IMO it would be best if these functions continued to refrain from overflow checks. A situation where some operations (eg `abs`) check, but others (eg `+`, `-`, `^`, …) don’t, is not really useful in practice. Alternatives exist, eg [SaferIntegers.jl checks](https://github.com/JeffreySarnoff/SaferIntegers.jl/blob/f725b679d1b43f8425543e8cf35b6a77f2523e4b/src/int_ops.jl#L32-L36) for this.
