CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Negative floating zero.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Negative floating zero.
PostPosted: Mon Feb 11, 2008 7:43 pm     Reply with quote

I came across the following problem in my application. When I subtract two floating point numbers that happen to be zero and test the result, the zero has a negative sign.

Code:
float   x,y;

x=y=0.0;
x-=y;
if(x<0.0)
    printf("Zero has negative sign.\n");
else if(x>0.0)
    printf("Zero has positive sign.\n");
else
    printf("Zero has no sign.\n");

I can work around the problem by adding the following line before the if statement:

Code:
x+=0.0;

Am I missing something?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 11, 2008 11:45 pm     Reply with quote

It's definitely setting the sign bit. I modified your program so it would
display the bytes that make up the floating point number.
This page shows the floating point format:
http://www.ccsinfo.com/faq.php?page=mchp_float_format
Here's the output of the program:
Code:

00 00 00 00

00 80 00 00  <-- sign bit is set.


Code:

#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOPUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//====================================
void main()
{
float x, y;
int8 i;

x = y = 0.0;

for(i = 0; i < 4; i++)
    printf("%X ", *((int8 *)&x + i));
printf("\n\r");

x -= y;

for(i = 0; i < 4; i++)
    printf("%X ", *((int8 *)&x + i));
printf("\n\r");

while(1);
}
Ttelmah
Guest







PostPosted: Tue Feb 12, 2008 3:15 am     Reply with quote

Yes, I ran into this a long time ago.
The (dirty) solution, is to add:

if (x==0.0) x=0.0;

The 'test', only checks for the value being zero (doesn't check the sign bit), so if a value is zero, sets it 'back' to the positive zero.
It leads to some real oddities latter in arithmetic, if the sign is left set...

Best Wishes
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Tue Feb 12, 2008 5:19 am     Reply with quote

Strictly mathematically zero is the only numerical quantity that is simultaneously both positive and negative. By convention the negative aspect of zero is usually ignored in everyday life.
Now when it comes to the quantum mechanical nature of electrons the negative aspect of zero is an important property. Without this property electrons would be very different and there wouldn't be Microchip devices.
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Tue Feb 12, 2008 7:19 am     Reply with quote

Has anyone reported this to ccs?

(0.0 - 0.0), where ALL bits are zero, should definitely give a 0.0 result, with all bits zero.

Ken
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Feb 12, 2008 9:42 am     Reply with quote

Hey, wait a minute. Are you trying to tell me that zero (0) is not zero (-0)? Shocked
Guest








PostPosted: Tue Feb 12, 2008 10:11 am     Reply with quote

rnielsen wrote:
Hey, wait a minute. Are you trying to tell me that zero (0) is not zero (-0)? Shocked


That is not the problem. If this -0 value is tested as above, the conclusion is that the value is negative and not zero. The program then takes the wrong branch with whatever consequences.

I see that the following produces the same result:

Code:
float x;

x = 0.0;
x = -x;
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group