# Is there any way to get square root of Nat numbers bigger than 64 bits?

**URL:** https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977
**Category:** Developers
**Created:** [July 19, 2021, 6:15am UTC](https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977 "2021-07-19T06:15:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ayazgv](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayazgv/32/1587_2.png) [@ayazgv](https://forum.dfinity.org/u/ayazgv)
#### Post date: [July 19, 2021, 6:15am UTC](https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977/1 "2021-07-19T06:15:22Z")

</div>

Hello Dear All!

Is there any way to get square root of Nat numbers bigger than 64 bits?  
I tried this but it produces error `canister trapped explicitly: losing precision`

```auto
var num = 112345678912345678900 * 112345678912345678900; // OK
var ftval = Float.fromInt(num); // <- error
var sqr = Float.sqrt(ftval);

```

---

<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 19, 2021, 7:11am UTC](https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977/2 "2021-07-19T07:11:29Z")

</div>

Yes, that is rather unhelpful. I created an [issue](https://github.com/dfinity/motoko-base/issues/272).

Until that’s fixed, you’ll have to use your own conversion routine, e.g.:

```auto
let base = 0x1000_0000_0000_0000;
func nontrappingNatToFloat(n : Nat) : Float {
    var m : Nat = n;
    var x : Float = 0;
    var p : Float = 1;
    while (m > 0) {
        x += p * Float.fromInt(m % base);
        p *= Float.fromInt(base);
        m /= base;
    };
    return x;
};

```

---

<div class="post-metadata">

### Author: ![ayazgv](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayazgv/32/1587_2.png) [@ayazgv](https://forum.dfinity.org/u/ayazgv)
#### Post date: [July 19, 2021, 7:41am UTC](https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977/3 "2021-07-19T07:41:37Z")

</div>

@rossberg Thank you for your reply!

But the routine produces different result than Nat version

```auto
var num = 112345678912345678900 * 112345678912345678900; // OK
Debug.print("Nat num: " # _Nat.toText(num));
var newFt = nontrappingNatToFloat(num);
Debug.print("Flt num: " # _Float.toText(newFt));

```

Results:

```auto
Nat num: 12621551570275872565156226207501905210000
Flt num: 12621551570275871845290248787080326938624.000000

```

---

<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 19, 2021, 8:44am UTC](https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977/4 "2021-07-19T08:44:57Z")

</div>

That’s expected. 64 bit floats only have a mantissa precision of 53 bits, which corresponds to roughly 16 decimal digits. So you get rounding errors when you convert larger integers. See [here](https://floating-point-gui.de) for more background. Floats are inappropriate if you need exact precision.

---

<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: [September 7, 2021, 9:16am UTC](https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977/5 "2021-09-07T09:16:08Z")

</div>

It took its time, but after [https://github.com/dfinity/motoko-base/pull/281](https://github.com/dfinity/motoko-base/pull/281) has been merged, the _next_ release of Motoko will have non-trapping `Float` to `Int` (and back) conversions, as inherited from the underlying `LibTomMath` library. Still, many caveats apply, as round-tripping is lossy on many levels, and `NaN`s and `Inf`inities will probably have surprising behaviours.

Addendum: `moc` `0.6.9` is now released, Next up: `dfx`.

---

<div class="post-metadata">

### Author: ![goose](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/goose/32/6164_2.png) [@goose](https://forum.dfinity.org/u/goose)
#### Post date: [September 8, 2021, 1:39am UTC](https://forum.dfinity.org/t/is-there-any-way-to-get-square-root-of-nat-numbers-bigger-than-64-bits/5977/6 "2021-09-08T01:39:36Z")

</div>

I use Newton iterative method to implement the code as follows：

```auto
 public func sqrt(x : Nat) : Nat {
        if (x == 0) {
           return 0;
        };

        var pre : Int = 0;
        var cur : Int = 1;

        loop {
            pre := cur;
            cur := (cur + x/cur)/2;

            if (Int.abs(cur - pre) <= 1) {
                return Int.abs(cur);
            };
        } while(true);

        Int.abs(cur);
    };

```
