G
Guest
Hello,
We are writing an application in C# that finds roots of a function up to
machine precision. Therefore I have to compare the number of significant bits
in two double variables (the upper- and lowerbound of the interval containing
the root). Currently I am struggling with the binary representation of a
double variable.
I have written some test code and it seems that the binary representation of
a double is given by:
MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM EEESMMMM EEEEEEEE
Where M denotes the mantissa, E the exponent and S the sign.
The location of the least significant bit (*) is seems to be given by:
MMMMMMM* MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM EEESMMMM EEEEEEEE
And the most significant bit:
MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM EEES*MMM EEEEEEEE
This would mean that the mantissa part is written as:
qrstuvwxy ijklmnop abcdefgh
Where y is the least significant bit and a the most significant bit. This is
kind of unfortunate, because we cannot simply use a bitwise shift operator to
determine if all but the least significant bit are equal. Could you confirm
if we have this bitwise structure correct?
Regards,
Martijn Kaag
We use test code similar to the following to obtain the bitwise
representation:
______________________________
www.VECOZO.nl
We are writing an application in C# that finds roots of a function up to
machine precision. Therefore I have to compare the number of significant bits
in two double variables (the upper- and lowerbound of the interval containing
the root). Currently I am struggling with the binary representation of a
double variable.
I have written some test code and it seems that the binary representation of
a double is given by:
MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM EEESMMMM EEEEEEEE
Where M denotes the mantissa, E the exponent and S the sign.
The location of the least significant bit (*) is seems to be given by:
MMMMMMM* MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM EEESMMMM EEEEEEEE
And the most significant bit:
MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM EEES*MMM EEEEEEEE
This would mean that the mantissa part is written as:
qrstuvwxy ijklmnop abcdefgh
Where y is the least significant bit and a the most significant bit. This is
kind of unfortunate, because we cannot simply use a bitwise shift operator to
determine if all but the least significant bit are equal. Could you confirm
if we have this bitwise structure correct?
Regards,
Martijn Kaag
We use test code similar to the following to obtain the bitwise
representation:
public string GetBitString (double d) {
byte[] bytes = ToByte(d);
string rval = “â€;
for (int b=0; b<8;b++) {
rval += GetBitString (bytes) + “ â€;
}
return rval;
}
public unsafe Byte[] ToByte ( double d)
{
*Byte[] byte;
&byte = (*Byte[])&d;
return byt;
}
public string GetBitString (byte b)
{
string rval = "";
for (int i=0; i <8;i++)
{
if (b>=126){
// alleen >=126 als meest significante bit in b = 1
rval += "1";
}
else {
rval += "0";
}
______________________________
www.VECOZO.nl