MathGroup Archive 2007

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

Search the Archive

Re: Next representable number


Andrew Moylan wrote:
> What's a neat way to determine the next (higher) representable number
> (at a given precision)? That is, I want f[x] to be the smallest
> representable number (at precision Precision[x]) that's larger than x.
> 

I'm not sure this is quite correct but here goes.

(1) Convert to binary using RealDigits.

(2) Add one to the last "bit" in the mantissa. No matter if it 
"overflows"; the reverse conversion won't mind. This feature caused 
remarkable controversy in-house a few months ago.

(3) Convert from {mantissa,exponent} to a rational using FromDigits.

(4) Numericize to the original precision.

nextRepresentable[x_Real] := Module[
   {mant, expon, prec=Precision[x]},
   {mant,expon} = RealDigits[x];
   N[FromDigits[{Append[Most[mant],Last[mant]+1],expon}], prec]
   ]

In[40]:= ee = N[7/4,22]
Out[40]= 1.750000000000000000000

In[41]:= ff = nextRepresentable[ee]
Out[41]= 1.750000000000000000001

In[42]:= InputForm[ff-ee]
Out[42]//InputForm= 1.`0.45593195564972433*^-21


Daniel Lichtblau
Wolfram Research


  • Prev by Date: Re: NonLinearRegression Weights
  • Next by Date: Fw: Maclaurin series for ArcCosh[x]
  • Previous by thread: Next representable number
  • Next by thread: Re: Next representable number