Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2010, 11:35 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 142
Exclamation compare values

Hi. I am taking the values of the accelerometer and compare with doubles. I mean, i take the value of the axy X and i wanto to compare with double x=0.00023 for example....but it doesnt work.anyone have an idea???

if(acceleration.x==x){
////something
}

THANKSSSS

Last edited by ivanraso; 03-18-2010 at 11:54 AM.
ivanraso is offline   Reply With Quote
Old 03-18-2010, 12:09 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

When comparing floats and doubles it's better to see if the difference between them is less than some value. Checking for exact equality does not work well, since some numbers can not be stored exactly.

Code:
double delta = abs(acceleration.x-x);

if(delta < 0.001){
////do something
}
You can read more here:
http://www.cygnus-software.com/paper...ringfloats.htm
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-18-2010, 12:19 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 142
Default

Quote:
Originally Posted by smasher View Post
When comparing floats and doubles it's better to see if the difference between them is less than some value. Checking for exact equality does not work well, since some numbers can not be stored exactly.

Code:
double delta = abs(acceleration.x-x);

if(delta < 0.001){
////do something
}
You can read more here:
http://www.cygnus-software.com/paper...ringfloats.htm

I dont why i cannot compare two numbers. I can do it in Java,C,etc..
Maybe if i create the x value in float.?¿??¿ I am confused.

Great game DEAD PANIC.
ivanraso is offline   Reply With Quote
Old 03-18-2010, 12:37 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

Quote:
Originally Posted by ivanraso View Post
I dont why i cannot compare two numbers. I can do it in Java,C,etc..
Maybe if i create the x value in float.?¿??¿ I am confused.
Yes, you can compare two numbers as either doubles or floats. But you probably won't get the result you want. Floats and doubles are approximations. The "==" comparison checks that the two numbers are exactly the same. The problem is they won't be exactly the same, unless they were set from the same number originally, such as:

Code:
  float x = 34.5;
  float y = 34.5;
  if(x == y)  {...}
But this may not work:
Code:
  float x = 34.5;
  if(x == 34.5)  {...}
That is because in the "if" statement, the x is promoted to double before comparing with the (double) constant 34.5. The double constant 34.5 retains all the precision of a double, but some of that precision may have been sacrificed when the assignment to x was made. So x is just an approximation of 34.5. Then when x is promoted to double, it may not be exactly the same as the double constant 34.5.

When floats and doubles represent real measurements in the physical world (as they do in the case of the accelerometer) then comparisons for equality are usually inappropriate for the application because they will hardly ever be true.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 253
24 members and 229 guests
@sandris, AdamL, ADY, Dani77, diyora, FAED, fredidf, F_Bryant, GHuebner, HDshot, JasonR, mer10, Oral B, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Thompson22, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,752
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0