我试图将两个相等价值的双打分开最小数量。上下文是一个事件模拟。我不希望事件同时发生,因此我将设置为发生新事件的时间增加了最小量。
(这很烦人(自我实现的 RNG),所以我实际上需要进行调查,直到找到没有事件发生的时间。)
目前,我的代码想做这样的事情(未经测试,视为伪代码):
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;
}
现在有很多问题,但我想以某种方式摆脱中间的while循环。我的大部分时间甚至都没有接近双浮点可以聚集的最大精度,所以假设他们这样做是浪费时间,而且因为我的 RNG 不是特别随机,这个循环必须执行得相当频繁地。
有没有办法(1)直接增加双浮点变量的小数部分,或者(2)找出给定浮点变量 x 在小于 O(log(x)) 时的精确度?