Multiply by pi in a 16f pic

Status
Not open for further replies.

bryan1

Well-Known Member
Hiya Guy's,
I'm searching the net looking for examples on multiplying by pi in a pic. The reason I want to use pi is I'm working on a wind speed meter and the easiest way to work out wind speed is simply pi x D where 0.1 or 100mm is my pcd diameter. Google isn't proving to fruitfull so I thought I'd ask here as I'm sure it's be done before in the pic world.

Cheers Bryan
 
Check the EPE website, they have lots of nice maths routines, and have even done the PI thing a number of times - check the source for their LCF Meter.
 
Hello, I have been away for a while, work pressures

Anyway.... another way to achive what you want to do is to use a "look-up" table, lets say you have 100 variations of PI that you need to calculate, you can work these all out on a pc (or calculator etc) and put these answers into a table. The pic will then look at the input from the wind sensor and using this value, jump to the value in the table.

It may not be what you want to do, and you may WANT to calc PI on-the-fly, but i just thought I would bring your attention to a different approach to a problem

I am sure that Nigel has some examples of how to impliment/use lookup's. Oh and if you need more than 256 elements in a table, this is still possible....

I hope this helps!
 
Bryan,

Have a look near the bottom of This Thread.

It shows how to do 8 and 16 bit versions of floating point using PI.

Mike.
 
The most straight forward way I can suggest would be to use a rational approximation. The ones I remember are:
Code:
22 / 7 = 3.142857143...  good to about 400 ppm

355 / 113 = 3.14159292... good to about 85 ppb
I'm sure there are other rational approximations that are even better, I just don't know what they are, but I'm sure you can find them with a bit of effort.

This trick will work for any real number.
 
The EPE article I referred to is at **broken link removed**, or at least the software is - the maths routines are easily extracted.
 
how about just multiplying with 3 ? it would generate an error of 4%.

How acurate is your sensors? If the sensors acuracy is alot lmore then 4% (say 10%) then simply rounding pi of to 3 is a perfectly good solution.
 
Another way is to multiply with 25 and then rightshift by 3(esentialy dividing by 8). This gives you a PI = 3,125, an error of 0,5%. I very much doubt you need windspeed more acurat then that.

25/8 = 3,125
 
in picbasic it would be like this;

Dim D as Word
Dim ValueX as Float

D = 100
' D is recorded in mm

ValueX = 314 * D / 100000
' notice pie is 3.14 * 100, this is to get rid of decimals
' and improve accuracy
' Divide by 1000 for mtrs scale, and 100 again to scale pie down,
' therefor, / 100000

Print At 1,1,Dec ValueX

and the result is 0.314
 
due to the nature of floats, the rounding did not provide exact values. ie, the aswer it was returning was .313 (I can verify this).

floats always round down the last value in any equation should there be decimal places. thier values can be -2147483646.999 to +2147483646.999 so stepping a value up for division is in most cases not an issue, and provides more accurate results, in this case 100%.
 
Last edited:
just on that, notice that im using a word for the value D, so anything that I multiply / divide with it in any equation (irrelivant if the answer is stored in a float) will not produce deciaml places

If i make both D and valueX Floats, and perform the equation;

D = .1
ValueX = 3.14 * D

the anser is
**broken link removed**

As you can see, its close, but no cigar, due to the nature of float algorithm. Keeping this in mind, I want the end answer to be 100% accurate, therefore keeping decimals to a minimum, theres no point having 2 mathamatical equations that could have a deciaml error(which leads to the small error above), why not just make it one. Hence the way i went about it.

As D needn't be a float, (thats if you need it at all, as its a known value - i just put it in there as an example of float point math with words & floats), its declared as a word like so,

Dim D as Word
Dim ValueX as Float

D = 100
' D is recorded in mm

ValueX = d * 314 / 100000


and the asnwer;

**broken link removed**
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…