# Is there any way to explicitly allow Arithmetic overflow, for hash calculations?

**URL:** <https://forum.crystal-lang.org/t/is-there-any-way-to-explicitly-allow-arithmetic-overflow-for-hash-calculations/1449>\
**Category:** Crystal Contrib\
**Created:** [December 6, 2019, 7:07am UTC](https://forum.crystal-lang.org/t/is-there-any-way-to-explicitly-allow-arithmetic-overflow-for-hash-calculations/1449 "2019-12-06T07:07:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndbroadbent](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ndbroadbent/32/637_2.png) [@ndbroadbent](https://forum.crystal-lang.org/u/ndbroadbent)\
**Post date:** [December 6, 2019, 7:07am UTC](https://forum.crystal-lang.org/t/is-there-any-way-to-explicitly-allow-arithmetic-overflow-for-hash-calculations/1449/1 "2019-12-06T07:07:04Z")

</div>

Hello,

I’m trying to fix a bug in the [mocks.cr shard](https://github.com/waterlink/mocks.cr). ([Here’s my issue on GitHub](https://github.com/waterlink/mocks.cr/issues/38#issuecomment-562455232).) It’s doing some hash calculations like this:

```auto
      def hash
        object_id.hash * 32 + args.hash
      end

      # ...

      def hash
        @registry_name.hash * 32 * 32 + @name.hash * 32 + @object_id.hash
      end

```

These are crashing with `OverflowError: Arithmetic overflow`:

```auto
     Error: Arithmetic overflow

       OverflowError: Arithmetic overflow
         lib/mocks/src/mocks/registry.cr:86:60 in 'hash'
         /usr/local/Cellar/crystal/0.31.1/src/hash.cr:893:12 in 'key_hash'
         /usr/local/Cellar/crystal/0.31.1/src/hash.cr:336:5 in 'upsert'
         /usr/local/Cellar/crystal/0.31.1/src/hash.cr:912:5 in '[]='
         lib/mocks/src/mocks/registry.cr:104:9 in 'add'
         lib/mocks/src/mocks/registry.cr:156:9 in 'store_stub'
  ...

```

Is there any way I can tell the compiler that I don’t care about overflows, since this is a hashing algorithm where the final result just needs to be unique?

In the meantime, I’ve been able to [fix these hash methods with some bit shifts (and sent a PR)](https://github.com/waterlink/mocks.cr/pull/40), to make sure it never overflows a UInt64:

```auto
      def hash
        (object_id.hash >> 2) + (args.hash >> 2)
      end

     # ...

      def hash
        (@registry_name.hash >> 3) + (@name.hash >> 3) + (@object_id.hash >> 3)
      end

```

Is there a better way to fix this issue?

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [December 6, 2019, 7:52am UTC](https://forum.crystal-lang.org/t/is-there-any-way-to-explicitly-allow-arithmetic-overflow-for-hash-calculations/1449/2 "2019-12-06T07:52:54Z")

</div>

Arithmetic operators that can cause overflow can be prefixed by `&` to make them wrap.

---

<div class="post-metadata">

**Author:** ![ndbroadbent](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ndbroadbent/32/637_2.png) [@ndbroadbent](https://forum.crystal-lang.org/u/ndbroadbent)\
**Post date:** [December 6, 2019, 8:30am UTC](https://forum.crystal-lang.org/t/is-there-any-way-to-explicitly-allow-arithmetic-overflow-for-hash-calculations/1449/3 "2019-12-06T08:30:13Z")

</div>

Oh awesome, thank you!

(I couldn’t find this in the docs, but [I just found it in the release notes for 0.27.0, under the ARITHMETIC CHANGES section](https://crystal-lang.org/2018/11/01/crystal-0.27.0-released.html).)

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [December 6, 2019, 10:15am UTC](https://forum.crystal-lang.org/t/is-there-any-way-to-explicitly-allow-arithmetic-overflow-for-hash-calculations/1449/4 "2019-12-06T10:15:53Z")

</div>

Yes, the operators reference is [currently being worked on](https://github.com/crystal-lang/crystal-book/pull/370).
