1
$\begingroup$

I was going through the C++ 10th edition deitel's book. There I encountered a questioned regarding floor() function. And here's the question: Floor() practice question Now, from what I understand so far is that no matter what number I set as x I get the same output from both expressions.

Can anyone simplify this for me? How are the above expressions any different from one another? And how are they rounding the input in either the tenths or hundredths form?

$\endgroup$
4
  • 1
    $\begingroup$ Hint: try out some examples, like $x = 0.637$ or $x = 1.375$. $\endgroup$
    – Rob Arthan
    Aug 15, 2020 at 15:08
  • $\begingroup$ Welcome to MSE. Hint: your inputs should have two or more places to the right of the decimal point. Otherwise, the outputs would just be the same. (Can you see why?) $\endgroup$ Aug 15, 2020 at 15:11
  • $\begingroup$ @RobArthan, oh i see. Basically i didn't got as far to input number which had such decimal values. i tried with values like 2.2 or 3.3 and since there was no hundredth or thousandths to roundoff from it just showed me the available part. Thank you!! $\endgroup$ Aug 15, 2020 at 15:12
  • 1
    $\begingroup$ @BenjaminWang, yeah the hint up above was really helpful. anyway thanks $\endgroup$ Aug 15, 2020 at 15:13

1 Answer 1

0
$\begingroup$

Take $x=0.7152$, for example. The first expression yields:$$\text{floor}(x\cdot 10+0.5)/10=\text{floor}(7.652)/10=7/10=0.7$$ while the second expression yields:$$\text{floor}(x\cdot 100+0.5)/100=\text{floor}(72.02)/100=72/100=0.72$$ Both results represent correct rounding of $x$ to one and two places after the decimal point, respectively.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .