Just beware that you need a data type which is large enough to store x^2 to do this. You might be better doing c*x first and then multiplying by x again. That minimizes the range of values which your variables need to store. Sean On Thu, Oct 30, 2008 at 6:48 AM, Matthew Rhys-Roberts wrote: > Hi Gerhard > > I figured out that > x^2/309023 is approx. equal to > (x^2 / 2^16) / 4.72, which is approx. equal to > [(x^2 / 2^16) * 13885] / 65536 !! > > The graph gives a nice enough fit in OpenOffice Calc, so it should be OK > when the PIC spits it out. :) > > Thanks for your encouragement. > > Best regards, > Matt > > > Gerhard Fiedler wrote: >>>> I'm calculating a quadratic expression, y=a+bx+cx^2, using PIC18 >>>> assembler, as simply as possible in binary. >>>> >>>> Calculating bx is easy; >>>> b=-0.0407; >>>> 0.0407x == 2667x/65536. (just make a 32bit product, then chop off the >>>> bottom 16 bits). >>>> >>>> However: >>>> c = 3.236e-6, or 1/309023. >>>> My range of x^2 is between 171,793,449 and 3,310,820,528. >>>> 171,793,449/309023 is about 556; 3,310,820,528/309023 is about 10714. >>>> >>>> Now my brain hurts. How can I calculate cx^2 as easily as I did bx? >>>> >> Try using the same approach... one thing that's missing here is how precise >> you need it. Based on this, you look for a multiplier/dividend pair that >> approximates 1/309023. Depending on your precision needs, you can chop off >> low-order bits from the squared x before multiplying and dividing, and thus >> be able to fit it into 32-bit math. >> >> Gerhard >> >> > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist