Dr. David Kirkby wrote:
> The IEEE 754 representation of a floating point number is basically
> 
> (-1)^2 x c x 2^q
> s=sign bit
> c=significand (or 'coefficient')
> q=exponent
> 
> http://en.wikipedia.org/wiki/IEEE_754-2008
> 
> E is most accurately represented by:
> 
> 6121026514868073 x 2^-51
> 
> though on my SPARC the best one gets is
> 
> 6121026514868074 x 2^-51
> 
> I wanted to know how to convert a floating point number to that format using 
> Mathematica. The very knowledgeable and helpful Daniel Lichtblau of Wolfram 
> Research answered my question on sci.math.symbolic. I must admit I was 
> impressed 
> how few lines of code it took him.
> 
> $ cat RealToIEEE754.m
> 
> RealToIEEE754[x_]:=Block[{s,digits,expon,c,q},
> s=Sign[x];
> {digits,expon}=RealDigits[x,2];
> c = FromDigits[digits, 2] ;
> q = expon - Length[digits] ;
> {s,c,q} ]
> 
> 
> In[1]:= <<RealToIEEE754.m
> 
> In[2]:= RealToIEEE754[Exp[1.]]
> 
> Out[2]= {1, 6121026514868074, -51}
> 
> 
> I was wondering how elegant one could implement that in Sage.
> 


See http://trac.sagemath.org/sage_trac/ticket/7830 for a patch for the 
following functionality:

              sage: R=RealField(53)
              sage: a=R(exp(1.0)); a
              2.71828182845905
              sage: sign,mantissa,exponent=R(exp(1.0)).representation()
              sage: sign,mantissa,exponent
              (1, 6121026514868073, -51)
              sage: sign*mantissa*(2**exponent)==a
              True

I'm not happy with the name ("representation").  What do people think?

Thanks,

Jason


-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to