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 CCS Technical Support

Help: a/abs(a)=0 when a positive??

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



Joined: 13 May 2010
Posts: 42
Location: Ottawa, Canada

View user's profile Send private message

Help: a/abs(a)=0 when a positive??
PostPosted: Sat Jul 17, 2010 6:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jul 17, 2010 6:35 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jul 17, 2010 6:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jul 17, 2010 8:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jul 17, 2010 8:34 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jul 17, 2010 9:46 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jul 17, 2010 9:55 pm     Reply with quote

That version is basically pre-beta and is well known to be buggy.
louarnold



Joined: 13 May 2010
Posts: 42
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 9:35 am     Reply with quote

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.
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