I’m doing the following in Objective-C and expecting 180 as the output but I’m getting 150. Can anyone explain what I’m doing wrong?
(360 / 100) * 50
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re (accidentally) using integer division.
360 / 100is returning3, then3 * 50is of course150. To obtain a floating point result, try casting360or100to a float first, or just use a literal float – i.e.,360.0 / 100or360 / 100.0or even360.0 / 100.0.Or, as @KennyTM pointed out in a comment, you can reorder the statement such as
360 * 50 / 100— this is particularly useful if a floating-point number is unacceptable for any reason.