• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: floatValue of nil NSNumber unreliable (was Re: Bug or feature?)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
  • Prev by Date: [resend] Bindings validation weirdness
  • Next by Date: CoreImage: Newbie can't find memory leak
  • Previous by thread: [resend] Bindings validation weirdness
  • Next by thread: CoreImage: Newbie can't find memory leak
  • Index(es):
    • Date
    • Thread