Discussion:
SetPrecision vs N
(too old to reply)
Andrew Moylan
2006-11-26 09:27:57 UTC
Permalink
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
Andrzej Kozlowski
2006-11-27 09:24:39 UTC
Permalink
Post by Andrew Moylan
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
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
They 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
Andrew Moylan
2006-11-28 11:19:45 UTC
Permalink
Thanks Andrzej for your clear explanation.

Andrew
Post by Andrzej Kozlowski
Post by Andrew Moylan
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
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
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.
x = N[Pi - 22/7, 2]
-0.0012644892673496187`1.9999999999999998
y = SetPrecision[Pi - 22/7, 2]
0``1.2017327387937378
{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
Jean-Marc Gulliet
2006-11-27 09:28:03 UTC
Permalink
Post by Andrew Moylan
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]?
. N[expr, n] attempts to give a result with n‐-digit precision.

==> N[] does *NOT* change the precision of an expression. That is, a
machine-precision number is still a machine-precision number.

. SetPrecision[expr, p] yields a version of expr in which all numbers
have been set to have precision p.

==> SetPrecision[] *CHANGES* the precision of the expression, paddding
numbers with additional zeros if needed (in base 2). Therefore, having
used SetPrecision[], you have change the computational model from
machine arithmetic to arbitrary-precision arithmetic.

So, for less than 16-digit precision (typical 32-bit machine) both
function might appear equivalent, although they are not because the
computational model is different. And to work with higher precision,
you must use arbitrary precision, that is SetPrecision[] (N[] is
hopeless in this case.)

In[1]:=
val = 1./3

Out[1]=
0.333333

In[2]:=
Precision[N[val]]

Out[2]=
MachinePrecision

In[3]:=
Precision[N[val, 15]]

Out[3]=
MachinePrecision

In[4]:=
Precision[N[val, 20]]

Out[4]=
MachinePrecision

In[5]:=
Precision[SetPrecision[val, 15]]

Out[5]=
15.

In[6]:=
Precision[SetPrecision[val, 20]]

Out[6]=
20.

Regards,
Jean-Marc
Peter Pein
2006-11-27 09:43:13 UTC
Permalink
Post by Andrew Moylan
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
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)).


Bruce Colletti
2006-11-28 11:20:47 UTC
Permalink
Why does the code below return True? Bruce

-----------------

x=N[Pi-22./7,30]
y=SetPrecision[Pi-22./7,40]
Precision/@{x,y}
x==y

-0.00126449

-0.001264489267349677703577981446869671344757

{MachinePrecision,40.}

True

=====================
From: Andrzej Kozlowski <***@mimuw.edu.pl>
Date: 2006/11/27 Mon AM 03:03:57 CST
To: ***@smc.vnet.net
Subject: SetPrecision vs N
Post by Andrew Moylan
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
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
They 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
David Park
2006-11-28 11:23:49 UTC
Permalink
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
***@earthlink.net
http://home.earthlink.net/~djmp/

David Park
***@earthlink.net
http://home.earthlink.net/~djmp/
Post by Andrew Moylan
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
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)).



Loading...