I am trying to separate two doubles of equal value by the minimum amount. The context is an event simulation. I don't want events to occur at the same time, so I increment the time that a new event is set to occur, by a minimal amount.
(This happens annoyingly often (self-implemented RNG), so I actually need to probe until I find a time when there isn't an event occurring.)
Currently, my code wants to do something like this (not tested, treat as pseudocode):
typedef struct event
{
double time;
struct event* nexteventinqueue;
} event;
//insert newevent into the queue:
void add(event *newevent)
{
static event *firsteventinqueue = NULL;
if(firsteventinqueue == NULL)
{
firsteventinqueue = newevent;
return;
}
event *currentevent = firsteventinqueue;
event *temp;
//find an event point in the queue that does not precede the new event.
while((currentevent->time) < (newevent -> time))
{
temp = currentevent;
currentevent = currentevent->nexteventinqueue;
}
if(currentevent == NULL)//no such event found, so tag it onto the end.
{
temp->next = newevent;
return;
}
//handle coincidences by delaying the new event
while((currentevent->time) == (newevent->time))
{
double d = (maximally precise increment of a double precision floating point number);
while((currentevent->time) == (newevent.time)) //loop I want to get rid of
{
newevent.time += d;
d *= 2;
}
temp = currentevent;
currentevent = currentevent.nexteventinqueue;
}
temp.nexteventinqueue = newevent;
newevent.nexteventinqueue = currentevent;
return;
}
Now there are quite a few issues with this, but I want to somehow get rid of the while loop in the middle. Most of my times aren't even close to holding the maximum precision that a double floating point can muster, so it's a waste of time to start by assuming they do, and because my RNG isn't especially random, this loop must execute quite frequently.
Is there a way to either (1) directly increment the fractional part of a double floating point variable, or to (2) figure out how precise a given floating point variable x in less than O(log(x))?
Use
nextafter
(from<math.h>
) to get the immediately next largest value after a double-precision value. (Ornextafterf
for a single-precision value).For more information,
man nextafter
, also available here.