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? 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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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....
|
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...
|
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...
| |
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...
|
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...
|
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 );...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |