How much code to convert to IEEE 754 format ?

45 views
Skip to first unread message

Dr. David Kirkby

unread,
Jan 3, 2010, 12:00:32 AM1/3/10
to sage-...@googlegroups.com
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.

Robert Bradshaw

unread,
Jan 3, 2010, 12:22:31 AM1/3/10
to sage-...@googlegroups.com

sage: def realToIEEE754(x):
....: if x == 0: return 0, 0, 0
....: valuation, unit = x.exact_rational().val_unit(2)
....: return x.sign(), unit, valuation
....:
sage: realToIEEE754(exp(1.0))
(1, 6121026514868073, -51)

It is sad we don't have a top-level sign function.

- Robert

Mike Hansen

unread,
Jan 3, 2010, 12:26:42 AM1/3/10
to sage-...@googlegroups.com
On Sat, Jan 2, 2010 at 11:22 PM, Robert Bradshaw
<robe...@math.washington.edu> wrote:
> It is sad we don't have a top-level sign function.

We do, but it's called sgn.

sage: sgn(3.0)
1

--Mike

David Kirkby

unread,
Jan 3, 2010, 12:35:37 AM1/3/10
to sage-...@googlegroups.com
2010/1/3 Mike Hansen <mha...@gmail.com>:

Was there a good reason for choosing the name 'sgn'? It sems a bit
strange to me.

Dave

Alex Ghitza

unread,
Jan 3, 2010, 12:49:29 AM1/3/10
to sage-...@googlegroups.com
On Sun, 3 Jan 2010 05:35:37 +0000, David Kirkby <david....@onetel.net> wrote:
>
> Was there a good reason for choosing the name 'sgn'? It sems a bit
> strange to me.
>

That's the standard mathematical notation for this function, see

http://en.wikipedia.org/wiki/Sign_function


Best,
Alex


--
Alex Ghitza -- Lecturer in Mathematics -- The University of Melbourne
-- Australia -- http://www.ms.unimelb.edu.au/~aghitza/

Jason Grout

unread,
Jan 3, 2010, 2:44:16 AM1/3/10
to sage-...@googlegroups.com


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


Jason Grout

unread,
Jan 3, 2010, 2:53:05 AM1/3/10
to sage-...@googlegroups.com

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

David Kirkby

unread,
Jan 3, 2010, 8:33:17 PM1/3/10
to sage-...@googlegroups.com
2010/1/3 Jason Grout <jason...@creativetrax.com>:


OK, Mathematica is no shorter than Sage in this respect.

dave

David Kirkby

unread,
Jan 3, 2010, 8:35:23 PM1/3/10
to sage-...@googlegroups.com
2010/1/3 Alex Ghitza <agh...@gmail.com>:

> On Sun, 3 Jan 2010 05:35:37 +0000, David Kirkby <david....@onetel.net> wrote:
>>
>> Was there a good reason for choosing the name 'sgn'? It sems a bit
>> strange to me.
>>
>
> That's the standard mathematical notation for this function, see
>
> http://en.wikipedia.org/wiki/Sign_function
>
>
> Best,
> Alex

Thank you. I was not aware of that - but as you probably gather, I did
not study maths,

Jason Grout

unread,
Jan 4, 2010, 4:25:25 AM1/4/10
to sage-...@googlegroups.com
Alex Ghitza wrote:
> On Sun, 3 Jan 2010 05:35:37 +0000, David Kirkby <david....@onetel.net> wrote:
>> Was there a good reason for choosing the name 'sgn'? It sems a bit
>> strange to me.
>>
>
> That's the standard mathematical notation for this function, see
>
> http://en.wikipedia.org/wiki/Sign_function


Then there is an inconsistency with x.sign(), which seems like a problem.

Jason


Gonzalo Tornaria

unread,
Jan 4, 2010, 11:14:00 AM1/4/10
to sage-...@googlegroups.com
On Sun, Jan 3, 2010 at 3:49 AM, Alex Ghitza <agh...@gmail.com> wrote:
> On Sun, 3 Jan 2010 05:35:37 +0000, David Kirkby <david....@onetel.net> wrote:
>>
>> Was there a good reason for choosing the name 'sgn'? It sems a bit
>> strange to me.
>>
>
> That's the standard mathematical notation for this function, see
>
> http://en.wikipedia.org/wiki/Sign_function

"""
Ken Thompson was once asked what he would do differently if he were
redesigning the UNIX system. His reply: "I'd spell creat with an e."
"""

(http://en.wikiquote.org/wiki/Kenneth_Thompson)

Best, Gonzalo

rjf

unread,
Jan 4, 2010, 6:21:29 PM1/4/10
to sage-devel
In common lisp (part of Maxima) one can do this:

(integer-decode-float (exp 1.0d0))

which produces 3 values:

6121026514868073
-51
1


In maxima you would have to prefix this with :lisp.

In Sage, perhaps
maxima ":lisp (integer-decode-float .... "

Though how Sage treats multiple-value returns, from lisp, I don't
know. so maybe you need

(multiple-value-list (integer-decode-float ...))

Reply all
Reply to author
Forward
0 new messages