MathGroup Archive 2008

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

Search the Archive

Re: Is there a simple way to transform 1.1 to

So Andrzej Kozlowski proposed:

    In[1]:= rAndrzej[z_] := With[{w = RealDigits[z]},FromDigits[MapAt[# /. {x___, a_ /;  a != 0, 0 ...} :> {x, a} &, w, 1]]]

Someone else proposed in private:

    In[2]:= rSH[x_Real] := FromDigits[#1]/10^(Length[#1] - #2) & @@ RealDigits[x]

Bill Rowe proposed:

    In[3]:= rBill[x_] := Module[{r = Rationalize[x, 0], p}, p = Round@Log[10, Denominator[r]]; Round[r 10^p]/10^p]

They all performed well on early isolated trials, but more systematic
tests show failures:

    In[4]:= x=1.001;{rAndrzej[x],rSH[x],rBill[x]}

	     1000999999999999  1000999999999999  1001
    Out[4]= {----------------, ----------------, ----}
	     1000000000000000  1000000000000000  1000

    In[5]:= x=1.0000000000000001;{rAndrzej[x],rSH[x],rBill[x]}

    Out[5]= {1, 1, 1}

Then, when getting closer to 1, the first 2 give the expected result,
while the 3rd one gives sometimes the expected result, sometimes 1.

I found a way to do what I want, but only by starting from a string,
i.e., I have to enclose the number between double quotes, which is
impractical in general.

I could not find a general way to transform a number to a
string. Specifically, is there a way to transform the above number
1.0000000000000001 (fifteen zeros), which seems to be the most
problematic, to the corresponding string "1.0000000000000001" ?


  • Prev by Date: Re: Expressions with ellipsis (...)
  • Next by Date: Contourplot3d of a list of expressions
  • Previous by thread: Re: How does Mathematica know whether a number is real or complex?
  • Next by thread: Re: Is there a simple way to transform 1.1 to 11/10