MathGroup Archive 2008

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

Search the Archive

Re: On default numerical precision

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90719] Re: On default numerical precision
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 21 Jul 2008 04:28:45 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g5pj07$qbc$1@smc.vnet.net> <g5s9q0$51h$1@smc.vnet.net> <g5v5nr$ibi$1@smc.vnet.net>

sergio_r at mail.com wrote:

> Jens thanks for you reply. I am not sure what
> I am missing but here are the results of following
> the sugested approach:
> 
> In[1]:= $Post = ( # /. a_Real :> NumberForm[a, {32, 16}]) &;
> 
> In[2]:= a=2.
> 
> Out[2]//NumberForm= 2.
> 
> In[3]:= b=3.
> 
> Out[3]//NumberForm= 3.
> 
> In[4]:= a*b
> 
> Out[4]//NumberForm= 6.

What version of Mathematica do you use? Jens-Peer's solution works fine 
on my system (64-bit Intel Core 2 Duo Mac OS X Leopard 1.5.4 Mathematica 
6.0.3).

     In[1]:= $Post =.
             $Post = #1 /. a_Real :> NumberForm[a, {32, 30}] & ;
             a = 2.
             Precision[%]
             Precision[a]
             a = 0.3
             b = 0.7
             a*b

     Out[3]//NumberForm= 2.000000000000000000000000000000

     Out[4]= MachinePrecision

     Out[5]= MachinePrecision

     Out[6]//NumberForm= 0.300000000000000000000000000000

     Out[7]//NumberForm= 0.700000000000000000000000000000

     Out[8]//NumberForm= 0.210000000000000000000000000000


> But the following form works the way I was looking
> for:
> 
> In[5]:= $Post =.
> 
> In[6]:= $Post
> 
> Out[6]= $Post
> 
> In[7]:= $Post = ( # /. a_Real :> SetPrecision[a, 32]) &;
> 
> In[8]:= a=2.
> 
> Out[8]= 2.0000000000000000000000000000000
> 
> In[9]:=  b=3.
> 
> Out[9]= 3.0000000000000000000000000000000
> 
> In[10]:= a*b
> 
> Out[10]= 6.0000000000000000000000000000000

Are you hundred percent sure that changing the precision of result is 
the thing to do if what you want is just more zeros to your display?

What the above does is evaluating a numeric expression according to the 
rules and precision of hardware floating-point arithmetic (lowest 
precision but fastest computational model out of the three Mathematica 
implements), then Mathematica converts the result/output to higher 
precision according to the arbitrary-precision model.

In other words, you upcast real numbers from lower to higher precision 
which might lead to some surprising result to say the least. (Be sure to 
test thoroughly your solution before using it for real work.)

     In[9]:= $Post =.
             $Post = #1 /. a_Real :> SetPrecision[a, 30] & ;
             a = 2.
             Precision[%]
             Precision[a]
             a = 0.3
             b = 0.7
             a*b

     Out[11]= 2.00000000000000000000000000000

     Out[12]= 30.0000000000000000000000000000

     Out[13]= MachinePrecision

     Out[14]= 0.299999999999999988897769753748

     Out[15]= 0.699999999999999955591079014994

     Out[16]= 0.209999999999999992228438827624


> Thanks again,
> 
> Sergio
> 
> 
> On Jul 19, 4:49 am, Jens-Peer Kuska <ku... at informatik.uni-leipzig.de>
> wrote:
>> Hi,
>>
>> $Post = ( # /. a_Real :> NumberForm[a, {32, 16}]) &;
>>
>> Regards
>>    Jens
>>
>> sergi... at mail.com wrote:
>>> Hello all,
>>> I am wonderin on how to change the mathematica default printing
>>> precision, so I don't have to type on any defined constant the
>>> required  precision, like " `30 "  in:
>>> In[1]:= a=2.3`30; b=2.1`30;
>>> In[2]:= a*b
>>> Out[2]= 4.83000000000000000000000000000
>>> I would like  to be able to do:
>>> a=2.3; b=2.1; and obtain
>>> a*b = 4.83000000000000000000000000000
>>> Regards,
>>> Sergio
>>> PS. Apparently the internal variable $Pre could be set to do that, but
>>> the respective help page [  http://reference.wolfram.com/mathematica/ref/$Pre.html
>>>    ] is not clear about how to do that.
> 
> 

Regards,
-- Jean-Marc


  • Prev by Date: Re: Problems with FindMinimum
  • Next by Date: Re: Installing Personal Package Documentation not showing up
  • Previous by thread: Re: On default numerical precision
  • Next by thread: Re: On default numerical precision