Bit twiddling floating point numbers
Blubaugh, David A.
dblubaugh at belcan.com
Tue May 6 17:53:35 EDT 2008
Sorry for the reply. I did not get your message until now. I was
wondering if there was a way to develop floating-point mathematics
package within a module. I was wondering if some of your work on bit
twiddling floating - point numbers could be provided to me!!! Thanks.
David Blubaugh
________________________________
From: Jeff Goldfinkle [mailto:jeff.goldfinkle at gmail.com]
Sent: Monday, April 28, 2008 2:10 PM
To: Blubaugh, David A.
Subject: Re: Bit twiddling floating point numbers
sure - go right ahead
On Mon, Apr 28, 2008 at 8:17 PM, Blubaugh, David A.
<dblubaugh at belcan.com> wrote:
Are you still out there?
Can I still ask you about my question?
thanks,
David
________________________________
From: Jeff Goldfinkle [mailto:jeff.goldfinkle at gmail.com]
Sent: Friday, March 07, 2008 1:04 AM
To: Blubaugh, David A.
Subject: Re: Bit twiddling floating point numbers
Hi David
Sure. I'm not sure what help I can be, but ask away.
Jeff
On Thu, Mar 6, 2008 at 11:25 PM, Blubaugh, David A.
<dblubaugh at belcan.com> wrote:
Jeff,
Hello. This idea or representing a float with long
seems to be
extremely interesting. I am currently utilizing the
MyHDL environment
to map algorithms to hardware. May I ask you more
questions in order to
map integer mathematics to emulate floating -point
mathematics, in order
to maybe develop efficient pseudo-floating-point
hardware??
Thanks,
David Blubaugh
-----Original Message-----
From: Jeff.Goldfinkle at gmail.com
[mailto:Jeff.Goldfinkle at gmail.com]
Sent: Thursday, March 06, 2008 4:01 PM
To: python-list at python.org
Subject: Re: Bit twiddling floating point numbers
On Mar 6, 11:00 am, Bryan Olson
<fakeaddr... at nowhere.org> wrote:
> Mark Dickinson wrote:
> > Jeff Goldfin wrote:
> >> I can pack and unpack a float into a long e.g.
> >> struct.unpack('I',struct.pack('f',0.123))[0]
> >> but then I'm not sure how to work with the
resulting long.
>
> >> Any suggestions?
>
> > One alternative to using struct is to use math.ldexp
and math.frexp:
>
> >>>> m, e = frexp(pi)
> >>>> m
> > 0.78539816339744828
> >>>> e
> > 2
> >>>> int(m*2**53)
> > 7074237752028440L
>
> > Then you can do your bit twiddling on int(m*2**53),
before using
> > ldexp to 'repack' the float.
>
> Ah, those are handy. Jeff described his problem: "In
particular, I
> would like to round my float to the n most significant
bits."
> I think this works:
>
> from math import frexp, ldexp, floor
>
> def round_mantissa(x, nbits):
> shifter = 1 << nbits
> (m, e) = frexp(x)
> m = floor(m * shifter + 0.5) / shifter
> return ldexp(m, e)
>
> --
> --Bryan
Thanks for the help - your function seems to fit the
bill even better
than gmpy since I don't need an external module. In my
case I'll use m =
floor(m * shifter) / shifter instead of m = floor(m *
shifter +
0.5) / shifter
Jeff
This e-mail transmission contains information that is
confidential and may be
privileged. It is intended only for the addressee(s)
named above. If you receive
this e-mail in error, please do not read, copy or
disseminate it in any manner.
If you are not the intended recipient, any disclosure,
copying, distribution or
use of the contents of this information is prohibited.
Please reply to the
message immediately by informing the sender that the
message was misdirected.
After replying, please erase it from your computer
system. Your assistance in
correcting this error is appreciated.
This e-mail transmission contains information that is
confidential and may be privileged. It is intended only for the
addressee(s) named above. If you receive this e-mail in error, please do
not read, copy or disseminate it in any manner. If you are not the
intended recipient, any disclosure, copying, distribution or use of the
contents of this information is prohibited. Please reply to the message
immediately by informing the sender that the message was misdirected.
After replying, please erase it from your computer system. Your
assistance in correcting this error is appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080506/706c3c79/attachment.html>
More information about the Python-list
mailing list