Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > why can't I do simple math?

why can't I do simple math?
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 5, 2003, 12:18 PM
 
Code:
float progressIndicator; progressIndicator=17/400*100; NSLog(@"progressIndicator=%f",progressIndicator);
This results in progressIndicator being 0. What am I doing wrong?

kman
     
Mac Enthusiast
Join Date: Feb 2003
Location: Portland, Oregon
Status: Offline
Reply With Quote
Apr 5, 2003, 01:36 PM
 
I still don't know objective-C well, but could it be as simple as missing parenthesis around the math?

progressIndicator = ( 17/400*100 );
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Apr 5, 2003, 02:59 PM
 
Your code is standard C. In standard C, when you do integer math, you get an integer answer. Using all integers, 17/400 is zero (it's .0425 - truncate the non-integer portion and you get zero).

In standard C, an arithmetic operation's resulting type is dependent upon the types used within that operation. So, if you used 17.0 / 400 instead of 17 / 400, you'd get a fraction. Adding the '.0' tells the compiler "even though this number is an integer, I want you to treat it as a float". You could get the same result using (float)17 / 400.

The rule of thumb when using constants in a math expression is: If you want the answer to be a float, use constants that are explicitly floats. If not, don't.

For more info, consult any book about C and look for the section on 'expressions'.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 5, 2003, 03:06 PM
 
Originally posted by smeger:
Your code is standard C. In standard C, when you do integer math, you get an integer answer. Using all integers, 17/400 is zero (it's .0425 - truncate the non-integer portion and you get zero).

In standard C, an arithmetic operation's resulting type is dependent upon the types used within that operation. So, if you used 17.0 / 400 instead of 17 / 400, you'd get a fraction. Adding the '.0' tells the compiler "even though this number is an integer, I want you to treat it as a float". You could get the same result using (float)17 / 400.

The rule of thumb when using constants in a math expression is: If you want the answer to be a float, use constants that are explicitly floats. If not, don't.

For more info, consult any book about C and look for the section on 'expressions'.
Thanks!
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 03:21 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2