invalid operands `int' and `double' to binary `operator %'Code:double Sleep(rand()%0.1);
I get this error when I compile this so how do I use it corectly and also what does the % sign mean or y do i haft to use it ?
Printable View
invalid operands `int' and `double' to binary `operator %'Code:double Sleep(rand()%0.1);
I get this error when I compile this so how do I use it corectly and also what does the % sign mean or y do i haft to use it ?
% is modulus.
What are you trying to do?
im trying to use 0.1 in Sleep(rand()%1); witch that code does work.
Modulus (the % sign) finds the remainder when the first number in the expression is divided by the second number. It only works on integers, for floating point numbers I think theres a function that does this called fmod(), but I'm not sure as I have never used this.
Are you calling a sleep function to make your program pause for a random amount of time? Time is usually measured in millisecs so you might be able to do something like:
to pause your prog for a random amount of time up to 5 seconds.Code:Sleep(rand()%5000);
ya i have that code working and thank you vary much i can atleast do some googling and maby figure out what i need
i need a % of a millisecs like 0.4 and thats not working.
You would need to use fmod for this. Heres an example of how to use it:
http://www.cprogramming.com/fod/fmod.html
Hope that helps.
[edit]I'm not sure if you can make a delay less than one millisec tho[/edit]
Sleep works on a millisecond basis since it takes a DWORD. You can't sleep less than 1ms and even if you try to sleep 1ms, you're probably going to sleep longer anyway, so there's no point.
ok thanks to all of you I understand this now : )
hay wout would you guys say the best looping procedure is I here that goto loop; is hard to use curectly
Depends on the situation. You have for, while and do...while. Show some sample code and we could help more.
Jordan,
Exactly what are you trying to do? Why do you want to make a short delay?
I'm asking, because what you're trying to do is probably possible, but requires a bit more information to understand what you actually want to do.
By the way, in Linux there's a function "usleep" which takes a microsecond count. But that doesn't mean that if you give it a 1000 count, that the process will sleep for exactly 1ms - it may well sleep for 10ms [even if there's no other process to run on the system - if there is, there's no saying when your process will wake up, it could be 50 or 100 ms later].
Windows also have the same principles at the base - if a process goes to sleep, there's no guarantee about when it will wake up, other than it will be AT LEAST as much as you specified later - but it could be 1ms more or 100ms more than you specified.
If you want proper guarantees about what executes when. you need a real-time OS.
--
Mats
What about a timer? Is that as unprecise as Sleep too?
matsp: what about the HPET-chip? you can program it to callback a function after a specified amount of time. Same is true for the RTC. Both have defined resolution so it should work reliable.
That seems illogical, at least on Windows since threads aren't scheduled that way.
Threads are dynamically assigned time slices depending on remaining time over from higher threads. There's just no way it will be able to call back in the specified time unless the thread has a time slice.
I think Elysia is on the right track. A "timer callback" will not be issued until it's time to schedule the thread. If your thread is higher priority than the currently running one, then it will happen immediately as the kernel reschedules [which is part of the returning from an interrupt].
I just found this function: timeBeginPeriod()
http://msdn2.microsoft.com/en-us/library/ms713413.aspx
It appears that you can change the timer behaviour in Windows with this call.
--
Mats
I believe the timer allows the kernel to reschedule threads every X ms specified, if I understand it correctly. However, you can't force it to reschedule on periods less than 1 ms. So it still won't allow you "sleep" half a millisecond or so.
No, and it would be terribly inefficient to run the scheduler [even if it doesn't reschedule, it still has to update the stats for each process] 2000 times a second, don't you think?
I also believ the DEFAULT timer tick is 10ms, so you get 10x better precision.
--
Mats