View previous topic :: View next topic |
Author |
Message |
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
Help: a/abs(a)=0 when a positive?? |
Posted: Sat Jul 17, 2010 6:10 pm |
|
|
Here is the code and the output wth the CCS V3.170b compiler.
Code: |
#include <16f877A.h>
#fuses HS, NOLVP, NOWDT, PUT, NODEBUG
#use delay (clock = 20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main () //A test version of main
{
signed long Xvalue;
for (Xvalue=-1; Xvalue<5; Xvalue++)
{
printf("\n\ra=%ld,Abs(a)=%ld, a/(ABS(a))=%ld",Xvalue,ABS(Xvalue)
,Xvalue/ABS(Xvalue));
}
while(1);
}
--------results:
a=-1,Abs(a)=1, a/(ABS(a))=-1
a=0,Abs(a)=0, a/(ABS(a))=0
a=1,Abs(a)=1, a/(ABS(a))=0
a=2,Abs(a)=2, a/(ABS(a))=0
a=3,Abs(a)=3, a/(ABS(a))=0
a=4,Abs(a)=4, a/(ABS(a))=0
|
I assume this is a compiler bug?
Regards
Lou. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 17, 2010 6:35 pm |
|
|
I ran your program in vs. 4.106 (in MPLAB simulator) and got this:
Quote: |
a=-1,Abs(a)=1, a/(ABS(a))=1
a=0,Abs(a)=0, a/(ABS(a))=0
a=1,Abs(a)=1, a/(ABS(a))=1
a=2,Abs(a)=2, a/(ABS(a))=1
a=3,Abs(a)=3, a/(ABS(a))=1
a=4,Abs(a)=4, a/(ABS(a))=1
|
So I think it's a bug in your version.
Try using a substitute. Put this macro above main(), re-compile and
see what happens:
Code: | #define ABS(x) (((x) < 0) ? -(x) : (x)) |
|
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Sat Jul 17, 2010 6:40 pm |
|
|
Yes, that works for me also.
My friend has a V4.0 compiler. It has the same bug.
How do I find out if the bug has been reported? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 17, 2010 8:01 pm |
|
|
Quote: | My friend has a V4.0 compiler
|
That isn't a very definitive version number.
Wait for one of us to test it with vs. 4.109 later in the weekend.
We'll tell you if it works or not. |
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Sat Jul 17, 2010 8:34 pm |
|
|
well, a/abs(a) should fail when a=0, so its not a useful function as it stands.
A better one is:
Code: |
#define sgn(x) ((x==0) ? 0 : ((x < 0) ? -1 : 1))
|
But that one always produces a signed 8 bit number and I wanted a signed 16 bit number. All the casting I tried didn't work.
Funny that stdlib.h has the same problem with abs().
Lou. |
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Sat Jul 17, 2010 9:46 pm |
|
|
PCM programmer wrote: | Quote: | My friend has a V4.0 compiler
|
That isn't a very definitive version number.
Wait for one of us to test it with vs. 4.109 later in the weekend.
We'll tell you if it works or not. |
Its V4.013. The bug is in that compiler as well. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 17, 2010 9:55 pm |
|
|
That version is basically pre-beta and is well known to be buggy. |
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Sun Jul 18, 2010 9:35 am |
|
|
PCM programmer wrote: | That version is basically pre-beta and is well known to be buggy. |
Haha. I think he already knows.
Thanks very much for your help.
Lou. |
|
|
|