Amazon.com Widgets float value
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 04:57:09 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Help!!!
| | |-+  float value
Pages: [1]   Go Down
Print
Author Topic: float value  (Read 622 times)
diuming
Newbie
*
Posts: 8


Email




« on: June 17, 2011, 08:00:53 AM »

What happen? When i declare float in Xcode. please refer to attached file.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #1 on: June 17, 2011, 11:06:36 AM »

Floating point numbers can not always be stored exactly inside a digital computer (think about storing 1/3 or .33333333).   Search this forum, as this issue was asked once before.

Cheers,

Steve
Logged
diuming
Newbie
*
Posts: 8


Email




« Reply #2 on: June 18, 2011, 06:23:37 AM »

Hi Steve,

thanks for your reply

In attached file content,
I declare a float data type name is floatValue that I assigned value 45.70 to it.

In my concept the result should be 45.000000
But I got the result is 45.000001 and I got the value in debug mode is 45.70000008.

So, If i got the result is 45.000001 is correct when i using NSLog or printf function?

I confuse it.
« Last Edit: June 18, 2011, 06:27:29 AM by diuming » Logged
diuming
Newbie
*
Posts: 8


Email




« Reply #3 on: June 18, 2011, 08:13:00 AM »

In my concept the result should be 45.000000
change to
In my concept the result should be 45.700000
Logged
dharr19
Full Member
***
Posts: 175


Email




« Reply #4 on: June 30, 2011, 07:21:56 PM »

Hi,

I received the following


2011-06-30 22:20:10.217 prog4[1528:903] floatingVar = 45.700001


David
Logged
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #5 on: July 01, 2011, 10:30:49 AM »

Yeah, once again this illustrates the fact that digital computers cannot always exactly represent a number internally.  That's the reason for that output.  When writing program examples that require comparison against floating point values, an error value, typically called epsilon, is often used.  For example, this would be the safest way to check to see if a floating point variable floatingVar contained the value 45.7:

Code: (Objective-C)
float  epsilon = .000001

if ( abs (floatingVar - 45.7) < epsilon )
   //   consider it equal to 45.7
else
   // no, it's not equal to 45.7

Cheers,

Steve
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.