Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Objective-C parameter mutation




On 16 Oct 2008, at 11:31 am, James Trankelson wrote:

... and when I would call it ([inst foo:0.001], for example), the
value inside the foo method would NOT be 0.001. At the time, I
believed there to be an Objective C requirement that parameters to
methods need to be pointers to objects, not primitive values. So, I
achieved success by changing the above method declaration to

-(void) foo:(NSNumber*)val { }

... and passing in a NSNumber* with a float value worked.

Now, I'm having the same problem, but am not convinced there is any
such requirement that parameters be pointers to object instances. (I
could be wrong, though)

You're not wrong, there is no such requirement. A glance at most Cocoa classes will reveal methods with simple scalar parameters.


Others have addressed your current problem, but a note about the problem of passing 0.001:

A float may not be able to exactly represent 0.001, but a value somewhat close to it. When the debugger displays a value it is having to do some internal conversion to generate the actual characters displayed and there may be some rounding or other adjustment occurring. Passing it in an NSNumber may appear to work because it *maybe* storing the value internally as a double or in some other form that subtly affects the rounding.

Whatever is happening, the fact is that floats cannot store numbers arbitrarily precisely. It's obvious really - a float is 32 bits so no matter how a value is encoded into those 32 bits, it can only represent 2^32-1 possible discrete values.

--Graham
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Objective-C parameter mutation (From: "James Trankelson" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.