Re: Is there a simple way to transform 1.1 to 11/10?
- To: mathgroup at smc.vnet.net
- Subject: [mg92994] Re: Is there a simple way to transform 1.1 to 11/10?
- From: Szabolcs Horvat <szhorvat at gmail.com>
- Date: Wed, 22 Oct 2008 05:39:01 -0400 (EDT)
- Organization: University of Bergen
- References: <gdkajv$4r2$1@smc.vnet.net>
Alain Cochard wrote: > The obvious > > In[1]:= x=1.1`Infinity > > is not syntactically correct. > > I understand that SetPrecision[1.1,Infinity] does not work either: > > In[3]:= SetPrecision[1.1,Infinity] > > 2476979795053773 > Out[3]= ---------------- > 2251799813685248 > > In[4]:= N[%,20] > > Out[4]= 1.1000000000000000888 > > I searched the newsgroup and thought I had the solution with Rationalize: > > In[5]:= Rationalize[1.1,0] > > 11 > Out[5]= -- > 10 > > But > > In[9]:= Rationalize[1.000000001,0] > > 999999918 > Out[9]= --------- > 999999917 > > In[10]:= N[%,20] > > Out[10]= 1.0000000010000000830 > > So any simple way? > Hello Alain, Rationalize is the way to go. Floating point numbers are usually stored in a binary (not decimal) representation on computers. 1.000000001 is not exactly representable in binary (in the same way as 1/3 = 0.3333333... is not exactly representable in decimal). Note that in your example you start with a MachinePrecision number (approximately 15 digits), and then convert back to a number with 20 digits of precision. If you start with 1.000000001`20 then everything will be fine. In[1]:= Rationalize[1.000000001`20, 0] Out[1]= 1000000001/1000000000 In[2]:= N[%, 20] Out[2]= 1.0000000010000000000 Another example: In[1]:= Rationalize[N[Sqrt[2], 30], 0] Out[1]= 1023286908188737/723573111879672 In[2]:= N[%, 30] Out[2]= 1.41421356237309504880168872421 In[3]:= % - Sqrt[2] Out[3]= 0.*10^-30