Re: Next representable number
- To: mathgroup at smc.vnet.net
- Subject: [mg73627] Re: [mg73542] Next representable number
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Fri, 23 Feb 2007 04:38:41 -0500 (EST)
- References: <200702210653.BAA18712@smc.vnet.net>
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
- References:
- Next representable number
- From: "Andrew Moylan" <andrew.j.moylan@gmail.com>
- Next representable number