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

POW(10,1) = 9?

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



Joined: 12 Dec 2008
Posts: 7

View user's profile Send private message

POW(10,1) = 9?
PostPosted: Tue Dec 16, 2008 8:53 am     Reply with quote

When I Run POW(10,1) I get 9.

WHY? and can I do anything about it?

IDE ver 4.077
PCM ver 4.013
PCD ver 4.077
DsPIC30F4012

Is this just a known problem that I don't know?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 16, 2008 12:06 pm     Reply with quote

You didn't post a test program so I made the one shown below.
When I compile it with vs. 4.083 and run it, I get this:
Quote:
10.000000

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

#include <math.h>

//=========================================
void main (void)
{
float result;

result = pow(10, 1);

printf("%7.6f \n\r", result);

while(1);
}
Ttelmah
Guest







PostPosted: Tue Dec 16, 2008 1:17 pm     Reply with quote

One possibility, is if you put the result into an integer. Then 9.9999999, will give a result of '9'. Default is a _crop_ conversion, not a rounding operation.

Best Wishes
SimpleGuy



Joined: 12 Dec 2008
Posts: 7

View user's profile Send private message

PostPosted: Tue Dec 16, 2008 2:28 pm     Reply with quote

My Code
Code:
#include <prototype.h>
#include <math.h>


void main()

{
unsigned int8 A;
int8 B;
float c;
while(true)
{
A=pow(10,1);
B=pow(10,1);
C=pow(10,1);


printf("\r\n%u",a);
printf("\r\n%u",b);
printf("\r\n%7.6f",c);
delay_ms(1000);
}
}


output
Quote:
9
9
9.999956
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 16, 2008 2:48 pm     Reply with quote

The closest I have to your version is 4.014. Here are the results of
running your program with that version:
Quote:
10
10
10.000000

I got the same as above with vs. 3.249 as well.

So, I think it's a problem with vs. 4.013. That is a very early version
after a major change to the compiler. It is known to be buggy.


Also, I don't think you ever posted what PIC you're using with the
PCM compiler. All my tests were done with a 16F877, running in MPLAB
simulator.
Ttelmah
Guest







PostPosted: Tue Dec 16, 2008 2:55 pm     Reply with quote

Exactly what I said/suspected....
You are putting a floating point value, directly into an integer. It therefore gets _trimmed_, losing all the decimals.

Seriously, if you want integer results, you would get better results, by just using your own integer power functon. The pow function is designed for float operation. Something like:
Code:

ipow(unsigned int8 b, unsigned int8 exp) {
   unsigned int16  res = 1;
   unsigned int8   ctr;
   for(ctr=0;ctr<exponent;ctr++)   res *= b;
   return res;
}

This will handle your simple example, mch faster, than using pow.
Alternatively, use a rounding function to put the value into the int. So:

A=(pow(10,1)+0.4999999);

Which will generally give the closest integer, rather than the one below which your current code gives.

Best Wishes
Guest








PostPosted: Tue Dec 16, 2008 5:19 pm     Reply with quote

Ttelmah thanks I think I will go that way in the end.

PCM programmer
I am using DsPIC30F4012 and I am running it on the chip.

PCWHD Compiler auto updates I think but where can I get the newest update
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