1

I am trying to solve an equation using a PIC 18F4520 but the result that I am getting is not matching to the accurate answer that I calculated with Matlab.

The part of the equation is y = a * x^8
where a = -6.5991144677544e-09
let x = 60.

When solving via Matlab I get: ans_matlab = -1108397.8
When solving via PIC I get: ans_pic = -1108207.8

I am unable to understand why the answer via PIC is not accurate.
I used long double for data type of a, my compiler is XC8 and C18.

Velvel
  • 3,591
  • 3
  • 12
  • 30
newbie
  • 25
  • 6
  • 2
    Hello and welcome. Can I suggest you edit in your fragment of code and your compiler command? – jonathanjo Jun 19 '23 at 12:11
  • 2
    What do you see if you just try printing the values of a and x^8? The PIC may not define "long double" in the same way that MATLAB does. – Elliot Alderson Jun 19 '23 at 12:20
  • hi @Elliot Alderson, how can i print value in mplab x ide? until now i was using 7 segment displays to see the answer. but if i can print then it will be very helpful . – newbie Jun 19 '23 at 16:51

2 Answers2

5

Unless specified, your XC8 compiler is using 24-bit floats with a 16-bit mantissa. Floats and doubles are the same size, unless overridden, and long doubles are just doubles.

Hence: try -fno-short-double

From Microchip documentation:

Floating-Point Data Types The MPLAB XC8 compiler supports 32- and 24-bit floating-point types, being an IEEE 754 32-bit format, or a truncated, 24-bit form of this, respectively. Floating-point sizes of 32-bits will be automatically set when you select C99 compliance. [...] For both float and double values, the 24-bit format is the default. The options -fshort-float and -fshort-double can also be used to specify this explicitly. The 32-bit format is used for double values if -fno-short-double option is used and for float values if -fno-short-float is used.

How you tell the compiler you want this depends on how you're invoking the compiler; it's different on the command line and with the MPLAB development environment. See Microchip "Invoking the compiler"

jonathanjo
  • 12,049
  • 3
  • 27
  • 60
  • also i am a confused with "number of bits" . like how many bits make up a digit in decimal number ? If we say 4 bits( i. e. according to hexadecimal representation - 9 = 1100) then 24 bits would only give 6 digit number in decimal and that's not the case. – newbie Jun 19 '23 at 16:44
  • can you tell how can we select this option fno-short-double option. – newbie Jun 19 '23 at 16:46
  • 2
    Floating point is considerably more complex: you might care to read [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) at wikipedia. 16 bits is approximately 4.8 decimal digits' precision -- log(2 ** 16). You may well see more decimal digits in output because of binary-decimal conversion. – jonathanjo Jun 19 '23 at 17:20
  • i am using MPLAB X IDE. ok , thanks – newbie Jun 19 '23 at 17:23
  • I believe that in MPLAB X IDE the compiler settings are in [Project Configurations](https://microchipdeveloper.com/mplabx:project-configurations) – jonathanjo Jun 19 '23 at 17:49
3

MATLAB (or your PC) probably uses IEEE 32b FP representation whereas your XC8 compiler uses 24b FP representation. 24b means, as you might guess, less accuracy.

By the way, PIC is off by -0.17 permille (‰) i.e. ans_pic / ans_matblab = 0.9998285812755.

Rohat Kılıç
  • 26,954
  • 3
  • 25
  • 67
  • but i need good accuracy for getting complete solution. This was a part of calculation but the actual ans will be <200 range. and even such small error % to such large numbers easily affect the result badly. any suggestion by your end – newbie Jun 19 '23 at 17:01
  • @newbie just how accurate do you expect your solution to be? Are you going to be dealing with and/or mixing very large and very small numbers? – selectstriker2 Jun 19 '23 at 17:56