4
$\begingroup$

Something very strange is happening as I evaluate the Floor function:

(9.2 - 8)/1.2
Floor[(9.2 - 8)/1.2]

returns:

1.

0

so I did the following:

Floor[1]
Floor[1.0]
Floor[1.2/1.2]

returning

1

1

1

Does anybody know what is happening? Am I doing something wrong here? I also tried the following and got the same confusing result:

x = (9.2 - 8)/1.2
Floor[x]
1.

0

and

In[57]:= Floor[(10.2 - 9.0)/1.2]

Out[57]= 0

but

In[59]:= Floor[(10.4 - 9.0)/1.4]

Out[59]= 1

I'm getting so confused that sometimes I doubt myself when doing this simple operations in my head... Any information about this will be highly appreciated.

$\endgroup$
4
  • 1
    $\begingroup$ Examine the output of (9.2 - 8)/1.2 // FullForm. $\endgroup$ – Carl Woll Mar 11 at 21:48
  • 1
    $\begingroup$ You might find (9.2 - 8)/1.2 // FullForm illuminating—Mathematica does this occasionally annoying thing where it rounds numerical output in output cells by default. As to why (9.2 - 8)/1.2 is less than one, I'm not totally sure beyond "implementation details", and that's still worth getting an explanation for. EDIT: I see @CarlWoll beat me to it by 15 seconds... :) $\endgroup$ – thorimur Mar 11 at 21:49
  • $\begingroup$ Oh, so it's related to the order of evaluation... Thank you very very much @CarlWoll and @thorimur, I think Round[x,0.1] will do the trick for me :D $\endgroup$ – Sofi Ixchel Mar 11 at 21:58
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Mar 12 at 5:28
3
$\begingroup$

As it was suggested by @CarlWoll and @thorimur in the comments, I evaluated:

(9.2 - 8)/1.2 // FullForm

which returns:

0.9999999999999994`

I'm not really sure why, but I suspect that this is related to the order in which Mathematica evaluates each operation. Since 9.2/1.2 and 8/1.2 have no finite decimal expansion, apparently Mathematica truncates this expressions and rounds them when showing the final answer. So I decided to round them myself before plugging them in the Floor function as follows:

Floor[Round[(9.2 - 8)/1.2, 0.1]]

resulting in:

1

I still have no clue why Mathematica would distribute the 1/1.2 into the subtraction apparently ignoring the parenthesis, but I suppose it's usually better when working with large numbers...

$\endgroup$
1
  • 1
    $\begingroup$ It's not that it's distributing the 1.2, it's that 9.2 - 8 is slightly less than 1.2 when using machine precision numbers. Try just 9.2 - 8//FullForm and you'll get 1.1999999999999993. If you use Round (as you already did) or Rationalize to get exact numbers, or enter exact numbers yourself (Floor[(92 - 80)/12]), Mathematica will be able to return the correct value. $\endgroup$ – MassDefect Mar 12 at 4:51

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.