473,503 Members | 7,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit twiddling floating point numbers

Hi All

Is there a simple way to twiddle the bits of a float? In particular, I
would like to round my float to the n most significant bits.

For example - 0.123 in binary is 0.000111111
Rounding to 4 bits I get 0.0001.

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?
Mar 5 '08 #1
7 1826
On 2008-03-05, Je*************@gmail.com <Je*************@gmail.comwrote:
Is there a simple way to twiddle the bits of a float? In particular, I
would like to round my float to the n most significant bits.

For example - 0.123 in binary is 0.000111111
Rounding to 4 bits I get 0.0001.

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?
Just use the bitwise and/or/not operators: & | ~

--
Grant Edwards grante Yow! Half a mind is a
at terrible thing to waste!
visi.com
Mar 5 '08 #2
On 2008-03-05, Grant Edwards <gr****@visi.comwrote:
On 2008-03-05, Je*************@gmail.com <Je*************@gmail.comwrote:
>Any suggestions?

Just use the bitwise and/or/not operators: & | ~
Oh, I forgot to mention the shift operators << and >>

--
Grant Edwards grante Yow! All of life is a blur
at of Republicans and meat!
visi.com
Mar 5 '08 #3
On Mar 5, 10:48 pm, Grant Edwards <gra...@visi.comwrote:
On 2008-03-05, Grant Edwards <gra...@visi.comwrote:
On 2008-03-05, Jeff.Goldfin...@gmail.com <Jeff.Goldfin...@gmail.comwrote:
Any suggestions?
Just use the bitwise and/or/not operators: & | ~

Oh, I forgot to mention the shift operators << and >>

--
Grant Edwards grante Yow! All of life is a blur
at of Republicans and meat!
visi.com
thanks for the reply but I'm still unsure as to how to continue. Using
the bitwise operators will help me deal with integers but I really
want to work with floats. For instance - which bits do I twiddle to
round my float to the nearest number of bits?

Jeff
Mar 5 '08 #4
On 2008-03-05, Je*************@gmail.com <Je*************@gmail.comwrote:
thanks for the reply but I'm still unsure as to how to
continue. Using the bitwise operators will help me deal with
integers but I really want to work with floats.
In your original post, you said that you've got the values as
integers.
For instance - which bits do I twiddle to round my float to
the nearest number of bits?
The format of a float (actually Python uses doubles) depends on
your platform, but in all likelihood it's the IEEE-754 64-bit
format.

googling for "IEEE-754 format" finds some good references:

http://en.wikipedia.org/wiki/IEEE_fl...point_standard
http://steve.hollasch.net/cgindex/coding/ieeefloat.html
http://www.psc.edu/general/software/...ieee/ieee.html

--
Grant Edwards grante Yow! Well, I'm INVISIBLE
at AGAIN ... I might as well
visi.com pay a visit to the LADIES
ROOM ...
Mar 5 '08 #5
On Mar 5, 3:25*pm, "Jeff.Goldfin...@gmail.com"
<Jeff.Goldfin...@gmail.comwrote:
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.

Mark
Mar 5 '08 #6
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
Mar 6 '08 #7
On Mar 6, 11:00 am, Bryan Olson <fakeaddr...@nowhere.orgwrote:
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

Mar 6 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7827
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
4
3288
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
31
3620
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
687
22803
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
10
2236
by: Shawn | last post by:
Hello all, I apologize as I am sure this has probably been dealth with before... but I am doing an exercise from "Practical C Programming" and I have been unable to get it to work perfectly due to...
7
3375
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
3897
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
32
4047
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
11
2047
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.