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

#define evaluation

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



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

#define evaluation
PostPosted: Thu Jul 18, 2013 4:33 am     Reply with quote

Why does the following expression evaluate to 99 instead of 100
Tested with V1.141 and V5.009
Code:

  #define Tcy (unsigned int64)(2e9)/((unsigned int64)(20000000))


Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Jul 18, 2013 6:31 am     Reply with quote

Because rounding from a float to an integer, clips downwards. So it becomes 1999999999/20000000, which in integer arithmetic is 99.

'2E9', is always a float value.

You don't need the casts.

Just use 2000000000/20000000.

Best Wishes
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Thu Jul 18, 2013 9:11 am     Reply with quote

Thanks

Just needed to add an L.
2000000000L/20000000L

Regards
Alan
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Sat Jul 27, 2013 2:59 am     Reply with quote

Still battling with #define

I have the following

Code:

#define CAPACITY             40
#define IFLOAT    CAPACITY * 1.57

 if(IOut < IFLOAT )

itoa(IFLOAT,10,ID0str);


This produce the following asm

Code:

                         ....................       itoa(IFLOAT,10,ID0str);
MOV     #A,W4          : W4 = A
MOV     W4,10AA        : [10AA] = W4
MOV     #3E,W4         : W4 = 3E    <----  This is the correct value, thus evaluate OK
MOV     W4,10A6        : [10A6] = W4
CLR     10A8           : [10A8] = 0
MOV     #105C,W4       : W4 = 105C
MOV     W4,10AC        : [10AC] = W4
CALL    1384           :

and IOut is declared as uint16_t
Code:

 ....................      if(IOut < IFLOAT ) {
MOV     1008,W0        : W0 = [1008]
MOV     #0,W1          : W1 = 0
MOV     #0,W2          : W2 = 0
MOV     #0,W3          : W3 = 0
CALL    1744           :
MOV     #6667,W4       : W4 = 6667
MOV     #6666,W5       : W5 = 6666
MOV     #6666,W6       : W6 = 6666
MOV     #404F,W7       : W7 = 404F
CALL    17A8           :
BRA     NC,18BA        : Branch if not carry (or Borrow)

If I replace IFLOAT with 63 it works fine.

How can I get the compiler to do the comparison right.

Tested with V5.010 and V4.141. Listings above are for the V1.141

Regards
Alan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 27, 2013 10:29 am     Reply with quote

Post a test program that we can run in MPLAB simulator.
It must be compilable and very short. Something like this:
Code:
#include <16F887.h>
#fuses INTRC_IO, NOWDT
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)

//==========================================
void main()
{
float IOut = 10.0;

#define CAPACITY             40
#define IFLOAT    CAPACITY * 1.57

if(IOut < IFLOAT )
   printf("IOut < IFLOAT\r");
else
   printf("IOut is not < IFLOAT\r");

while(1);
}


Aside from that, the standard, safe way to define math expressions
other than individual constants is to enclose them in parentheses like this:
Code:
#define IFLOAT   (CAPACITY * 1.57)
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Sat Jul 27, 2013 11:16 am     Reply with quote

Thanks PCM

Didn't know MPLAB had an UART monitor. Yes the code above does give the expected result, was a fault somewhere else. I stopped at this funny comparison and thought that may be the problem.

The parentheses also did the job together with the following cast of the constant. The asm are now what I expected in the 1st place.

Code:
 if(IOut < ((uint16_t) IFLOAT)

Thanks again for taking the time.

Regards
Alan
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