Re: SetPrecision vs N
- To: mathgroup at smc.vnet.net
- Subject: [mg71741] Re: SetPrecision vs N
- From: "Andrew Moylan" <andrew.j.moylan at gmail.com>
- Date: Tue, 28 Nov 2006 06:03:54 -0500 (EST)
- References: <200611260849.DAA14687@smc.vnet.net><ekeasn$st1$1@smc.vnet.net>
Thanks Andrzej for your clear explanation. Andrew On Nov 27, 8:24 pm, Andrzej Kozlowski <a... at mimuw.edu.pl> wrote: > On 26 Nov 2006, at 17:49, Andrew Moylan wrote: > > > > > Hi all, > > > Suppose I want to evaluate an expression at a given precision. What is > > the difference between using N[expr, precision] and using > > SetPrecision[expr, precision]? > > > I've noticed that SetPrecision seems to be equivalent even in such > > situations as e.g. N[Integrate[...]] automatically calling > > NIntegrate[...] when the integral can't be done exactly: > > > SetPrecision[Integrate[x^x, {x, 0, 1}], 20] > > and > > N[Integrate[x^x, {x, 0, 1}], 20] > > both give > > 0.78343051071213440706 > > > Are there important differences between SetPrecision and N that I > > should be aware of? > > > Cheers, > > AndrewThey are actually completely different (though sometimes the effect > may look the same). N[expr,p] computes expression to precision p. If > expr is an exact expression Mathematica uses significance arithmetic > to determine what precision it should use for the input to give you > the output with p digits of precision. SetPrecison does nothing but > simply changes the precision of expression to p, either by padding it > with 0 or truncating. To see the difference just compare these two > examples: > > SetPrecision[2.2, 20] > > 2.20000000000000017763568394002504646778`19.999999999999996 > > 2.2 was (essentially) padded to precision of around 20. > > N[2.2, 20] > > 2.2 > > Nothing changed because N cannot compute a machine precision number > with extended precision. > > Here is a different example: > > x = N[Pi - 22/7, 2] > > -0.0012644892673496187`1.9999999999999998 > > y = SetPrecision[Pi - 22/7, 2] > > 0``1.2017327387937378 > > Precision/@{x,y} > > {2.,0.} > > Here x is the value of Pi - 22/7 computed to give you two significant > digits, whereas y has actually precision 0, because what it does is > this: SetPrecision[Pi, 2] - SetPrecision[22/7, 2], which for obvious > reasons has precision 0. > > Andrzej Kozlowski > > Tokyo, Japan
- References:
- SetPrecision vs N
- From: "Andrew Moylan" <andrew.j.moylan@gmail.com>
- SetPrecision vs N