1
$\begingroup$

I often see different solutions where users have taken advantage of how Mathematica handles numbers differently. I.e. when is it appropriate to use a decimal point in a calculation?

Can someone explain when I should use a decimal place in a numerical calculation, for example in a table Table[SomeCalc,{x,0,10}] or Table[SomeCalc,{x,0.,10.}]?

What are the advantages and are there any impacts on numerical accuracy, especially when dealing with very large or small numbers?

$\endgroup$
2
$\begingroup$

As an example, make a complicated function:

f[y_] = x /. Solve[x^4 - 10 x + y == 0, x, Quartics -> True][[4]];

Note the use of = to force evaluation of the expression.

Now,

Table[f[x], {x, 0., 10.}]
(* {2.15443, 2.12001, 2.08315, 2.04337, 2., 1.95208, 1.89815, 1.8358, 
    1.76041, 1.6608, 1.48072} *)

But,

Table[f[x], {x, 0, 10}] // LeafCount
(* 1500 *)

I only show LeafCount because the expression is a monster.

If you give Mathematica's symbolic machinery exact input like 10, it will yield an exact result. Often, this result cannot be expressed as a decimal number, so it must be expressed in a more complicated way. On the other hand, Mathematica treats any number containing a decimal point as approximate, not exact, and thus calculates an approximate result that it displays in decimal.

On the other hand, if you feed expressions containing approximate numbers like 10. to symbolic manipulation functions like Solve or Integrate, sometimes you'll get nonsense. The internals of these functions implicitly assume exact arithmetic. It's usually best to use exact numbers as input in these cases.

$\endgroup$
3
$\begingroup$

The documentation on the Mathematica website is pretty strong in this area. Try this link for help https://reference.wolfram.com/language/tutorial/NumericalPrecision.html

Most of it is simply how you want your output to look(symbolic, precise to a certain number of digits, etc)

$\endgroup$
1
$\begingroup$

Use decimals whenever you want to enforce machine precision computations. Moreover, this may enable Mathematica to store the interpediate data and the output in so-called packed arrays which can be handle more efficiently when it is about number crunching.

The advantage of machine precision numbers is a tremendous speedup as your machine has physical circuits that are optimized for such computations. Large scale applications can often handled only in machine precision.

The drawback is that the precision handling is quite different to Mathematica's arbitrary precision arithmetic: Mathematica can refine the working precision if necessary to produce results whose actual precision is close to PrecisionGoal because she internally keeps track of estimators for the computational errors that arise during computations. The floating point unit in your CPU does not.

For most applications and with a descently careful style of programming, machine precision is more than sufficient. However, some problems need additiona care. It is actually a nonnegligible part of the art of numerics to develop algorithms that produce accurate results also with machine precision floating point numbers.

$\endgroup$

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.