• 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: why are floats flakey?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why are floats flakey?


  • Subject: Re: why are floats flakey?
  • From: Bob Ippolito <email@hidden>
  • Date: Mon, 24 May 2004 23:53:04 -0400

On May 24, 2004, at 10:23 PM, justin webster wrote:

> why are floats inaccurate?
> when can I use them with confidence and what can I use in their place
> when I need absolute accuracy?
>
> i'm trying to create a float with a value of 29.97 and I get 29.9699999
> like this:
> float myFloat = [myTextField floatValue]
> where the text field contains the string "29.97df"

floats are have a finite representation. They are exactly 32 bits in
length, which means that there are only 2^32 possible values a float
could have. Doubles are twice as big (64bits). Also, they are stored
in base 2, not base 10, so there are quite a few decimal numbers, even
reasonably sized ones like your example, that aren't precisely
representable with a floating point number.

There is a class called NSDecimalNumber, which also has a (MUCH larger)
finite representation and is designed to store *decimal* numbers. It
is almost definitely suitable for your needs. NSDecimalNumber has no C
language equivalent, and ObjC does not support operator overloading, so
you need to do [math
byPassingVerboseSelectors:wheneverYouWantToAddOrSubtract:orMultiply:
orExponentiate:]

-bob

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >why are floats flakey? (From: justin webster <email@hidden>)

  • Prev by Date: Re: Toolbar in a Sheet?
  • Next by Date: Re: NSURLRequest and HTTP PUT?
  • Previous by thread: Re: why are floats flakey?
  • Next by thread: Re: why are floats flakey?
  • Index(es):
    • Date
    • Thread