Search code examples
cfibonaccifreertoslpc

Fibonacci series incorrectly


This program should print the first 50 numbers of the Fibonacci series. But only the first 47 prints correctly, the last three prints incorrect.

 /*FreeRTOS.org includes.*/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "stdint.h"
#include "basic_io.h"

static void vSenderTask( void *pvParameters );
static void vReceiverTask( void *pvParameters );
int long resultado;

xQueueHandle xQueue;

int main( void )
{
    xQueue = xQueueCreate( 10, sizeof( long ) );

    if( xQueue != NULL )
    {
        xTaskCreate( vSenderTask, "Sender1", 240, ( long * ) resultado, 2, NULL );
        xTaskCreate( vReceiverTask, "Receiver", 240, ( long * ) resultado, 1, NULL );
        vTaskStartScheduler();
    }
    else
    {
    }
    for( ;; );
    return 0;
}

/*---------------------------------------------------------------------------------------------*/

static void vSenderTask( void *pvParameters )
{
    portBASE_TYPE xStatus;
    const portTickType xTicksToWait = 1000 / portTICK_RATE_MS;
    long s =1;
    long r=0;
    int  k=1;
    uint64_t resultado;
    resultado = ( long ) pvParameters;
    for ( ;; ){
        for (k; k<=61; k++) {
            resultado= s+r;
            s=r;
            r=resultado;

            xStatus = xQueueSendToBack( xQueue, &resultado, xTicksToWait );
        }
        if( xStatus != pdPASS )
        {
            vPrintString( "Could not send to the queue.\n" );
        }
    }
}

/*---------------------------------------------------------------------------------------------*/

static void vReceiverTask( void *pvParameters )
{
    uint64_t resultado;
    portBASE_TYPE xStatus;

    for( ;; )
    {
        if( uxQueueMessagesWaiting( xQueue ) != 10 )
        {
            vPrintString( "Queue should have been full!\n" );
        }

        xStatus = xQueueReceive( xQueue, &resultado , 0 );
        if( xStatus == pdPASS )
        {
            vPrintStringAndNumber( "Received = ", resultado );
        }
        else
        {
            vPrintString( "Could not receive from the queue.\r\n" );
        }
    }
}

/*---------------------------------------------------------------------------------------------*/
void vApplicationMallocFailedHook( void )
{
    /* This function will only be called if an API call to create a task, queue
    or semaphore fails because there is too little heap RAM remaining. */
    for( ;; );
}
/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
    /*This function will only be called if a task overflows its stack.  Note
    that stack overflow checking does slow down the context switch
    implementation. */
    for( ;; );
}
/*-----------------------------------------------------------*/

void vApplicationIdleHook( void )
{
    /* This example does not use the idle hook to perform any processing. */
}
/*-----------------------------------------------------------*/

void vApplicationTickHook( void )
{
    /* This example does not use the tick hook to perform any processing. */
}

The output is:

Received =  1
Received =  1
Received =  2
Received =  3
Received =  5
Received =  8
Received =  13
Received =  21
Received =  34
Received =  55
Received =  89
Received =  144
Received =  233
Received =  377
Received =  610
Received =  987
Received =  1597
Received =  2584
Received =  4181
Received =  6765
Received =  10946
Received =  17711
Received =  28657
Received =  46368
Received =  75025
Received =  121393
Received =  196418
Received =  317811
Received =  514229
Received =  832040
Received =  1346269
Received =  2178309
Received =  3524578
Received =  5702887
Received =  9227465
Received =  14930352
Received =  24157817
Received =  39088169
Received =  63245986
Received =  102334155
Received =  165580141
Received =  267914296
Received =  433494437
Received =  701408733
Received =  1134903170
Received =  1836311903
Received =  2971215073
Received =  512559680
Received =  3483774753
Received =  3996334433

Solution

  • On your system, long is 32 bits. The largest positive integer it can hold is 4294967295. But 1836311903 + 2971215073 = 4807526976, which is larger, so it overflowed and you got 512559680 (which is 4807526976 - 4294967296). If you want to go past the 47th Fibonacci number, you'll need a larger datatype or do multi-precision arithmetic.