Is it safe to assume that the condition (int)(i * 1.0f) == i is true for any integer i?
Is it safe to assume that the condition (int)(i * 1.0f) == i is
Share
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.
No.
If
iis sufficiently large thatint(float(i)) != i(assuming float is IEEE-754 single precision,i = 0x1000001suffices to exhibit this) then this is false, because multiplication by1.0fforces a conversion tofloat, which changes the value even though the subsequent multiplication does not.However, if
iis a 32-bit integer anddoubleis IEEE-754 double, then it is true thatint(i*1.0) == i.Just to be totally clear, multiplication by
1.0fis exact. It’s the conversion frominttofloatthat may not be.