What does this mean

Hi.

Newbie here. I have to do some software work for a module and I am using the Arduino uno. I have to use it to analyse a repeating waveform. I will not pretend I am using all my own work. I have used bits and pieces and my own stuff I am trying to understand a recurring line used many times in various frequency counting subroutines but I can not figure it out. Please can somebody explain it?

return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime);

I do not understand the “?” and the : infact I would like the who;e line explained! I think this wouls be good for other people to as alot of people have just shrugged shoulders!

Regards.

Alf 442

That’s a c syntax shortcut. It is functionally equivalent to:

if(numPulses < 3) {
  return 0;
}
else {
  return ((1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime)); 
}

Depending on the compiler, the assembler generated might be a little different especially after optimization, but functionally it’s the same.

Do you need the algebraic expression explained? It’s pretty straight forward except maybe for the (float) parts. Those are just converting the subexpressions to floating point representation. If you look, the variables following (float) are likely integers.

Thakyou uChip. I think that should be sufficient. I was getting frustrated because so many frequency counting articles use the line in the accompanying code and have lots of remarks for lines that seem pretty straight forward , then nothing for that one! I got the impression this line or very similiar is being used by many without knowing what it does:-) . Now I know I shall include comments on it. Thankyou again for your fast response.