|
|
View previous topic :: View next topic |
Author |
Message |
SimpleGuy
Joined: 12 Dec 2008 Posts: 7
|
POW(10,1) = 9? |
Posted: Tue Dec 16, 2008 8:53 am |
|
|
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
|
|
Posted: Tue Dec 16, 2008 12:06 pm |
|
|
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:
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
|
|
Posted: Tue Dec 16, 2008 1:17 pm |
|
|
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
|
|
Posted: Tue Dec 16, 2008 2:28 pm |
|
|
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
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 16, 2008 2:48 pm |
|
|
The closest I have to your version is 4.014. Here are the results of
running your program with that version:
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
|
|
Posted: Tue Dec 16, 2008 2:55 pm |
|
|
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
|
|
Posted: Tue Dec 16, 2008 5:19 pm |
|
|
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 |
|
|
|
|
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
|