Re: Preserve natural number "e" using N or SetPrecision
- To: mathgroup at smc.vnet.net
- Subject: [mg30922] Re: Preserve natural number "e" using N or SetPrecision
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Thu, 27 Sep 2001 02:16:39 -0400 (EDT)
- References: <9o973r$cfd$1@smc.vnet.net> <9oc76p$fue$1@smc.vnet.net> <9ojung$m0k$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Kevin,
You may be seeing something like:
with Block:
NSaveE[expr_, prec___] := Block[{E = ee},
N[expr, prec]
] /. ee -> E
NSaveE[E]
E
but, with Module
NSaveEMod[expr_, prec___] := Module[{E = ee},
N[expr, prec]
] /. ee -> E
NSaveEMod[E]
2.718281828459045
This is because:
When NSaveEMod[E] is evaluated,
the E in Module[{E=ee}, body] is protected against being confused with any E
coming from outside, by being renamed to E$ (this is to do with scoping).
Only after this is E from NSaveEMod[E] passed to replace expr in Module[..],
so that we have
Module[{E$ = ee},
N[E]
] /. ee -> E
Which is then evaluated and gives the result that you see.
You can follow the process by evaluating
TracePrint[NSaveEMod[E]]
Three further points:
1) we have automatic changes like
{1.2 E, E^1.2}
{3.261938194150854, 3.3201169227365472}
To deal with these may need some holding or a dummy version of E.
2) N works on held expressions, for example:
N[HoldComplete[E]]
HoldComplete[2.718281828459045]
But NSaveE, and NSaveMod, work with assignments they do not work on held
expressions.
For example
NSaveE[HoldComplete[E]]
HoldComplete[2.718281828459045]
We can get round this by using replacement
NSaveERep[expr_,prec___]:=
Module[{ee},
N[expr/.E->ee,prec]/.ee->E
]
NSaveERep[HoldComplete[E]]
HoldComplete[E]
But we still have
NSaveERep[Pi E]
8.539734222673566
A simple, dummy E, solution is
NSaveERepDum[expr_,prec___]:=
N[expr/.{E:>ee},prec]
NSaveERepDum[Pi E]
3.141592653589793*ee
A holding solution is
NSaveERepHF[expr_,prec___]:=
Module[{ee},
N[expr/.E->ee,prec]/.ee->HoldForm[E]
]
NSaveERepHF[Pi E]
3.141592653589793*HoldForm[E]
3) Another problem to do with holding
Hold[Exp[Pi]]/.E->ee
Hold[E^Pi]
This is because it is only during evaluation that Exp[x] is converted to
E^x.
(the following is misleading
Hold[Exp[Pi]]
Hold[E^Pi]
we have in fact
FullForm[Hold[Exp[Pi]]]
FullForm[Hold[Exp[Pi]]]
)
This may not seem too bad since we have, for example
NSaveERepDum[Hold[Exp[Pi]]]
Hold[E^3.141592653589793]
But if may preferr to have a uniform treatment , for example:
NSaveERepDum2[expr_,prec___]:=
N[expr/.E:>ee, prec]//.HoldPattern[Exp[x_]]:>ee^x
NSaveERepDum2[Hold[Exp[Pi]]]
Hold[ee^3.141592653589793]
(this may need more work)
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Kevin J. McCann" <kevinmccann at Home.com> wrote in message
news:9ojung$m0k$1 at smc.vnet.net...
> I get a very strange error if I replace Block with Module in the function
> below.
>
> "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de> wrote in message
> news:9oc76p$fue$1 at smc.vnet.net...
> > Hi,
> >
> > NSaveE[expr_, prec___] := Block[{E = ee},
> > N[expr, prec]
> > ] /. ee -> E
> >
> >
> > will do it.
> >
> > Regards
> > Jens
>
>
>