Message Boards Message Boards

0
|
4163 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

error in N // Floor? (corrected version)

Posted 10 years ago

Hello,

my question is easy to understand: Shouldn't Mathematica produce the same output for the following two inputs?

  1. Log[10^3]/Log[10] // FullSimplify // N // Floor
  2. Log[10^3]/Log[10] // N // Floor

The corresponding output of mathematica version 8 and 9 are 3 (correct) and 2 (false) respectively. You can see the problem mathematica has, when you use RealDigits instead of floor:

  • Log[10^3]/Log[10] // FullSimplify // N // RealDigits gives {{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1}

  • Log[10^3]/Log[10] // N // RealDigits gives {{2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, 1}

From my understanding a CAS-system shouldn give here the same outputs. What do you think about it? Thanks in advance for your answer.

Peter Quedens

POSTED BY: Peter Quedens
5 Replies

Hi,

Shouldn't Mathematica produce the same output for the following two inputs?

It should not, since you use N[] this kind of behavior (round off error) is normal.

I.M.

POSTED BY: Ivan Morozov
Posted 10 years ago

Hi,

yes, N is used, but it is used in both expressions and so it should give the same result, since the only difference ist the FullSimplify. In any case: I think mathematica should know, that Log[10^3]/Log[10] is 3 without a FullSimplify. P.Q.

POSTED BY: Peter Quedens

Even it is used in both expressions it is applied to different ones:

Log[10^3]/Log[10] // FullSimplify // N // Floor // Trace
Log[10^3]/Log[10] // N // Floor // Trace

I.M.

POSTED BY: Ivan Morozov

From my understanding a CAS-system shouldn give here the same outputs.

And it does so. This problem arises solely from the mindless usage of N[]

In[27]:= Union[(Floor[N[FullSimplify[Log[10^3]/Log[10]], #]] - Floor[N[Log[10^3]/Log[10], #]]) & /@ RandomReal[{1., 100.}, 1000]]
Out[27]= {0}

if you consider that a sloppy argument, look at this

In[28]:= Floor[N[FullSimplify[Log[10^3]/Log[10]], 1.]] - Floor[N[Log[10^3]/Log[10], 1.]]
Out[28]= 0

even that works

In[31]:= Floor[N[FullSimplify[Log[10^3]/Log[10]], .0]] - Floor[N[Log[10^3]/Log[10], .0]]
Out[31]= 0

and this

In[32]:= Floor[N[FullSimplify[Log[10^3]/Log[10]], 0]] - Floor[N[Log[10^3]/Log[10], 0]]
Out[32]= 0

and this

 In[35]:= Floor[N[FullSimplify[Log[10^3]/Log[10]], $MachinePrecision]] - Floor[N[Log[10^3]/Log[10], $MachinePrecision]]
 Out[35]= 0

You hit seemingly the only case not working

In[34]:= Floor[N[FullSimplify[Log[10^3]/Log[10]]]] - Floor[N[Log[10^3]/Log[10]]]
Out[34]= 1

despite the description in the help, that In[35] is equivalent to In[34].

POSTED BY: Udo Krause

Carrying out the division with machine precision numbers is subject to floating point error.

This is not specific to Mathematica at all, for example numpy gives the exact same result:

>>> a=numpy.log(1000)
>>> b=numpy.log(10)
>>> a
6.9077552789821368
>>> b
2.3025850929940459
>>> a/b
2.9999999999999996

But in Mathematica we can also use arbitrary precision arithmetic, to any desired number of digits:

N[Log[1000]/Log[10], 200] 
POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract