How can I remove precision from long variable

I am preparing the code for a POV LED clock and I'm having a little trouble implementing some of the timing. I currently have a line that checks if the time of rotation matches the expected time for a given position to flash an LED. Unfortunately the code takes time to execute, and rarely if ever lines up.

My question is as follows:

Say I have a long variable 123456789 how could I quickly and efficiently change that into 123456000 in order to check for an approximate equality.

I don't want to use any division to save crucial processor time but I can't seem to find a way to use any round function to get such a result.

Thanks in advance!

I don't know of a way that's going to be any faster than

long number= 123455789;
number = (number/1000)*1000;

Its hard to run a test as the serial prints will take longer to process then the sum does.

even with serial printing it less than 15ms (in set up one time it claims its less than 1ms)

if Mr gammon reads this he might know exactly the cost to process it.

andrewlumley:
Say I have a long variable 123456789 how could I quickly and efficiently change that into 123456000 in order to check for an approximate equality.

If you want to check two long numbers if they are approximately equal, you create the absolute value of the difference and compare against the interval.

Code for comparing a variable and 123456000 for "approximately equal" within an interval of 1000:

   if (labs(variable-123456000)<1000) Serial.println("approximately equal");

andrewlumley:
Say I have a long variable 123456789 how could I quickly and efficiently change that into 123456000 in order to check for an approximate equality.

Dont forget that 10000000 is approximately equal to 09999999...

Okay, so here's where I'm at. I first tried the technique suggested by jurs to check for equality within 100 microseconds. While this method worked, it still meant that this instance jumped around by about 1cm each time which isn't ideal.

Most recently I have been trying to implement a timed interrupt to automatically pulse the LED's every period/4 microseconds for example. This is still in progress.

The proof is in the pudding test

uint32_t start;
uint32_t stop;

void setup()
{
  Serial.begin(115200);
  Serial.println("Start ");

  volatile uint32_t r = random();
  uint32_t p = 0;

  start = micros();
  for (int i = 0; i < 1000; i++)
  {
    if ((900 < r) && (r < 1100)) p++;
  }
  stop = micros();
  Serial.println(stop - start);

  start = micros();
  for (int i = 0; i < 1000; i++)
  {
    if (labs(r - 1000) < 100) p++;
  }
  stop = micros();
  Serial.println(stop - start);
}

void loop()
{
}

Start
1612
788

so the double compare slower as the labs() method.
Both loops have the same overhead, only difference is the compare method.
QED

andrewlumley:
Okay, so here's where I'm at. I first tried the technique suggested by jurs to check for equality within 100 microseconds. While this method worked, it still meant that this instance jumped around by about 1cm each time which isn't ideal.

Most recently I have been trying to implement a timed interrupt to automatically pulse the LED's every period/4 microseconds for example. This is still in progress.

maybe time to post your whole sketch?

Another test for approx equality can be done with shifts (but it has its limits)

if ((value + 128) >> 8 == someThresHold ) // means the first 22 bits of value equals someThresHold.
(approx equal performance as the labs() )

Instead of rounding down to the nearest 1000, it might be better to try rounding to the nearest 1024. This is because the processor works in binary, and 1024 is a round number in binary.

1024 (decimal) equals 10000000000 (binary)

If you have a variable x,then (x >> 10) means "what is left after removing the last ten bits from x".

If you don't know what binary is, now would be a good time to read up on it.

Oh, and if you really want to divide by 1000 and not by 1024, I think there are tricks for that, involving bit shifts and addition.