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

Math question
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jeremiah



Joined: 20 Jul 2010
Posts: 1317

View user's profile Send private message

PostPosted: Wed Apr 15, 2020 4:09 pm     Reply with quote

Marttyn wrote:

But don't understand the last example... why you multiply by 32000?

And can you show some more examples? or is there any good resource to find examples like this? Would like to learn to think "that way" Smile
Thanks!


Ttelmah's example wanted to display an ADC value from 0.00 to 5.00v. One way to do that is to use integer values 0 to 500. For a 10bit ADC each of those bits is worth 500/1024 = 0.48828125 per bit. That's not an integer so Ttelmah approximated it by multiplying by a big number and then dividing by a bigger number. The key there is Ttelmah divided by 65536, which is a 16bit shift (thus no actual division...much faster). To figure out what to multiply by take

0.48828125 * 65536 = ~32000.

So then Ttelmah new that if he multiplied by 32000 and then did a 16bit shift, it would be about the same as multiplying by 0.48828125
temtronic



Joined: 01 Jul 2010
Posts: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 16, 2020 4:37 am     Reply with quote

hmm. it might be nice to have a 'scaled integers and other tricks' sticky. I'm sure everyone has clever ways to get PICs to run better.
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Thu Apr 16, 2020 6:02 am     Reply with quote

and (of course), I wouldn't do a 16bit shift. Instead use a union,
containing the int32, and two int16 values. Do the multiply with the
int32 value, and then just use the upper int16 value for the result.

An 'optimisation' sticky might be quite a good idea.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 16, 2020 2:57 pm     Reply with quote

Sorry trying to give you work Ttelmah Smile

Jay idea is great!
Can you write the:

Quote:
An 'optimisation' sticky might be quite a good idea.


All of us will benefit from such a topic Smile
And if someone have some idea in the subject can add his post.

Best wishes
Joe
temtronic



Joined: 01 Jul 2010
Posts: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 16, 2020 5:49 pm     Reply with quote

I figure Mr. T. needs to get some of his wisdom 'downloaded' before his grey ( gray sp?) cells explode.

I remember my Grade 13 high school chem teacher saying, I'd forget 90% of what he taught BUT that I should remember what BOOK the knowledge is in...
THAT has stuck with me but, um, where are the databooks today ?

I did locate my copy of Basic, better and faster. LOTS of neat 'tricks', though 40 years old some still work today.......
hmm, anyone know what a wirewrap gun is for ??
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 17, 2020 1:24 am     Reply with quote

I'm sure there should be some information out there already.
But I could buy a beer for Ttelmah (or other) for such a sticky topic. Very Happy
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Fri Apr 17, 2020 1:38 am     Reply with quote

OK. Lets talk privately, and each come up with some ideas and try to
structure some general 'optimisation ideas', which can then be assembled.

A further thought.
kda406, has assembled a nice little post about 'How to use the CRC module'.
Talking to him privately, the suggestion was raised that this is a 'How too',
rather than complete code, and as such it'd be really nice if there was
a 'how too' section to the forum.
Now the thoughts raised here about using integer maths fall into the
same category.

So (don't know if they will answer given Covid-19), I'm going to suggest
to CCS that they might consider a new 'How to' section to the forum.
We can then use this for things like this CRC guide, and perhaps some
guides to using scaled integer maths etc..
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Tue Apr 21, 2020 3:28 am     Reply with quote

I've put a link to this, and a new post about scaled integer maths into the
new 'Best of' forum. Hopefully of use. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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