Error in calculation

//Defining the variables, XYZ (capitalised were defined as floats as well but as a parameter in the function)

float x, y, z;
  float Total = X + Y + Z;
  x = X / Total;
  y = Y / Total;
  z = Z / Total;

Hello, I have this bit of maths that I'm trying to do but it gives m the following error:

C:\Users\Nneka\Documents\Arduino\IRP\IRP_MainCode\IRP_MainCode.ino: In function 'void RGBtoCIElab(uint8_t*, uint8_t*, uint8_t*, float*, float*, float*, double*, double*, double*)':
C:\Users\Nneka\Documents\Arduino\IRP\IRP_MainCode\IRP_MainCode.ino:164:19: error: invalid operands of types 'float*' and 'float*' to binary 'operator+'
   float Total = X + Y + Z;
                 ~~^~~
C:\Users\Nneka\Documents\Arduino\IRP\IRP_MainCode\IRP_MainCode.ino:165:9: error: invalid operands of types 'float*' and 'float' to binary 'operator/'
   x = X / Total;
       ~~^~~~~~~
C:\Users\Nneka\Documents\Arduino\IRP\IRP_MainCode\IRP_MainCode.ino:166:9: error: invalid operands of types 'float*' and 'float' to binary 'operator/'
   y = Y / Total;
       ~~^~~~~~~
C:\Users\Nneka\Documents\Arduino\IRP\IRP_MainCode\IRP_MainCode.ino:167:9: error: invalid operands of types 'float*' and 'float' to binary 'operator/'
   z = Z / Total;
       ~~^~~~~~~

exit status 1

Compilation error: invalid operands of types 'float*' and 'float*' to binary 'operator+'

This is the entire function for reference:

void RGBtoCIElab(uint8_t *r,uint8_t *g,uint8_t *b, float *X, float *Y, float *Z, double *CIEL, double *CIEa, double *CIEb) {

  //Converting the RGB values to XYZ trimstimulus counterparts

  float fracX, fracY, fracZ;
  float Fx, Fy, Fz;
  float x, y, z;

  // turning the 0-255 values to 0-1

  *r /= 255;
  *g /= 255;
  *b /= 255;

  //Continuity check

  Serial.print("value between 0 - 1 R: "); Serial.print(*r); Serial.print(" ");
  Serial.print("G: "); Serial.print(*g); Serial.print(" ");
  Serial.print("B: "); Serial.print(*b); Serial.print(" ");
  Serial.println(" ");

  // Using the matrix calculation found in the manufacturer datasheet to find the XYZ trimstimulus values

  *X = (-0.14282F * *r) + (1.54924F * *g) + (-0.95641F * *b);
  *Y = (-0.32466F * *r) + (1.57837F * *g) + (-0.73191F * *b);
  *Z = (-0.68202F * *r) + (0.77073F * *g) + (0.56332F * *b);

  // Converting the XYZ trimstimulus values to xyz chromacities values
  
  float Total = X + Y + Z;
  x = X / Total;
  y = Y / Total;
  z = Z / Total;

  // Going from CIExyz to CIELab

  fracX = *X/Xn;
  fracY = *Y/Yn;
  fracZ = *Z/Zn;

  if (fracX > (6/29)^3) {
    Fx = pow(fracX, 1/3);
  } 
  else (fracX <= (6/29)^3); {
    Fx = ((841/108)*(fracX)) + (4/29);
  }


  if (fracY > (6/29)^3) {
    Fy = pow(fracY, 1/3);    
  }
  else (fracY <= (6/29)^3); {
    Fy = ((841/108)*(fracY)) + (4/29);
  } 

  if (fracZ > (6/29)^3) {
    Fz = pow(fracZ, 1/3);
  }
  else (fracZ <= (6/29)^3); {
    Fz = ((841/108)*(fracZ)) + (4/29);
  }


  *CIEL = (116 * Fy) - 16;
  *CIEa = 500 * (Fx - Fy);
  *CIEb = 200 * (Fy - Fz);

}

X, Y and Z are pointers to floats. Not floats.

You likely wanted:

  float Total = *X + *Y + *Z;
  x = *X / Total;
  y = *Y / Total;
  z = *Z / Total;

Also, the r, g, b values returned during the continuity check are 0 which is an issue. I assume it's due to the data types used but I'm not sure what to use if not floats.

I see thank you!

is there a way to make it return the decimal value?

r value is taken from the output of a sensor which is why I've given it the data type uint8_t but would it be fine to just add an additional line changing it to a float or will this give me an error?

Also, when you divide an integer by an integer, the result is an integer. So expressions like 1/3 for example, evaluate to 0, not 0.333333. You might want to look at your code and think about that.

Thank you! I've changed that all around and now I'm getting values out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.