Re: floatValue of nil NSNumber unreliable (was Re: Bug or feature?)
Re: floatValue of nil NSNumber unreliable (was Re: Bug or feature?)
- Subject: Re: floatValue of nil NSNumber unreliable (was Re: Bug or feature?)
- From: Greg Parker <email@hidden>
- Date: Tue, 14 Feb 2006 12:48:15 -0800
M. Carlson wrote:
What number should variable 'fval' have after this is executed?
- (void) doit
{
float fval=0.0;
NSNumber *aNumber=nil;
fval=[aNumber floatValue];
NSLog(@"fval is %f",fval);
}
Short answer:
It depends on which architecture and Mac OS X version you're using. If
you want to be maximally portable, assume `fval` is undefined.
Medium answer:
On Panther and earlier, any architecture: `fval` is undefined.
On Tiger PPC: `fval` is undefined.
On Tiger i386: `fval` is 0.0.
On some future version of Mac OS X, any architecture: `fval` is 0.0.
On Tiger "Intel Developer Transition Kit" machines: `fval` is undefined.
Long answer:
On i386, the int-return and float-return function call conventions are
sufficiently different that a new objc_msgSend() variant is needed to
make messages to nil return 0.0. (The problem involves the x87
floating-point stack, a relic from the Intel 8086!) objc_msgSend_fpret
() was added late in Tiger i386 development, so Tiger i386 guarantees
`returns 0.0`.
On PPC, objc_msgSend() clears registers r3 and r4 before returning
from a message to nil. This means that pointer and integer return
types are 0, but float and double are not because they are returned in
register f1. objc_msgSend() could have cleared register f1 as well,
but we were previously worried that we couldn't guarantee the same
`returns 0.0` semantics on i386 as well.
Now that i386 has objc_msgSend_fpret(), PPC will someday change to
`returns 0.0` as well. However, this does create binary compatibility
problems with code that was incorrect but just happened to do
something useful before, so it probably won't appear in a Tiger update
release.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden