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.
> 

Here's yet another way to do it:

sage: import mpmath
sage: b=mpmath.mpf(exp(1.0))
sage: b
mpf('2.7182818284590451')
sage: b.man # mantissa
6121026514868073
sage: b.exp # exponent
-51

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