Re: NSTimer or double to int cast (Bug?)
Re: NSTimer or double to int cast (Bug?)
- Subject: Re: NSTimer or double to int cast (Bug?)
- From: Dietmar Planitzer <email@hidden>
- Date: Sat, 11 Jan 2003 23:21:40 +0100
>
From: Marco Binder <email@hidden>
>
>
OK, just now I4ve done some comparison and calculation stuff:
>
>
[timer timeInterval]<1.0 return NO on my G4 and YES on my iBook!
>
>
([timer timeInterval]-1.0) * 10000 returns 0.00000 on my G4 and
>
-0.00150 on my iBook!
>
>
So the returned timeInterval is NOT equal to the value passed for
>
initialization on all machines. I still consider that to be a bug,
>
altough one with easy workarounds.
>
>
Somehow it seems to be correlated with the machines speed, as Cameron4s
>
600 MHz iBook works as expected (that is: correct), whereas my 500 MHz
>
iBook, my girlfriends 400 MHz G3 iMac and another developers 400 MHz
>
iMac all three show the deviation.
>
>
PS to Dietmar: as it turned out, you were right. The question remaining
>
is, why is the timer not returning the same value as I have set?!
Probably because NSTimer is, I think since 10.2, toll-free bridged to
CoreFoundation CFTimers. CFTimer, though, does all its internal calculations
with 64-bit integers. So there's a good chance for rounding errors.
Anyway, you've got two options around this problem:
Either do all your calculations with double precision and use range checks
rather than exact comparisions against a limit (fabs(v) < DBL_EPSILON [or
i.e. 0.0001] rather than v == 0.0), or use integer values and round the
double value before you cast it. A faster way to do the rounding is by
adding 0.5 to the double value before you cast it: y = (x + 0.5). Naturally,
this works only correctly for positive values of x.
Regards,
Dietmar Planitzer
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.