Re: inconsistent float behaviour
On 11/10/2008, at 11:59 AM, Kyle Sluder wrote: On Fri, Oct 10, 2008 at 9:58 PM, Kyle Sluder [EMAIL PROTECTED] wrote: This article It would help if I provided the link. http://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency Thanks Kyle. I didn't realise floating point were so weird. I'm going off to do more research on this as its just changed everything I've believe for years. Thanks for the link. I've now changed all the code to deal in NSDecimalNumbers instead and all is now accurate. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
Thanks guys, I'm still unsure why I'm getting 4 decimal places as I know for fact that the data is 2 decimal places. However, it seems my approach is wrong. I'll convert to int's and NSDecimalNumbers instead and remove all floats. On 11/10/2008, at 4:14 AM, Uli Kusterer wrote: On 10.10.2008, at 13:04, Steven Hamilton wrote: NSLog(@current balance is: %@,balance); and get current balance is: 18976.69 in the console. So far so good. (...) The float calculation is exactly 2 decimal places, I know that for a fact and NSLog proves it. You may want to be aware that NSLog gives no guarantees as to the output of its formats if you use [EMAIL PROTECTED] All it does is call the - description method of the requisite object. -description is only intended for debugging, and as such what it returns for a particular object may change from release to release, or even from instance to instance (e.g. when you log an NSArray, you occasionally get more detailed output than when logging a toll-free-bridged CFArrayRef). Cheers, -- Uli Kusterer The Witnesses of TeachText are everywhere... http://www.zathras.de ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
On Oct 10, 2008, at 4:04 AM, Steven Hamilton wrote: Hi folks, I think I need some help understanding floats. I have a class that calculates a balance and places it in a table column. At the end of this method I NSLog the output like so; NSLog(@current balance is: %@,balance); and get current balance is: 18976.69 in the console. So far so good. The method plonks the balance into a table and returns it to whence it came. The table is sourced by the normal datasource delegate methods and the column has a number formatter on it set to Decimal #,##0,00## with half even rounding. I placed the last ##'s in there to check that the floats weren't producing decimals with more than 2 places but in my table column I get 18976.6995 which doesn't make sense. The float calculation is exactly 2 decimal places, I know that for a fact and NSLog proves it. Yet somehow, the table datasource appears to produce random decimal digits where i have the last ##. Am I totally misunderstanding something about floats? Or is the extra ##'s in the formatter making things up? There are an infinite number of floating point values between any two integer numbers, so it is not possible to represent every single possible float value using a fixed-width binary number. You're most likely seeing a number that can't be exactly represented. If you're dealing with currency, you should probably be using integer arithmetic, and store all values as pennies or tenths of pennies, depending on the precision you need. -- Dave Carrigan [EMAIL PROTECTED] Seattle, WA, USA PGP.sig Description: This is a digitally signed message part ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
On Oct 10, 2008, at 5:04 AM, Steven Hamilton wrote: Am I totally misunderstanding something about floats? Or is the extra ##'s in the formatter making things up? Floating point values of either float or double are not guaranteed to be super precise, and should never be used to represent currency values. Try using a long long instead, and have the first two digits represent a fractional unit of currency. Nick Zitzmann http://www.chronosnet.com/ ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
I've not used it myself, but there is also NSDecimal that may be an alternative. Also, I recall seeing an IBM-released class that I believe had been part of ICU that also did decimal arithmetic using 32 or 64 bits. On Oct 10, 2008, at 5:04 AM, Steven Hamilton wrote: Am I totally misunderstanding something about floats? Or is the extra ##'s in the formatter making things up? Floating point values of either float or double are not guaranteed to be super precise, and should never be used to represent currency values. Try using a long long instead, and have the first two digits represent a fractional unit of currency. Nick Zitzmann http://www.chronosnet.com/ ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
On Fri, Oct 10, 2008 at 7:04 AM, Steven Hamilton [EMAIL PROTECTED] wrote: The float calculation is exactly 2 decimal places This is unlikely. There are very few numbers with two decimal places which can be perfectly represented in floating-point numbers. (Those would be numbers with fractional values that can be composed from powers of two, i.e. 0.25, 0.5, and 0.75.) You'll probably want to read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html And then stop using floating point math for anything which you require to produce an exact result. For money, the best thing to do is to simply perform calculations in cents using integers, then divide by 100 for display purposes only. Mike ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Nick Zitzmann wrote: On Oct 10, 2008, at 5:04 AM, Steven Hamilton wrote: Am I totally misunderstanding something about floats? Or is the extra ##'s in the formatter making things up? Floating point values of either float or double are not guaranteed to be super precise, and should never be used to represent currency values. Try using a long long instead, and have the first two digits represent a fractional unit of currency. Keep in mind that despite the decimal point (US $$), money is an integer. If you're using a 64 bit int, there's room to shift it over 8 bits or so to give a little roundoff space for division and percents and the like. Unless you're working with your losses in the stock market (Oct 2008)... - -- Glenn English [EMAIL PROTECTED] -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkjvkQ4ACgkQ04yQfZbbTLYj8QCdEHM8nQplWqwsLHn9j8yCcqmT qvIAn12OoUgbvZURYvP+VOnU3/h2CuV2 =8jm1 -END PGP SIGNATURE- ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
On 10.10.2008, at 13:04, Steven Hamilton wrote: NSLog(@current balance is: %@,balance); and get current balance is: 18976.69 in the console. So far so good. (...) The float calculation is exactly 2 decimal places, I know that for a fact and NSLog proves it. You may want to be aware that NSLog gives no guarantees as to the output of its formats if you use [EMAIL PROTECTED] All it does is call the - description method of the requisite object. -description is only intended for debugging, and as such what it returns for a particular object may change from release to release, or even from instance to instance (e.g. when you log an NSArray, you occasionally get more detailed output than when logging a toll-free-bridged CFArrayRef). Cheers, -- Uli Kusterer The Witnesses of TeachText are everywhere... http://www.zathras.de ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
Thanks guys, I'm still unsure why I'm getting 4 decimal places as I know for fact that the data is 2 decimal places. However, it seems my approach is wrong. I'll convert to int's and NSDecimalNumbers instead and remove all floats. On 11/10/2008, at 4:14 AM, Uli Kusterer wrote: On 10.10.2008, at 13:04, Steven Hamilton wrote: NSLog(@current balance is: %@,balance); and get current balance is: 18976.69 in the console. So far so good. (...) The float calculation is exactly 2 decimal places, I know that for a fact and NSLog proves it. You may want to be aware that NSLog gives no guarantees as to the output of its formats if you use [EMAIL PROTECTED] All it does is call the - description method of the requisite object. -description is only intended for debugging, and as such what it returns for a particular object may change from release to release, or even from instance to instance (e.g. when you log an NSArray, you occasionally get more detailed output than when logging a toll-free-bridged CFArrayRef). Cheers, -- Uli Kusterer The Witnesses of TeachText are everywhere... http://www.zathras.de ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
On Fri, Oct 10, 2008 at 9:33 PM, Steven Hamilton [EMAIL PROTECTED] wrote: I'm still unsure why I'm getting 4 decimal places as I know for fact that the data is 2 decimal places. However, it seems my approach is wrong. I'll convert to int's and NSDecimalNumbers instead and remove all floats. The problem is that you don't really know that the data has two decimal places. As Mike Ash pointed out, IEEE floating point can rarely exactly specify a number that only has two decimal places. So though you might write a literal 1.23 in your code, the compiler converts it to a number like 1.2301. In fact, go ahead and launch Terminal.app and run `python`. Type 1.23, and the interpreter will give you back 1.23. Then type 1.23 * 10, and watch as the interpreter gives you 12.301. This article, 7 Top Tips for Coding with Currency, just made it to progamming.reddit.com about four days ago. It gives a very nice summary of the issues involved when dealing with currency -- not surprisingly, the first one is work with integer minor units, and never ever ever use floating point numbers. --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
On Fri, Oct 10, 2008 at 9:58 PM, Kyle Sluder [EMAIL PROTECTED] wrote: This article It would help if I provided the link. http://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: inconsistent float behaviour
On Fri, Oct 10, 2008 at 6:33 PM, Steven Hamilton [EMAIL PROTECTED] wrote: Thanks guys, I'm still unsure why I'm getting 4 decimal places as I know for fact that the data is 2 decimal places. That is because floating point numbers don't have decimal places; forget all about decimal places. Please read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html, it is, as the title says: what every computer scientist should know about floating-point arithmetic. However, it seems my approach is wrong. I'll convert to int's and NSDecimalNumbers instead and remove all floats. On 11/10/2008, at 4:14 AM, Uli Kusterer wrote: On 10.10.2008, at 13:04, Steven Hamilton wrote: NSLog(@current balance is: %@,balance); and get current balance is: 18976.69 in the console. So far so good. (...) The float calculation is exactly 2 decimal places, I know that for a fact and NSLog proves it. You may want to be aware that NSLog gives no guarantees as to the output of its formats if you use [EMAIL PROTECTED] All it does is call the -description method of the requisite object. -description is only intended for debugging, and as such what it returns for a particular object may change from release to release, or even from instance to instance (e.g. when you log an NSArray, you occasionally get more detailed output than when logging a toll-free-bridged CFArrayRef). Cheers, -- Uli Kusterer The Witnesses of TeachText are everywhere... http://www.zathras.de ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/clarkcox3%40gmail.com This email sent to [EMAIL PROTECTED] -- Clark S. Cox III [EMAIL PROTECTED] ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]