Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

XC8 and pic18f4520 not handling math problem correctly.

Status
Not open for further replies.

muxer107

Newbie level 2
Joined
Apr 20, 2016
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
18
Hi all,

I have a pic18f4520 programmed with mplab X and the XC8 compiler. All variables are floats and i'm using it to solve the following polynomial:

Code:
poly1 = (-3.951941733344756E+04);
poly2 = (4.252156952428445E-01*X);
poly3 = (6.840575570401091E-08*X *X);
poly4 = (6.564136661977851E-15*X *X *X);

Y = (poly1+poly2+poly3+poly4);

X is typically around 200000.

Now, for a given change in X, Y should change appropriately, however Y is moving in jumps.

For example, instead of moving from 8488 to 8489 as X increases, it waits for X to increase enough, then jumps to 8502.

I know this is some sort of floating point issue (using such small numbers in the polynomial). However I can't put my finger on it, or even see how I can work around it.

Can anyone help?

Any input appreciated.
 

XC8 uses reduced 24-bit float format by default which provides insufficient accuracy for your problem. Specify a 32-bit format either for all float variables or specifically for double.
 

Yeah thought of that, didn't help at all.

Changed to 32bit accuracy in the XC8 options, but the output remains the same.

Do I need to change it somewhere else as well?
 

Check if the respective variables are actually created with 32 bit width (e.g. in the memory map or assembly listing).

I was under the assumption that the poor accuracy is due to 24 bit float. But even 32 bit standard IEEE float might be insufficient. One would preferably use 64 bit double for polynomials with large number range. If the x range is limited to a certain positive range (e.g. around 200e3) you can greatly increase the numeric accuracy by rewriting the equations, evolving the polynomial fit around a different origin.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top