The Raspberry Pi Pico can be programmed in different ways, and the Python language is quite attractive. Code can be immediately typed at a prompt (known as a shell or console), or can be written in files and drag-and-dropped to the USB file storage drive letter that appears when you plug the Pi Pico into the PC.
The version/variant of Python that is installed on the Pi Pico can be changed at any time by also drag-and-dropping the Python firmware binary file onto the USB storage drive letter, but this time do it by first holding down the button that is on the Pi Pico, before plugging in the Pi Pico to the PC, and then release the button.
Unfortunately, the mathematic capabilities are quite limited with the Python variants available, which are CircuitPython and MicroPython. The pre-built ready-to-download CircuitPython and MicroPython firmware images are both configured for single-precision calculations. It is very easy for mathematics-related discrepancies to appear when using single-precision.
Here's an example with a downloaded (from the raspberrypi.com website) MicroPython image.
As can be seen from the screenshot above, very large numbers such as 5 x 10^38 cannot be represented properly.
The situation with CircuitPython is actually currently worse. The screenshot below shows the inaccuracy when calculating the remainder when dividing two large numbers. To be clear, these numbers are not very large in the engineering world. 845 x 10^6 happens to represent a phase locked loop (PLL) frequency, and 25 x 10^6 represents a cheap 25 MHz crystal.
From my perspective it's crippling having no sensible way to have double-precision in these Python variants; furthermore the maths capabilities ought to be a unique selling point of Python : )
Fortunately, it is easy to build a version of MicroPython with double precision capability. The information below is based on a very useful discussion here I have merely just added a few tiny bits to hopefully make it easy to follow.
Pre-built Double Precision MicroPython Firmware
Following the instructions below, I've created a binary file downloadable here that can be directly used. However, it won't be up-to-date forever, so the information below can be used at any time to create some new binaries (and please share them if you do build them, to save people time).
Setting up the PC to build MicroPython Firmware
On a Linux machine as root user (or prepend the text sudo to these lines) type the following:
apt-get update apt-get install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential
All the remainder commands below are executed as a normal user, not root/super-user.
Building a Release Version of MicroPython with Double Precision
Download the latest release source zip file to (say) ~/development/micropython
For example, type:
mkdir -p ~/development/micropython cd ~/development/micropython wget https://micropython.org/resources/source/micropython-1.17.zip
Unzip the file:
cd micropython-* make -C mpy-cross cd ports/rp2
Edit the file mpconfigport.h and around line 65, change from:
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
Basically, change the end of the line, from FLOAT) to DOUBLE) and then save the file.
To build the code, type:
rm -rf build mkdir build cd build cmake .. make
It should take a minute or so to complete, and the folder will contain (amongst other files) firmware.uf2
shabaz@u18-nuc:~/development/micropython/micropython-1.17/ports/rp2/build$ ls -l firm* -rwxrwxr-x 1 shabaz shabaz 289156 Oct 15 16:41 firmware.bin -rw-rw-r-- 1 shabaz shabaz 4238463 Oct 15 16:41 firmware.dis -rwxrwxr-x 1 shabaz shabaz 809608 Oct 15 16:41 firmware.elf -rw-rw-r-- 1 shabaz shabaz 1161543 Oct 15 16:41 firmware.elf.map -rw-rw-r-- 1 shabaz shabaz 813406 Oct 15 16:41 firmware.hex -rw-rw-r-- 1 shabaz shabaz 578560 Oct 15 16:41 firmware.uf2
That firmware.uf2 file can be renamed to something sensible, and then uploaded to the Pi Pico in the normal manner (i.e. hold down the button that is on the Pi Pico, and plug the USB connector to the PC, release the button, and then drag the .uf2 file to the USB file storage drive letter that appears.
mv firmware.uf2 rp2-pico-micropython-doubleprecision-1-17.uf2
Building Latest Snapshot version of MicroPython with Double Precision
Type the following:
mkdir -p ~/development/micropython-latest cd ~/development/micropython-latest git clone -b master https://github.com/micropython/micropython.git cd micropython git submodule update --init -- lib/pico-sdk lib/tinyusb make -C mpy-cross cd ports/rp2
Edit the file mpconfigport.h as described before. Then, type:
rm -rf build mkdir build cd build cmake .. make
As before, the folder will now contain the firmware.uf2 file.
Example Double Precision Output
Sticking with the same example as above, here is what appeared for me with the double-precision version of firmware:
With the latest snapshot version as of 25th October 2021, this is the output:
it is possible to get far more accurate maths capabilities with the Pi Pico and MicroPython, by re-building the MicroPython firmware image. Example firmware is available here.
Thanks for reading!