# How to calculate accurate answer in Mathematica?

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: But if I calculate it on site wolframalpha.com, I obtain correct result: Tell me, please, how can I obtain correct calculating in Mathematica like on site wolframalpha.com?

• 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). Apr 15, 2016 at 1:29
• To improve accuracy in many Mathematica calculations, use rational numbers instead of decimals. In this case, use 1/2 instead of 0.5. Apr 15, 2016 at 1:35
• 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. Apr 15, 2016 at 2:16

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. 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, .550}] 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.

• 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. Apr 15, 2016 at 4:42
• @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. Apr 15, 2016 at 15:41
• I guess you need to add a level, root the tree at numeric, with branches to number , symbolic (eg pi) , and expressions (eg Sqrt` ) Apr 15, 2016 at 18:05
• Thank you very much for your comprehensive answer! Apr 15, 2016 at 23:39