Re: Problem with Float type variable
Re: Problem with Float type variable
- Subject: Re: Problem with Float type variable
- From: Adil Saleem <email@hidden>
- Date: Sat, 15 Aug 2009 00:01:24 -0700 (PDT)
But why would it round 863.6 ? I mean there is only 1 digit after the decimal. It should not have the range problem with a value this small.
--- On Fri, 8/14/09, Graham Cox <email@hidden> wrote:
From: Graham Cox <email@hidden>
Subject: Re: Problem with Float type variable
To: "Adil Saleem" <email@hidden>
Cc: email@hidden
Date: Friday, August 14, 2009, 11:53 PM
On 15/08/2009, at 4:46 PM, Adil Saleem wrote:
> On calling this, the variable aFloat in setMyvar method gets value 863.599976. I also tried giving it 6.600000 but still didn't work correctly. I don't understand why is it changing the value. Why doesn't it get 863.6 ?
This is a classic misunderstanding of floats. Look up "floating point precision". Basically, a float is 32 bits - it can only represent 2^32 different values, not the infinity of numbers that exist. So quite often certain values are rounded off, truncated or represent by something "close". If you need exact values, use integers.
--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:
This email sent to email@hidden