# What is the maximum value of Nat64, Nat32, Nat16, Nat8, Int64, Int32, Int16, and Int8?

**URL:** <https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [March 28, 2022, 7:25am UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735 "2022-03-28T07:25:38Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![avi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/avi/32/1904_2.png) [@avi](https://forum.dfinity.org/u/avi)\
**Post date:** [March 28, 2022, 7:25am UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/1 "2022-03-28T07:25:38Z")

</div>

What is the maximum value of Nat64, Nat32, Nat16, Nat8, Int64, Int32, Int16, and Int8?

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [March 28, 2022, 8:15am UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/2 "2022-03-28T08:15:00Z")

</div>

2^64-1, 2^32-1, 2^16-1, 2^8-1, 2^63-1, 2^31-1, 2^15-1, 2^7-1.

But note that there are very few reasons to use them. The main ones are algorithms requiring bit fiddling (e.g., crypto, compression), or matching an external interface or data structure using them. In almost all other cases you are better off with Nat or Int (safer, typically requiring less memory, often even faster).

---

<div class="post-metadata">

**Author:** ![h1teshtr1path1](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/h1teshtr1path1/32/24662_2.png) [@h1teshtr1path1](https://forum.dfinity.org/u/h1teshtr1path1)\
**Post date:** [March 28, 2022, 4:11pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/3 "2022-03-28T16:11:57Z")

</div>

Can you also tell me one more thing, what can be the maximum size of Trie or Hashmaps Data Structures ? Is there any limit ? or they just depend on size of Canister (limited upto 4gb?)

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [March 29, 2022, 1:16am UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/4 "2022-03-29T01:16:38Z")

</div>

Oh, I didn’t know `Nat` may require less memory AND be faster than `NatX`… Why is that exactly?

Guess I need to refactor my code now.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [March 29, 2022, 1:17am UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/5 "2022-03-29T01:17:54Z")

</div>

They just depend on the size of the canister. But actually due to serialization, garbage collection, and other considerations, the available memory is probably closer to 3 GB, I believe. Hopefully that is temporary.

---

<div class="post-metadata">

**Author:** ![anon98822045](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@anon98822045](https://forum.dfinity.org/u/anon98822045)\
**Post date:** [March 29, 2022, 12:15pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/6 "2022-03-29T12:15:50Z")

</div>

I noticed that you can’t use a Nat as a key in a TrieMap because the Hash.hash function seems to be limited to Nat32. Is there any way around that?

When I try to do it the error I get is “canister trapped explicitly, losing precision”

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [March 29, 2022, 1:12pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/7 "2022-03-29T13:12:09Z")

</div>

Use something like the following as your hash function(actually hard code the number for fewer cycles)

```auto
func(a:Nat){Nat32.fromNat(a%(2^32 -1))};

```

Also see: [Hash Collisions](https://forum.dfinity.org/t/hash-collisions/11691)

---

<div class="post-metadata">

**Author:** ![anon98822045](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@anon98822045](https://forum.dfinity.org/u/anon98822045)\
**Post date:** [March 29, 2022, 1:22pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/8 "2022-03-29T13:22:00Z")

</div>

oh nice thanks, yeah I was going to use Text.hash but this solution seems better. I just hope we do get 64 bit hashes at some point.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [March 29, 2022, 1:54pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/9 "2022-03-29T13:54:06Z")

</div>

That Hash.hash function is buggy and should be fixed (See issue [Possible bug: Hash.hash(i : Nat) will trap if i \>= 2^32 · Issue #345 · dfinity/motoko-base · GitHub](https://github.com/dfinity/motoko-base/issues/345))

You could use

```auto
  /// Conversion. Wraps on overflow/underflow.
 Nat.fromIntWrap : Int -> Nat32;

```

Which won’t trap but won’t hash large numbers very well either (since it ignores all but the least significant 32 bits).

Note that you can apply `Nat.fromIntWrap(n)` to a `Nat`, `n`, by virtue of subtyping since `Nat <: Int`.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [March 29, 2022, 3:08pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/10 "2022-03-29T15:08:21Z")

</div>

I use @quint’s crypto library and Candy to get a 256 bit crypto secure hash like the below.

```auto
import Conversion "mo:candy/conversion";
import SHA256 "mo:crypto/SHA/SHA256";

SHA256.sum(CandyConversion.valuetoBytes(#Nat(a));

```

You may want to add a namespace into it depending on your use case. For that I use the .write() API endpoint.

```auto
        let h = SHA256.New();
        h.write(Conversion.valueToBytes(#Nat32(Text.hash("com.yourapp"))));
        h.write(Conversion.valueToBytes(#Nat(a);
        h.sum([]);

```

I had one issue with the crypto library where if I wrote a strange number of bytes it would throw. I think you have to have a mod(4) bytes…it would be nice if the crypto library had a padWriteLittleEndian() that would make sure a proper number of bytes get written. 🙂

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [July 11, 2022, 7:47am UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/11 "2022-07-11T07:47:57Z")

</div>

[Oops, never clicked the button.]

> [@jzxchiang](#):
>
> Oh, I didn’t know `Nat` may require less memory AND be faster than `NatX`… Why is that exactly?

Because Nat and Int are unboxed for small values (less than 31 bit), which means no allocation or indirection is required. The others are always stored with all bits, which means that Nat/Int32 and Nat/Int64 are always boxed on the heap.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [September 27, 2022, 10:01pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/12 "2022-09-27T22:01:58Z")

</div>

What’s the minimum and maximum values of the [`Int`](https://internetcomputer.org/docs/current/developer-docs/build/cdks/motoko-dfinity/base/Int) Type? The upper bound seems to stretch beyond the [`Int64`](https://internetcomputer.org/docs/current/developer-docs/build/cdks/motoko-dfinity/base/Int64) limit.

  

Example:

```auto
import Debug "mo:base/Debug";

let max_int_64: Int = 9_223_372_036_854_775_807; // 2^63 - 1

Debug.print("max_int_64: " # debug_show(max_int_64));
Debug.print("max + 200 : " # debug_show(max_int_64 + 200));

```

  

Output:

```auto
max_int_64: +9_223_372_036_854_775_807
max + 200 : +9_223_372_036_854_776_007

```

  
  

Note that this same test with `Int64`:

```auto
let max_int_64: Int64 = 9_223_372_036_854_775_807; // 2^63 - 1

Debug.print("max_int_64: " # debug_show(max_int_64));
Debug.print("max + 200 : " # debug_show(max_int_64 + 200));

```

Produces an arithmetic overflow error (as expected).

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [September 27, 2022, 10:17pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/13 "2022-09-27T22:17:21Z")

</div>

![infinite GIF](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/8/8a8dd0eb9b0ebe63179d0a5bfa3a45e673dcdb46.gif)

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [September 27, 2022, 10:35pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/14 "2022-09-27T22:35:31Z")

</div>

Maybe this is the value I’m looking for?

> [@Infinity and NaN](https://forum.dfinity.org/t/infinity-and-nan/14400/4):
>
> module { public func isNaN(v:Float): Bool { return ((v \< 0.000) == false) and ((v \> 0.000) == false) }; public func isInfinite(v:Float): Bool { return v \> 1.8e307 } } This is the method I wrote to judge NaN and Infinity, what’s wrong with it. . if so. Please point out where I am wrong

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [September 27, 2022, 10:43pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/15 "2022-09-27T22:43:05Z")

</div>

You’ve moved beyond my knowledge…interested in the answer. I assumed that like nat, ints just keep growing and taking up space and there is a bit for + or -. Of course, you eventually run out of bits in your memory, but I didn’t think there was an actual limit.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [September 27, 2022, 11:20pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/16 "2022-09-27T23:20:35Z")

</div>

@skilesare Just verified this, I start to receive `bigint function error` errors when using any floats larger than 1.79e+308.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [September 28, 2022, 12:01am UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/17 "2022-09-28T00:01:32Z")

</div>

At least we can count all the atoms in the universe. 😂

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [September 28, 2022, 4:25pm UTC](https://forum.dfinity.org/t/what-is-the-maximum-value-of-nat64-nat32-nat16-nat8-int64-int32-int16-and-int8/11735/18 "2022-09-28T16:25:23Z")

</div>

Int and Nat do not have any maximum value, other than what cannot be represented in 4 gigs of memory(\*). However, 64 bit floats have, which is the one you have approximated. After that, everything get’s rounded to infinity. And trying to invert float infinity to Int will fail.

I thought the Motoko compiler would warn when a float literal is rounded to infinity, but perhaps I was mistaken.

(\*) Though in practice, you will probably run out of cycles before you even get close.
