# Micropython math weirdness

**URL:** <https://forums.pimoroni.com/t/micropython-math-weirdness/16886>\
**Category:** Projects\
**Created:** [April 14, 2021, 11:34am UTC](https://forums.pimoroni.com/t/micropython-math-weirdness/16886 "2021-04-14T11:34:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stuG](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/stug/32/7789_2.png) [@stuG](https://forums.pimoroni.com/u/stuG)\
**Post date:** [April 14, 2021, 11:34am UTC](https://forums.pimoroni.com/t/micropython-math-weirdness/16886/1 "2021-04-14T11:34:10Z")

</div>

Hi,

I’m troubleshooting button input problem; the following code:  
print(math.log2(16384))  
print(int(math.log2(16384)))  
print(math.log2(32768))  
print(int(math.log2(32768)))

produces the following output:  
14.0  
14  
15.0  
14

Can anybody tell me why the log2(32768) is not being correctly converted to an int?

Cheers,  
Stu

---

<div class="post-metadata">

**Author:** ![major\_tomm](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/major_tomm/32/9044_2.png) [@major\_tomm](https://forums.pimoroni.com/u/major_tomm)\
**Post date:** [April 15, 2021, 6:46pm UTC](https://forums.pimoroni.com/t/micropython-math-weirdness/16886/2 "2021-04-15T18:46:46Z")

</div>

My guess would be that `int()` just truncates the floating point value, i.e. drops anything after the decimal place.

It could be that `math.log2(32768)` returns a value slightly less than `15.0` but when printed it comes out as `15.0`.

Floating points are tricky - you could try rounding the number prior to converting it to an integer but it might not do anything?

---

<div class="post-metadata">

**Author:** ![major\_tomm](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/major_tomm/32/9044_2.png) [@major\_tomm](https://forums.pimoroni.com/u/major_tomm)\
**Post date:** [April 15, 2021, 6:57pm UTC](https://forums.pimoroni.com/t/micropython-math-weirdness/16886/3 "2021-04-15T18:57:31Z")

</div>

This code snippet:

```python
import math
v = math.log2(32768)
print(v)
print("{:.10f}".format(v))
print("{:.10f}".format(round(v)))
print(int(round(v)))

```

Outputs:

```auto
15.0
14.9999990463
15.0000000000
15

```

And seems to confirm what I suspected.

---

<div class="post-metadata">

**Author:** ![kjw](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kjw](https://forums.pimoroni.com/u/kjw)\
**Post date:** [April 16, 2021, 1:37pm UTC](https://forums.pimoroni.com/t/micropython-math-weirdness/16886/4 "2021-04-16T13:37:36Z")

</div>

Same thing in CircuitPython, you’ve got `round()` if that works for you:

```auto
Adafruit CircuitPython 6.2.0 on 2021-04-05; Adafruit Feather nRF52840 Express with nRF52840
>>> print(math.log(32768, 2))
15.0
>>> print(int(math.log(32768, 2)))
14
>>> print(round(math.log(32768, 2)))
15

```

There are often issues like this relating to the finite precision and the underlying representation of floating-point values but some MicroPythons will also suffer from bias in the internal rounding depending on how they store FP internally, see [Adafruit Forums: CircuitPython floating point accuracy looks poor for 32bit](https://forums.adafruit.com/viewtopic.php?f=60&t=137002&hilit=truncation).

---

<div class="post-metadata">

**Author:** ![stuG](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/stug/32/7789_2.png) [@stuG](https://forums.pimoroni.com/u/stuG)\
**Post date:** [April 16, 2021, 2:54pm UTC](https://forums.pimoroni.com/t/micropython-math-weirdness/16886/5 "2021-04-16T14:54:11Z")

</div>

Thanks ya’ll. I suspected as much, and I’m intrigued by the fact that the same code produces correct results on a MacBook.

Ended up removing the use of `log2()` to keep it nice and integral.  
Happy weekends!

---

<div class="post-metadata">

**Author:** ![hoorem95](https://avatars.discourse-cdn.com/v4/letter/h/5e9695/32.png) [@hoorem95](https://forums.pimoroni.com/u/hoorem95)\
**Post date:** [November 4, 2026, 6:20pm UTC](https://forums.pimoroni.com/t/micropython-math-weirdness/16886/6 "2026-11-04T18:20:45Z")

</div>

> [@kjw](#):
>
> Same thing in CircuitPython, you’ve got `round()` if that works for you:
> 
> ```python-repl
> Adafruit CircuitPython 6.2.0 on 2021-04-05; Adafruit Feather nRF52840 Express with nRF52840
> >>> print(math.log(32768, 2))
> 15.0
> >>> print(int(math.log(32768, 2)))
> 14
> >>> print(round(math.log(32768, 2)))
> 15
> 
> ```
> 
> There are often issues like this relating to the finite precision and the underlying representation of floating-point [values](https://laacalculadoraalicia.es/)but some MicroPythons will also suffer from bias in the internal rounding depending on how they store FP internally, see [Adafruit Forums: CircuitPython floating point accuracy looks poor for 32bit](https://forums.adafruit.com/viewtopic.php?f=60&t=137002&hilit=truncation).

Its really helpfull for me at that time.
