News:

Let's find out together what makes a PIC Tick!

Main Menu

Need a bit of maths guidance.

Started by david, Apr 05, 2024, 09:55 AM

Previous topic - Next topic

david

Hello All,
I have a maths function which is a 2nd order polynomial with input being a signed integer and the output required is only signed integer.
The function is nearly a straight line but it does have a slight curve which is important.
I tried using SWord and SDWord and thought it was all working nicely until I saw a glitch in the output sequence which is shown in the attached spreadsheet picture.
It seems to me I need to calculate at a floating point level then convert the answer back to a signed integer but it does get quite bulky.  Is there a better option to do this?

Cheers,
David

ricardourio

Hi David,

      Using Google Sheets I could see some rounding issues with your results. Maybe a Proton issue ?

Ricardo Urio



trastikata

#2
Quote from: david on Apr 05, 2024, 09:55 AMIs there a better option to do this?

Hello David,

you have two divisions where you loose precision, especially in the intermediate, hence the glitch, which will be periodic - 2nd, 11th, 33th therm etc. If you rework your math problem to have only one division at the end, the glitch will disappear.

Y = X*(67681 - 2*X)/135362

Do the division as last operation.

Regards.

david

Hi trastikata,
Many thanks for your reply.   I shall load that in to my spreadsheet and check out the numbers but I think you're right.
I would never have thought of breaking down the function that way.
I did try the float calculation which worked but it certainly bloated the code.
Stand by....


Cheers,
David

david

Thank you trastikata - that looks excellent both in the spreadsheet and on the OLED display.
I thought my maths was ok but that's a very simple and innovative approach which saves about 200 bytes of code.

Cheers,
David

top204

#5
Nice one trastikata

With integer mathematics, divisions will always cause rounding problems with some values that will not divide fully. i.e. 100 / 30 for example will give a result of 3 with integers, even though it is actually 3.3333333333333333333333333333 (and on and on and on). This error gets expanded with more divisions, so it is always better to use as few divisions as required for integer mathematics.

Or if less divisions are not possible, expand the integer values with multiplications, then do the divisions, and divide the result by the value used for the multiplications. It will still produce rounding problems but they will usually be smaller, and maybe not noticable.

On occasions, I have actually used integer multiplications then divisions to round off values for simple filtering and to help stop digit jitters on displays.