4
$\begingroup$

I accidentally discovered for myself, that Mathematica outputs inaccurate answer. For instance, if I take $\sin(2 \cdot \pi \cdot 0.5) = 0 $, then in Mathematica it is:

Calculating Sin in Mathematica

But if I calculate it on site wolframalpha.com, I obtain correct result:

Calculating Sin on site wolframalpha.com

Tell me, please, how can I obtain correct calculating in Mathematica like on site wolframalpha.com?

I will be very appreciative for your answer!

$\endgroup$
  • 8
    $\begingroup$ That is called floating point error. Don't avoid it. You need to understand why it exists. If you want to get the answer that Wolfram|Alpha gives, use Rational numbers. Instead of using (0.5), use (1/2). $\endgroup$ – Searke Apr 15 '16 at 1:29
  • 2
    $\begingroup$ To improve accuracy in many Mathematica calculations, use rational numbers instead of decimals. In this case, use 1/2 instead of 0.5. $\endgroup$ – bbgodfrey Apr 15 '16 at 1:35
  • $\begingroup$ Thank you very much for your answers! I guessed, that root of this error is machine precision, but I couldn't understand, how to avoid this error, if it is possible in case of site version Mathematica. Now I understand how to realise it. $\endgroup$ – Roman Alexeev Apr 15 '16 at 2:16
13
$\begingroup$

Numerics in Mathematica can be as precise as you like. However, precision comes at price; you pay for it in computation time and in additional coding effort.

In Mathematica there are several computational classes of non-complex numbers, which form a tree like this.

tree

The computation you made was made with machine reals because you included 0.5 as a term. Mathematica always performs any computation expressed with even one machine number term using machine (CPU) arithmetic because that is the fastest way to compute for numerical problems.

Now let's do at your computation with rationals and arbitrary precision numbers as well as machine numbers. I have chosen precision of 50 decimal places for this demonstration.

result = Sin[2 π {.5, 1/2, .5`50}]

result

Wolfram|Alpha probably applied Chop to its answer. Let's do that too.

result // Chop

{0, 0, 0}

Now all three results agree with Wolfram|Alpha.

The way that the machine arithmetic result is expressed is useful; it informs you of the error that your computer's built-in machine floating-point arithmetic produces.

$\endgroup$
  • 4
    $\begingroup$ I upvoted and got what you were saying in the tree diagram, but I also feel compelled to point out that π does not belong to any of the leaves in your tree. $\endgroup$ – J. M. will be back soon Apr 15 '16 at 4:42
  • $\begingroup$ @J.M. True. That is because both InexactNumberQ[π] and ExactNumberQ[π] give False, so where in the tree would I put it? Such things satisfy NumericQ but not NumberQ and are a special class of forms but not numbers as I see it. $\endgroup$ – m_goldberg Apr 15 '16 at 15:41
  • $\begingroup$ I guess you need to add a level, root the tree at numeric, with branches to number , symbolic (eg pi) , and expressions (eg Sqrt[2] ) $\endgroup$ – george2079 Apr 15 '16 at 18:05
  • $\begingroup$ Thank you very much for your comprehensive answer! $\endgroup$ – Roman Alexeev Apr 15 '16 at 23:39

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.