Arithmatic over long and double numbers in nrf52840dk

Hi, I am working on nrf52840dk and have to do some arithmetic operations, namely, multiplication, square, mean and variance operations on sensor measurements. The code is compiling correctly but I am getting an infinite loop when running it. I have written code for other operations also but since it’s not running correctly for multiplication, so did not include the rest of the code here. Can someone check it out and let me know what am I doing wrong or is there an efficient way to write to code it?

#include <stdio.h>
#include <math.h>
#include "board.h"

int main(void)
{
  puts("KEY GENERATION BEGINS \n");  //
  // array length
  short num = 32;
  // declare variables
  long arr1XSqr[num], arr1YSqr[num], arr1ZSqr[num];
  long arr2XSqr[num], arr2YSqr[num], arr2ZSqr[num];
   //
   // first arr  //
   // arr1 -X
   long arr1X[] = {-9, -9, -55, -2, 5, -7, -56, -1,
                   -7, -56, 0, 5, -7, -56, 0, 4,
                   -8,-54, -1, 5, -10, -56, -1, 5,
                   -8,-54, -1, 5, -8, -55, -1, 5};
   // arr1- Y
   long arr1Y[] = {926, 926, 880, 925, 912, 932, 880, 925,
                   925, 880, 925, 912, 923, 880, 924, 912,
                   924, 880, 924, 913, 926, 881, 925, 913,
                   924, 879, 925, 912, 924, 880, 925, 914};
   // arr1 -Z
   long arr1Z[] = {-64, -64, -232, 35, 8, -65, -231, 36,
                   -67, -232, 34, 7, -66, -232, 34, 7,
                   -65, -231, 36, 8, -62, -232, 36, 8,
                   -67, -231, 33, 6,-65, -231, 34, 9};
   //
   // second arry //
   // Arr2- x
   long arr2X[]  = {-1025, -1025, -273, 1189, 878, -561, -180, 1134,
                   -828, -504, 1190, 994, -984, -502, 1131, 682,
                   -727, -343, 1107, 894, -839, -191, 1148, 835,
                    -558, -303, 1159, 696, -974, -249, 1128, 834};
   // arr2- y
   long arr2Y[]  = {-916, -916, -58, -927, 8, -1426, -235, -915,
                   -1249, -325, -941, 122, -988, -325, -933, 264,
                   -1395, 109, -1180, -17, -1266, -216, -917, 0,
                   -1440, 110, -915, 273, -997, 101, -772, -916};
   // arr2- z
   long arr2Z[]  = {1120, 1120, 255, -300, -155, 724, -52, -300,
                   945, -180, -299, 12, 1121, -183, -300, 175,
                   1035, 258, -596, -168, 1014, -41, -300, -156,
                   700, 257, -300, 179, 1106, 255, -299, -157};

   for (int i = 0; i < num; i++) {
     // take square all variables
     // arr1
     arr1XSqr[i] = arr1X[i] * arr1X[i];
     arr1YSqr[i] = arr1Y[i] * arr1Y[i];
     arr1ZSqr[i] = arr1Z[i] * arr1Z[i];
     //
     // arr2
     arr2XSqr[i] = arr2X[i] * arr2X[i];
     arr2YSqr[i] = arr2Y[i] * arr2Y[i];
     arr2ZSqr[i] = arr2Z[i] * arr2Z[i];
   }
    puts("========> squaring done <========");
    printf("first element %ld \n", (arr2XSqr[0]+ arr2YSqr[0] + arr2ZSqr[0]));
    printf("first element %ld \n",(arr1XSqr[0]+ arr1YSqr[0] + arr1ZSqr[0]) );

  return 0;
}

those arrays take 768 bytes on stack, which is too much for the default stack size. Either move them to file scope, or enlarge the main stack, e.g., by adding this to your makefile:

CFLAGS += -DTHREAD_STACKSIZE_MAIN=3*THREAD_STACKSIZE_LARGE
1 Like

Thanks. Its working.