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: Fri, 10 Jan 2003 21:05:08 +0100
>
From: Marco Binder <email@hidden>
>
<snip>
>
Maybe there is something wrong with the code, but I dont see it. And as
>
before, this code results in the following being written to the console
>
on my G4:
>
>
## Component Manager: attempting to find symbols in a component alias
>
of type (regR/carP/x!bt)
>
2003-01-10 13:58:04.390 TimerTest[834] - interval as returned:
>
1.000000, interval as int: 1
>
2003-01-10 13:58:05.389 TimerTest[834] - interval as returned:
>
1.000000, interval as int: 1
>
2003-01-10 13:58:06.389 TimerTest[834] - interval as returned:
>
1.000000, interval as int: 1
>
>
On the other hand on the G3 iBook:
>
>
2003-01-10 14:00:37.286 TimerTest[413] - interval as returned:
>
1.000000, interval as int: 0
>
2003-01-10 14:00:38.284 TimerTest[413] - interval as returned:
>
1.000000, interval as int: 0
>
2003-01-10 14:00:39.283 TimerTest[413] - interval as returned:
>
1.000000, interval as int: 0
>
>
I am not making this up, and you are more than welcome to downlad the
>
project at: http://www.sb-software.de/TimerTest.tgz
>
>
>
I still think this is extremly strange and would appreciate any kind of
>
explaination.
Wild Theory:
Floating point numbers are not exact, they are just approximations. I.e.
some numbers which are simple for us can't even be exactly represented in
the floating point format (think it was 0.5 or 0.1 - have forgotten which
one).
Because of this, de facto every function which prints a floating point value
to the screen does some cosmetic to it, also called rounding.
Now what I think is happening in your case is that on the G4 you have a time
interval which is really >= 1 second. But on the iBook you may have an
interval of 0.99999 ("periodisch" as we call it in German).
NSLog, takes your double value, and prints a rounded value to the console.
But if you tell the compile to cast the value to an int you end up with 1 in
the first case and 0 in the second, because casting in C based languages
does not round, it simply truncates the value.
You could verify my theory by declaring a union which consists of a double
value and a two element unsigned integer array. Then in your timer callback
put the time interval into the double field of the union. Then print both
unsigned integer values to the console. There should be a difference between
the values the G4 prints and the ones which the iBook prints.
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.