Re: Random[Real,{0,1},$MachinePrecision] isn't MachineNumber[] ???!!
- Subject: [mg932] Re: Random[Real,{0,1},$MachinePrecision] isn't MachineNumber[] ???!!
- From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
- Date: Wed, 3 May 1995 00:11:17 -0400
- Apparently-to: mathgroup-send at christensen.cybernetics.net
- Organization: University of Colorado, Boulder
In article <3nn86p$9pq at news0.cybernetics.net>, Paul A. Rubin <rubin at msu.edu> wrote: >In article <3nkbjg$mq8 at news0.cybernetics.net>, > pehowland at taz.dra.hmg.gb (Paul E. Howland) wrote: >-> >->When I generate a random number, with machine precision, as below >->In[1] := Random[Real,{0,1},$MachinePrecision] >->Out[1] := 0.3739679783533225 >-> >->Indeed, if I ask >->In[2] := MachineNumberQ[%1] >->Out[2] := False >-> >->!@@?!##$% >-> >->If using $MachinePrecision with Random[] doesn't give me a >MachinePrecision >->number, what does?! >-> >->However, if I use: >->In[3] := N[Random[Real,{0,1}],$MachinePrecision] >->Out[3] := 0.98857067609012 >->In[4] := MachineNumberQ[%] >->Out[4] := True >-> > >I did a little experiment, cranking out randoms with Random[ Real, {0, 1}, > >From this it would appear that the third argument to Random[] is being >interpreted as an accuracy target, not a precision target (the wording of >the textbook and on-line help notwithstanding). In contrast, N[] >interprets the precision target correctly (as precision, not accuracy). > >The only >part I'm not clear on is why Random[] outputs with accuracy and precision >both equal to $MachinePrecision still failed the test. More precisely (no >pun intended), I wonder what it is about them that allows Mma to >distinguish them from numbers generated by N[], also with accuracy and >precision equal to $MachinePrecision, which MachineNumberQ[] accepts. It >must be something in the internal representation of the numbers. Their >heads are Real in both cases. Here is the answer to your question: In[14]:= x1 = Random[Real, {0,1}, $MachinePrecision] Out[14]= 0.3681151324784739 In[21]:= x2 = N[x1] Out[21]= 0.368115 In[22]:= MachineNumberQ /@ {x1, x2} Out[22]= {False, True} Now what's happening here is that there are two kinds of Real numbers used in Mathematica: *fixed-precision* floating-point, and *variable-precision* floating-point. The former are known as machine numbers; their precision never changes no matter how unjustifiable it is. The latter are called bignums. The internal representation of a bignum is different. Just because a bignum has $MachinePrecision precision, that doesn't make it a machine number. The undocumented function $NumberBits proves this: In[29]:= ?$NumberBits $NumberBits[x] gives a list containing the sign, a list of the bits thought to be correct, a list of the bits not thought to be correct, and the binary exponent. The number x must have a head of Real. In[23]:= $NumberBits[x1] Out[23]= {1, {0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0} In[24]:= $NumberBits[x2] Out[24]= {1, {}, {1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1}, -1} All of this stuff is discussed in the papers by Keiper called "Numerical Computation I" and "Numerical Computation II" (pretty catchy, eh?), which are contained in WRI's "Selected Tutorial Notes." NC II discusses precision issues, whereas NC I discusses the algorithms that are used by the various N functions. There are also earlier versions of these papers available on MathSource. I can't recommend the Tutorial Notes highly enough. I think I answer about one question a week in this group straight out of that book. The two papers by Keiper alone are worth the price of the book. (NC I is over 80 pages long and is a motherlode of information.) There's also ~100 pages about graphics by Tom Wickham-Jones. The answer to Paul Howland's original question is simply this: don't specify a precision for Random! In[25]:= MachineNumberQ[Random[Real, {0,1}]] Out[25]= True Dave Wagner Principia Consulting (303) 786-8371 princon at csn.net http://www.csn.net/princon