1

I have a quick question. Its rather simple but I am very confused. I am creating a timer in C using the terminal. I am using SIGALRM to send signals that subtracts the value of SUBTRACT_CONST. I am assuming that there are 25 TICKS_PER_SECOND or 1/25 which is 0.0400 my subtract constant. I have installed the signal handler and the timer to go off every 40000us set in the itimer struct. However, in one of my functions I cannot "catch" when the time becomes zero . When this TICK_PER_SECOND value is 32 or 16 it works as expected. My comparison does not seem to catch these different tick times. My time variable that is holding this value that's being subtracted from is a double.

  if(TIME == 0) //my very simple comparison
  • I've tried this already. My timer counts down into negative time. Once its becomes less than 0 this condition would always be met. – Samuel Magaña May 27 '17 at 20:12
  • 1
    Just use an `int`, count down from 25 in steps of 1. – harold May 27 '17 at 20:12
  • You just need to use the proper condition, you just can't use an exact comparison. For example, `if(TIME < 0.00001)...` or something like that. Or do what @harold said and solve your problem into integers. – lurker May 27 '17 at 20:14
  • Count down the number of ticks with an integer type, instead of the number of seconds with a floating point type (basically what @harold is suggesting). – Dmitri May 27 '17 at 20:27

1 Answers1

1

If you use a floating point number for the timer then maybe the only way is to use a condition like:

if(abs(TIME) < number )... 

where number is very small value like e-9 than means practically zero.

coder
  • 12,832
  • 5
  • 39
  • 53