# Floats from bytes in Motoko

**URL:** https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595
**Category:** Language Support
**Tags:** Motoko
**Created:** [July 28, 2022, 2:14pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595 "2022-07-28T14:14:58Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)
#### Post date: [July 28, 2022, 2:14pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/1 "2022-07-28T14:14:58Z")

</div>

I am currently trying to convert a byte array to a Float (16, 32 and 64 bit) without success. I understand the binary structure of a float but not sure how to translate it in motoko.  
I don’t see any methods in the Float base module that would help and I’m not even sure where to look. Doesn’t seem like bitshifting out the components will help either but I could be missing something.  
Any help would be great

---

<div class="post-metadata">

### Author: ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)
#### Post date: [July 29, 2022, 5:17am UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/2 "2022-07-29T05:17:31Z")

</div>

I think I figured it out using bitshifting, needed to approach it from a different angle

```auto

    public func decodeFloat(bytes: [Nat8]) : ?Float {
        var bits: Nat64 = Binary.BigEndian.toNat64(bytes);
        let (exponentBitLength: Nat64, mantissaBitLength: Nat64) = switch(bytes.size()) {
            case (2) {
                (5: Nat64, 10: Nat64);
            };
            case (4) {
                (8: Nat64, 23: Nat64);
            };
            case (8) {
                (11: Nat64, 52: Nat64);
            };
            case (a) return null; 
        };
        // Bitshift to get mantissa, exponent and sign bits
        let mantissaBits: Nat64 = bits & (Nat64.pow(2, mantissaBitLength) - 1);
        let exponentBits: Nat64 = (bits >> mantissaBitLength) & (Nat64.pow(2, exponentBitLength) - 1);
        let signBits: Nat64 = (bits >> (mantissaBitLength + exponentBitLength)) & 0x01;

        // Convert bits into numbers
        let e: Int64 = Int64.pow(2, Int64.fromNat64(exponentBits) - ((Int64.fromNat64(Nat64.pow(2, exponentBitLength) / 2)) - 1));
        let maxOffsetInverse: Float = Float.pow(2, Float.fromInt64(Int64.fromNat64(mantissaBitLength)) * -1);
        let m: Float = 1.0 + (Float.fromInt64(Int64.fromNat64(mantissaBits)) * maxOffsetInverse);

        var floatValue: Float = Float.fromInt64(e) * m;

        // Make negative if sign bit is 1
        if (signBits == 1) {
            floatValue := Float.mul(floatValue, -1.0);
        };
        
        ?floatValue;
    }

```

BUT im still having issues with things like INFINITY and NaN, which dont seem to exist in Motoko.  
Anyone have thoughts on those? not sure if it matters

---

<div class="post-metadata">

### Author: ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)
#### Post date: [July 29, 2022, 6:39pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/3 "2022-07-29T18:39:03Z")

</div>

Curious why you cannot pass `Float` directly as argument?

---

<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: [July 29, 2022, 8:23pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/4 "2022-07-29T20:23:56Z")

</div>

He’s building CBOR and Candid Interpreters for Motoko so we can use http\_outgoing\_request from a canister to call services on the IC use t-ECDSA signatures. 😬

---

<div class="post-metadata">

### Author: ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)
#### Post date: [July 29, 2022, 9:19pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/5 "2022-07-29T21:19:28Z")

</div>

Okay, then you can use `from_candid(blob) : ?Float`. Append the blob with prefix `DIDL\00\01\72` to indicate this is a float64 value.

---

<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: [July 29, 2022, 9:33pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/6 "2022-07-29T21:33:55Z")

</div>

Is there a list of those prefixes somewhere?

---

<div class="post-metadata">

### Author: ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)
#### Post date: [July 29, 2022, 9:43pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/7 "2022-07-29T21:43:46Z")

</div>

Yes, it’s in the Candid spec: [candid/Candid.md at master · dfinity/candid · GitHub](https://github.com/dfinity/candid/blob/master/spec/Candid.md#binary-format)

---

<div class="post-metadata">

### Author: ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)
#### Post date: [July 29, 2022, 10:15pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/8 "2022-07-29T22:15:37Z")

</div>

I wonder how hard it would be to expose that float parsing functionality to Motoko itself

---

<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: [August 28, 2022, 12:11am UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/9 "2022-08-28T00:11:11Z")

</div>

Generally the RTS can do a lot of stuff (it’s written in Rust). But it is a delicate game to only include things that are relevant for the broad community, as bloat will weigh heavy on every installed canister.

---

<div class="post-metadata">

### Author: ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)
#### Post date: [August 30, 2022, 10:12pm UTC](https://forum.dfinity.org/t/floats-from-bytes-in-motoko/14595/10 "2022-08-30T22:12:52Z")

</div>

Because I was implementing byte encoding libraries, I went ahead and made a library for number encoding/extensions for Nat, Int and Float

> **[GitHub - Gekctek/motoko\_numbers: An extension to numbers in motoko](https://github.com/Gekctek/motoko_numbers)**
>
> An extension to numbers in motoko. Contribute to Gekctek/motoko\_numbers development by creating an account on GitHub.
