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 support@ccsinfo.com

pwr(x,y) error

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



Joined: 07 Feb 2008
Posts: 164

View user's profile Send private message

pwr(x,y) error
PostPosted: Mon Aug 10, 2020 10:52 am     Reply with quote

Anyone notice that pwr(x,y) evaluates inaccurately at higher values of y?
For instance: (int32)pwr(10,7)= 9999997 instead of 10000000.

Using PIC18F25K20, with compiler version 5.093.

Any workaround or solution?

Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Aug 10, 2020 11:06 am     Reply with quote

Remember this is being done using logarithms to allow support for things
like fractional powers. Using fp numbers there are only 6 actual significant
digits in an fp number. Result it's always going to give errors like this.
Solutions. Well for a positive power, using integers, just multiply using
integers. 10*10*10*10*10*10*10 done using int32 arithmetic, will take
less time that pow(10,7), and give an accurate result.
Beyond this, a PIC24/30/33 supports 'double' fp maths, and this should give
a better result.
JerryR



Joined: 07 Feb 2008
Posts: 164

View user's profile Send private message

PostPosted: Mon Aug 10, 2020 11:14 am     Reply with quote

Ttelmah:

Hi Thanks for the quick reply. Yes, I though that's the case. I'll use your "work-around, thanks. The pwr(x,y) method would have made my code a lot slimmer.

All the best
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Aug 10, 2020 11:50 am     Reply with quote

If your power is always of 10, then why not just have a lookup table?.
Code:

const unsigned int32 powers10[] = {0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000};

//Then:
powers10[7];

//is 10^7 etc...
JerryR



Joined: 07 Feb 2008
Posts: 164

View user's profile Send private message

PostPosted: Mon Aug 10, 2020 11:57 am     Reply with quote

Ahhh, clever. Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Aug 10, 2020 12:00 pm     Reply with quote

Hundreds of times faster and smaller.
JerryR



Joined: 07 Feb 2008
Posts: 164

View user's profile Send private message

PostPosted: Mon Aug 10, 2020 12:56 pm     Reply with quote

One little change, however. 10^0= 1.

const unsigned int32 powers10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000};

Thanks again, good solution!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Aug 11, 2020 1:11 am     Reply with quote

I'm sure I typed '1'!....

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