# Canister called \`ic0.trap\` with message: 'losing precision'

**URL:** <https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235>\
**Category:** Motoko\
**Created:** [April 1, 2025, 1:12am UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235 "2025-04-01T01:12:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [April 1, 2025, 1:12am UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235/1 "2025-04-01T01:12:25Z")

</div>

What is this error? How to correct it?

```plaintext
Canister called `ic0.trap` with message: 'losing precision'.

```

Suspected code:

```motoko
        switch(await CyclesLedger.icrc1_transfer({
            to = {owner = Principal.fromActor(this); subaccount = null};
            fee = null;
            memo = null;
            from_subaccount = ?(Principal.toBlob(user));
            created_at_time = ?(Nat64.fromNat(Int.abs(Time.now())));
            amount = Int.abs(Float.toInt(Float.fromInt(amountToMove) * (1.0 - env.revenueShare)));
        })) {
            case (#Err e) {
                Debug.trap("transfer failed: " # debug_show(e));
            };
            case (#Ok _) {};
        };
        // Pay to the vendor (TODO: Ensure no double fee later.)
        switch(await CyclesLedger.icrc1_transfer({
            to = {owner = revenueRecipient; subaccount = null};
            fee = null;
            memo = null;
            from_subaccount = ?(Principal.toBlob(user));
            created_at_time = ?(Nat64.fromNat(Int.abs(Time.now())));
            amount = Int.abs(Float.toInt(Float.fromInt(amountToMove) * env.revenueShare));
        })) {
            case (#Err e) {
                Debug.trap("transfer failed: " # debug_show(e));
            };
            case (#Ok _) {};
        };

```

`env.revenueShare == 0.05`.

---

<div class="post-metadata">

**Author:** ![ohsalmeron](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ohsalmeron/32/30099_2.png) [@ohsalmeron](https://forum.dfinity.org/u/ohsalmeron)\
**Post date:** [April 1, 2025, 3:19am UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235/2 "2025-04-01T03:19:28Z")

</div>

did you try hardcoding the value instead of using the .env?  
also could be, multiple data type conversions in Motoko often leads to these kind of errors

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [April 1, 2025, 4:07am UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235/3 "2025-04-01T04:07:07Z")

</div>

I use `env/lib.mo` module. That’s hardcoding.

“also could be, multiple data type conversions in Motoko often leads to these kind of errors” - no idea what you mean.

---

<div class="post-metadata">

**Author:** ![ohsalmeron](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ohsalmeron/32/30099_2.png) [@ohsalmeron](https://forum.dfinity.org/u/ohsalmeron)\
**Post date:** [April 1, 2025, 4:46am UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235/4 "2025-04-01T04:46:06Z")

</div>

I mean hardcoding the float, and with data conversions I mean that sometimes (or often times) you have a float that you need to convert to Nat or Int or viceversa. But idk I’m just guessing based on your code.

---

<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:** [April 3, 2025, 3:05pm UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235/5 "2025-04-03T15:05:16Z")

</div>

We trap with this error message when you attempt to do a lossy convertion, e.g Nat64.fromNat(n) when n is not in the correct range for a Nat64 [0 .. 2^64). Not sure which operation is raising it here though.

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [April 3, 2025, 9:17pm UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235/6 "2025-04-03T21:17:11Z")

</div>

Must be these two: `Float.toInt(Float.fromInt(...`.

Motoko has 64-bit `Float`s, with a 52bit mantissa. When the exponent is not 0, there are $2^ exponent$ possible integers to convert to. My hunch is that this is causing the trouble.

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [April 5, 2025, 3:00pm UTC](https://forum.dfinity.org/t/canister-called-ic0-trap-with-message-losing-precision/43235/7 "2025-04-05T15:00:03Z")

</div>

I’ve found the error. It was in another part of the program:

I was converting `Nat` to `Nat32` without ensuring 32-bit size.
