View previous topic :: View next topic |
Author |
Message |
Guest
|
Negative floating zero. |
Posted: Mon Feb 11, 2008 7:43 pm |
|
|
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:
Am I missing something? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 11, 2008 11:45 pm |
|
|
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
|
|
Posted: Tue Feb 12, 2008 3:15 am |
|
|
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
|
|
Posted: Tue Feb 12, 2008 5:19 am |
|
|
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
|
|
Posted: Tue Feb 12, 2008 7:19 am |
|
|
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
|
|
Posted: Tue Feb 12, 2008 9:42 am |
|
|
Hey, wait a minute. Are you trying to tell me that zero (0) is not zero (-0)? |
|
|
Guest
|
|
Posted: Tue Feb 12, 2008 10:11 am |
|
|
rnielsen wrote: | Hey, wait a minute. Are you trying to tell me that zero (0) is not zero (-0)? |
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;
|
|
|
|
|