MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: SetPrecision vs N

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71745] RE: [mg71714] Re: SetPrecision vs N
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 28 Nov 2006 06:03:58 -0500 (EST)

The purpose of N is to convert EXACT numbers to approximate numbers with a
given precision. N does nothing whatsoever on approximate numbers. So a
statement like N[1.1, 1000] just leaves 1.1 unchanged.

But

N[Pi, 50]
Precision[%]
3.1415926535897932384626433832795028841971693993751
50.

does something because Pi is an exact number.

But it appears that SetPrecision works on both approximate and exact
numbers.

SetPrecision[Pi, 50]
3.1415926535897932384626433832795028841971693993751

So if one wants to really set the precision of numbers, it is best to use
SetPrecision, and just use N for converting exact to approximate numbers.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

From: Peter Pein [mailto:petsie at dordos.net]


Andrew Moylan schrieb:
> 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,
> Andrew
>

Hi Andrew,

the most obvious difference is:

Precision[N[1.1, 1000]]
--> MachinePrecision

vs.

Precision[SetPrecision[1.1, 1000]]
-->1000.

I guess, SetPrecision[#,prec]& automagically applies N[#,prec]& to an
expression having greater precision than prec (especially when applied to
exact expressions (which got infinite precision)).

P²



  • Prev by Date: Re: Descending order
  • Next by Date: Unexpected warning with ArcTan
  • Previous by thread: Re: SetPrecision vs N
  • Next by thread: Re: Re: SetPrecision vs N