1
$\begingroup$

I'm trying to figure out what the smallest positive integer x such that the floating point expression round(round(1/x)*x) is not equal to 1 in single precision.

I have that the answer is 41, but when I let the number 3 for example be x, isn't 1/3 = 0.333, and when rounded = 0, 0 * 3 = 0? So why is the answer 41?

$\endgroup$
1
$\begingroup$

That's probably not what was meant by "round". I suspect "round" probably meant to round to the nearest single-precision float. In particular, you can easily verify that 41 is the smallest number with this property:

>>> from numpy import * 
>>> float32(float32(1.0/3.0)*3.0)
1.0
>>> float32(float32(1.0/40.0)*40.0)
1.0
>>> float32(float32(1.0/41.0)*41.0)
0.99999994

But you should double-check the definition of the function round() with whoever told you this problem. We can't read their mind.

$\endgroup$
  • $\begingroup$ This will probably also depend on which rounding method your floating-point library uses. $\endgroup$ – Davislor Sep 29 '15 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.