473,748 Members | 4,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange bit corruption in a double

I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux

I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.

However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.

I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:

printf = 0.0000000000000 00
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000

Any ideas??????

FWIW, I know the function to print the bit pattern of the double
is correct:

void print_binary_do uble(double value)
{
unsigned char *a;
a = (unsigned char *)&value;

int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc (*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc (unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}

Jan 14 '07 #1
25 1894
"Digital Puer" <di**********@h otmail.comwrote in message
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux

I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.

However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.
I haven't analyzed the bit pattern you provided, but the information you've
presented isn't consistent with "corruption ".

Assume that the number is positive, but very small (let's say 10^(-30)).
Then no version of printf with a practical number of decimal places will
show anything but zero. Additionally, it would test as positive as you
indicated above.

In order to print this number, you'd to use the "%e" rather than the "%f"
format specifier.

Most binary scientific notation (i.e. float, double) formats contain a
binary exponent, which I suspect in this case is a very negative number.
The fact that there are a lot of "1"s set in the number are not inconsistent
with a very small positive number.

Try "%e", and post your results ...
Jan 14 '07 #2

"Digital Puer" <di**********@h otmail.comwrote in message
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux

I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.

However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.

I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:

printf = 0.0000000000000 00
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000

Any ideas??????

FWIW, I know the function to print the bit pattern of the double
is correct:

void print_binary_do uble(double value)
{
unsigned char *a;
a = (unsigned char *)&value;

int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc (*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc (unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
I'll bet your format specifier needs tweeking. The source is sloppy-looking
too. LS
Jan 14 '07 #3
Lane Straatman said:
>
"Digital Puer" <di**********@h otmail.comwrote in message
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
<snip>
>void print_binary_do uble(double value)
{
unsigned char *a;
a = (unsigned char *)&value;

int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc (*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc (unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
I'll bet your format specifier needs tweeking. The source is
sloppy-looking too. LS
How would you improve the source?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 14 '07 #4

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:E6******** *************** *******@bt.com. ..
Lane Straatman said:
>>
"Digital Puer" <di**********@h otmail.comwrote in message
news:11******* *************** @v45g2000cwv.go oglegroups.com. ..

<snip>
>>void print_binary_do uble(double value)
{
unsigned char *a;
a = (unsigned char *)&value;

int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc (*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc (unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
I'll bet your format specifier needs tweeking. The source is
sloppy-looking too. LS

How would you improve the source?
Whitespace. LS
Jan 14 '07 #5
Lane Straatman said:
>
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:E6******** *************** *******@bt.com. ..
>Lane Straatman said:
>>I'll bet your format specifier needs tweeking. The source is
sloppy-looking too.

How would you improve the source?
Whitespace.
man indent if you care enough. Yes, whitespace matters, but it can be added
automatically and trivially to your exact requirements. I have my own
whitespace preferences, which not everybody shares, but "layout not in
accord with my preferences" and "sloppy-looking" are different concepts.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 14 '07 #6
"Digital Puer" <di**********@h otmail.comwrite s:
I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux

I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.

However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.

I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:

printf = 0.0000000000000 00
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000

Any ideas??????
There are lots of numbers that are consistent with this data. Any
number too small to have a non-zero decimal digit in the default
precision used by %f format may still be very much != 0.0 and 0.0.

I think your bit pattern represents a number in the order of
4.3e-305. The %g format will print it as will (on my gcc) %.310f!

--
Ben.
Jan 14 '07 #7
Digital Puer wrote:
I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux

I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.

However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.

I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:

printf = 0.0000000000000 00
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000

Any ideas??????

FWIW, I know the function to print the bit pattern of the double
is correct:

void print_binary_do uble(double value)
{
unsigned char *a;
a = (unsigned char *)&value;

int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc (*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc (unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
You've got something cocked up. I get..

11001000 00010100 00010100 00001001 10001100 00000010 10111110 00000000
Exp = 1153 (131)
000 10000011
Man = .10100 00010100 00001001 10001100 00000010 10111110 00000000
-1.7080703671901 993e+39

...from your 'bits' above.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 14 '07 #8
Ben Bacarisse wrote:
"Digital Puer" <di**********@h otmail.comwrite s:
>I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux

I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.

However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.

I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:

printf = 0.0000000000000 00
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000

Any ideas??????

There are lots of numbers that are consistent with this data. Any
number too small to have a non-zero decimal digit in the default
precision used by %f format may still be very much != 0.0 and 0.0.

I think your bit pattern represents a number in the order of
4.3e-305. The %g format will print it as will (on my gcc) %.310f!
No Ben. There is only one value consistent with the 'bits' data as
presented. It is a 64-bit double on x86 architecture and is unique.

This particular value, expressed by 'printf(".16e", v)' is..

-1.7080703671901 993e+39

...precisely.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 14 '07 #9
On 13 Jan 2007 22:00:05 -0800, "Digital Puer"
<di**********@h otmail.comwrote :
>I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux

I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.

However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.

I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:

printf = 0.0000000000000 00
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000

Any ideas??????
Others have explained why very small non-zero values will print as
zero.
>
FWIW, I know the function to print the bit pattern of the double
is correct:
Only if "correct" means specific to your system and either C99 or
extensions allowed.
>
void print_binary_do uble(double value)
{
unsigned char *a;
a = (unsigned char *)&value;

int bytes = sizeof(double);
C89 does not permit declarations after statements.
for (int i = 0; i < bytes; i++) {
print_binary_uc (*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc (unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
Assumes 8-bit characters. Look up CHAR_BIT in your reference.
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}

Remove del for email
Jan 14 '07 #10

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

Similar topics

10
1936
by: Arno R | last post by:
Hi all Yesterday I found a strange corruption-issue that I can't solve yet or actually point my finger at. I converted an A97 app to A2k. I have done this often enough so I didn't expect trouble here. Conversion seems OK and I start the app. BUT . . . Mainform doesn't work. Form comes up but none of the buttons react. Why not? I go to design view and see that code is not compiled. (compile-option is active) So I compile and go to normal...
1
2659
by: intl04 | last post by:
I am getting strange print-related error messages when trying to create (not print!) reports. For example, when I click 'new' to create a report then choose 'design view', I get an error message that says: 'There was a problem retrieving printer information for this object. The object may have been sent to a printer that was unavailable.' When I choose 'report wizard', I can go through all of the steps but then I get an error message...
8
1700
by: Ondine | last post by:
Hi I have a client running an Access 2000 database on a small network with 3 pc's. Two of the laptop pcs have a data replica, which they use when not connected to the network, the 'server' being the 3rd machine. The other day they reported that 'suddenly' a whole load of data that they had entered (on to the 'server' main data) that day had 'disappeared'. Not only had the new data gone, but updates performed
4
1909
by: Eric E | last post by:
Hi all, I have a fairly complex form in Access 2000. In particular, it has two subforms on separate tabs of a tab control. For the last two weeks, I've encountered the dreaded : "You can't carry out this action at the present time." error, runtime 2486. Subsequently Access will not exit the form, and shuts down uncleanly. Looking back on the messages here, this seems to be an indication of a corrupt object in the .mdb. So I imported...
17
2080
by: Jana | last post by:
Howdy! I have an Access 2003 SP1 where data tables reside on a server & each workstation runs the front end locally. All 5 users are on the same version of Access. We've been having problems on our main data entry form where information input for one record will suddenly attach itself to some other record. Then, once it starts doing this, it seems all users have the same problem. After an exhaustive search of the main table, I found a...
8
3413
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
9
1459
by: sylsau | last post by:
Hi, I am doing a little program who calculates the permutation of a set of vertex. I use the recursivity for this calcul. My function who calculate the permutations : void permutation(set *e, int *current, int nbre) {
11
2491
by: Mike C# | last post by:
Hi all, I keep getting a strange error and can't pin it down. The message is: This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. However I'm not purposely requesting that the Runtime terminate in an "unusual way." The line that is causing me headaches is:
7
2165
by: ifoundgoldbug | last post by:
Ok here is a short description of what i am doing. I need to program a dll for use by another program. the dll compiles fine but when i call it from another program i get a stack corruption of the array in the gamma section of the dll. so below i will post the code for in order: my dll header my dll cpp my testprogram header
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
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 we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.