1
$\begingroup$

I apologize upfront for the simple minded question, however i couldn't find an answer in either the Mathematica documentation or Stack exchange.

I am trying to explore the effect of finite precision in Mathematica, however i seem to miss how to limit calculation precision . Here is a trivial example . Say i want to calculate the n'th power of x^n using various precessions. I tried something like this, however while the output becomes truncated the calculation is clearly not limited (see the result from two 2) ?

what am i missing, or better how would this be done correct ?

SetPrecision[Pi, 3]
SetPrecision[SetPrecision[Pi, 3]^8, 15]
SetAccuracy[Pi, 3]^8
Pi^8 // N
3.14^8
$\endgroup$
1
  • $\begingroup$ among other things note the precision of the literal 3.14 is not 3, but is MachinePrecision. You should add 3.14`3^8 to your test list $\endgroup$
    – george2079
    Sep 10, 2015 at 18:46

1 Answer 1

7
$\begingroup$

Most Mathematica functions have no side-effects. This means that SetPrecision returns a version of its input in which all numbers have been set to a certain precision, but it does not influence the precision of the arguments themselves. In other words, it is the output of SetPrecision that has the requested precision; you will want to e.g. explicitly save the reduced-precision result in another variable in order to use it.

See the difference e.g. between the following expressions

pilow = SetPrecision[Pi, 3]
(* Out: 3.14 *)

N[Pi^8]
(* Out: 9488.53 *)

pilow^8
(* Out: 9.5*10^3 *)

3.14`3^8 (* this has the same precision as pilow *)
(* Out: 9.5*10^3 *)

3.14^8 (* this number actually has $MachinePrecision digits of precision *)
(* Out: 9450.12 *)

The precision of these results can be probed explicitly:

Precision[Pi^8]      (* Infinity         *)
Precision[N[Pi^8]]   (* MachinePrecision *)
Precision[pilow^8]   (* 2.09691          *)
Precision[3.14^8]    (* MachinePrecision *)
Precision[3.14`3^8]  (* 2.09691          *)

Alternatively, one can read the precision off e.g. the FullForm of any numerical result:

FullForm[pilow]      (* 9488.5310160705740071286`2.0969100130080567 *)
$\endgroup$
5
  • $\begingroup$ Thx a lot Marco, however to be honest your answer sounds puzzling to me. For one would you also say the instead of Sort[2.0]^8 i should use a= Sort[2.0]; a^2 ? I think the whole notion of Functional programming is to daisy chain functions ? Beside that your suggestion still gives the unexpected value that the limited precision calculation yields the full precision result ? $\endgroup$
    – bernddude
    Sep 11, 2015 at 15:31
  • $\begingroup$ 1) I am not sure what Sort[2.0] would achieve, but e.g. f[x]^2 is in fact daisy-chaining functions, i.e. you obtain Power[f[x], 2], which you could equivalently write as Power[#, 2]& @f[x] to highlight the daisy-chain: this calculates the return value of f when applied to x, then feeds the return value to Power. Am I missing your point? 2) The limited precision calculation does not yield a full precision result. You can convince yourself of that by noting the precision digits associated with the result in FullForm[pilow^8], or explicitly by running Precision[pilow^8]. $\endgroup$
    – MarcoB
    Sep 11, 2015 at 17:37
  • $\begingroup$ @bernddude Please see my comment above, and the edit to my answer. $\endgroup$
    – MarcoB
    Sep 11, 2015 at 17:43
  • $\begingroup$ Marco, thx a lot once again. Sorry for the typo, spellchecker feature Sqrt-> Sort nice :). I do under stand your point, I feel its more that SetPrecision does behave different then i would expect . Below is one more example N[Pi, 20] SetPrecision[Pi, 3] pillow = SetPrecision[Pi, 3] FullForm[N[Pi^8]] pillow^8 FullForm[pillow^8] FullForm[3.14^8] pillow^8 - 3.14^8 the last line gives the result 38.414 $\endgroup$
    – bernddude
    Sep 12, 2015 at 14:41
  • $\begingroup$ Marco here might be a better example of what i am trying to say . p = 200 e = .996 ep = SetPrecision[e, 2] SetPrecision[e^p, 3] e^p ep^p SetPrecision[ep, 20]^p i would expect for ep^100 to get 1 after the assignment is done. Obviously this isn't wrong however in order to "simulate" the behavior of a finite precision embedded system, its not that useful . $\endgroup$
    – bernddude
    Sep 12, 2015 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.