• 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: sorting on floats
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sorting on floats


  • Subject: Re: sorting on floats
  • From: Sherm Pendley <email@hidden>
  • Date: Wed, 8 Dec 2004 01:44:30 -0500

On Dec 7, 2004, at 6:47 PM, John Spicer wrote:

I've got a table and one of the columns is a money value (float, with $$formatter).

Never use floats to store currency values, or any other time when absolutely exact accuracy is required. Floats are an approximation, and there are some values that cannot be exactly represented with a float. The reason is similar to why the fraction 1/3 cannot be exactly represented in ordinary decimal notation.


To see this effect, build and run this simple example, which uses floats to add five cents to a million dollars:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    float a = 1000000.0;
    float b = 0.05;

    float c = a + b;

    NSLog(@"%f", c);

    return 0;
}

The result is not what you would expect it to be. For more details, google for "floating point accuracy".

For an amusing example of what can happen to all the pennies that might get "lost" this way, see the movie "Office Space". :-)

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
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


References: 
 >sorting on floats (From: John Spicer <email@hidden>)

  • Prev by Date: int <-> float formatters?
  • Next by Date: Re: Registering for NSNotification globally?
  • Previous by thread: Re: sorting on floats
  • Next by thread: delay function
  • Index(es):
    • Date
    • Thread