# Problems with a simple floating point adder overlay on PYNQZ-2

**URL:** <https://discuss.pynq.io/t/problems-with-a-simple-floating-point-adder-overlay-on-pynqz-2/694>\
**Category:** Support\
**Created:** [November 25, 2019, 8:41am UTC](https://discuss.pynq.io/t/problems-with-a-simple-floating-point-adder-overlay-on-pynqz-2/694 "2019-11-25T08:41:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumamon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/kumamon/32/533_2.png) [@kumamon](https://discuss.pynq.io/u/kumamon)\
**Post date:** [November 25, 2019, 8:41am UTC](https://discuss.pynq.io/t/problems-with-a-simple-floating-point-adder-overlay-on-pynqz-2/694/1 "2019-11-25T08:41:30Z")

</div>

I have made a simple adder on Vivado HLS with float input and outputs.

![float%20adder](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/1X/1d46436495de71a55e0917a43d21424469f9f299.jpeg)

On Jupyter Notebooks in PYNQ-Z2, I get the following errors.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/1X/d69d5ee56a71671c5e5c6e8abf5c28ea18873632.png)

How does one fix this problem?

Also, I am thinking of using Python to multiply inputs by some 10^n, write to the IP and dividing the outputs by 10^n. Is this inefficient when working with a larger set of data with a huge decimal place?

---

<div class="post-metadata">

**Author:** ![cathalmccabe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/cathalmccabe/32/3348_2.png) [@cathalmccabe](https://discuss.pynq.io/u/cathalmccabe)\
**Post date:** [November 26, 2019, 9:44am UTC](https://discuss.pynq.io/t/problems-with-a-simple-floating-point-adder-overlay-on-pynqz-2/694/2 "2019-11-26T09:44:04Z")

</div>

As you noticed, MMIO doesn’t support float. You need to manually ‘pack’ your float into something that can be written to the IP, and do the reverse if you need to read back the result.

You can use these types of techniques:  
[https://stackoverflow.com/questions/23624212/how-to-convert-a-float-into-hex](https://stackoverflow.com/questions/23624212/how-to-convert-a-float-into-hex)

Cathal

---

<div class="post-metadata">

**Author:** ![jmlaniel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/jmlaniel/32/3492_2.png) [@jmlaniel](https://discuss.pynq.io/u/jmlaniel)\
**Post date:** [September 8, 2022, 11:46pm UTC](https://discuss.pynq.io/t/problems-with-a-simple-floating-point-adder-overlay-on-pynqz-2/694/3 "2022-09-08T23:46:52Z")

</div>

I have tried the same thing with my PYNQ-Z2 using v2.7. I didn’t get the MMIO error. Instead, the adder returned simply c = 0, no matter the value that I send as a or c.

I knew, as Cathal mentioned before, that I needed to package the float number as an u32 integer IEEE 754 standard fro expressing floating-point numbers.

I found a solution on the web that allow conversion of floating-point value in Python to integer-type numbver compatible with IEEE 754 standard (I would like to give a reference, but I lost it). Here is the code to send values to a and b, then read the result in c where a, b and c are all floating-point numbers in Python:

```auto
add_ip.write(0x10, struct.unpack('I', struct.pack('f', a))[0])
add_ip.write(0x18, struct.unpack('I', struct.pack('f', b))[0])
c = struct.unpack('f', struct.pack('I', add_ip.read(0x20)))[0]

```

Hope this help!

---

<div class="post-metadata">

**Author:** ![marioruiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/marioruiz/32/2177_2.png) [@marioruiz](https://discuss.pynq.io/u/marioruiz)\
**Post date:** [September 9, 2022, 6:04am UTC](https://discuss.pynq.io/t/problems-with-a-simple-floating-point-adder-overlay-on-pynqz-2/694/4 "2022-09-09T06:04:27Z")

</div>

In this post, there is also a solution

> [@Unable to read back floating-point values when using MMIO read](https://discuss.pynq.io/t/unable-to-read-back-floating-point-values-when-using-mmio-read/2519/2):
>
> There are a couple of ways of reading/writing floats. One is convert to/from an integer in your code: f = struct.unpack('f', struct.pack('I', device.read(0x20)))[0] Alternatively you can cast the entire address space to an array of float and access the registers through that: equivalently ar = device.mmio.array.view('f') f = ar[0x8] Note that in this second way you need use indexes in the array rather than addresses. You should also do something similar for writing floating point values - .…

Mario

---

<div class="post-metadata">

**Author:** ![marioruiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/marioruiz/32/2177_2.png) [@marioruiz](https://discuss.pynq.io/u/marioruiz)\
**Post date:** [September 9, 2022, 6:04am UTC](https://discuss.pynq.io/t/problems-with-a-simple-floating-point-adder-overlay-on-pynqz-2/694/5 "2022-09-09T06:04:31Z")

</div>


