Part 14 - double

Today we are going to handle the double data type. As we discussed, in the Pico there is no co-processor to handle floating-point numbers as this is handled through a series of functionality through software in the API. This is the same with double-precision.

Let's work with a simple example. 0x06_double.c as follows.

#include <stdio.h>
#include "pico/stdlib.h"

int main() 
{
  stdio_init_all();

  while(1) 
  {
    double x = 40.5;

    printf("%f\n", x); 

    sleep_ms(1000);
  }

  return 0;
}

Very simply we assign a float of 40.5 into x and print it with the %f _format modifier and then sleep for _1 second.

Let's make a new dir 0x06_double and add our CMakeLists.txt file in it.

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(test_project C CXX ASM)
set(CMAKE_C_STANDARD 11) 
set(CMAKE_CXX_STANDARD 17) 
pico_sdk_init()

add_executable(0x06_double
  0x06_double.c
)

pico_enable_stdio_usb(0x06_double 1)

pico_add_extra_outputs(0x056_double)

target_link_libraries(0x06_double pico_stdlib)

Next we need to copy the pico_sdk_import.cmake file from the external folder in the pico-sdk installation to the 0x06_double project folder.

cp ../pico-sdk/external/pico_sdk_import.cmake .

Finally we are ready to build.

mkdir build
cd build
export PICO_SDK_PATH=../../pico-sdk
cmake ..
make

Then simply copy the .uf2 file to the drive.

cp 0x06_double.uf2 /Volumes/RPI-RP2

Then we need to locate the USB drive so you can do the following.

ls /dev/tty.

Press tab to find the drive and then in my case I will use screen to connect.

screen /dev/tty.usbmodem0000000000001

You should see a an 40.5 being printed every second.

40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000
40.500000

In our next lesson we will debug.

results matching ""

    No results matching ""