• AVR Freaks

[SOLVED] XC16 and Dspic33E : Float accuracy

Author
PEB
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2009/05/03 03:28:04
  • Location: 0
  • Status: offline
2013/09/25 13:25:40 (permalink)
0

[SOLVED] XC16 and Dspic33E : Float accuracy

Hi,
First, I use : 
- OSX
- XC16 v1.10
- MLPABX 1.51
- DSPIC 33EP256MU810 
 
I need to do precise floating point calculation, but I have bad and strange results that I don't understand...
 
My test :
   
double valDouble1 = 0.0123456;
double valDouble2 = 12345.1;
double valDouble3 = 1.0;
 
valDouble1 += valDouble2;
valDouble1 += valDouble3;
printfd ("1- Double : [%f+%f+%f=%f]\n", 0.0123456, valDouble2, valDouble3, valDouble1);

 
Result on OSX (test ok):
1- Double : [0.012346+12345.100000+1.000000=12346.112346]
 
Result on DSPIC (test nok) :
1- Double : [0.012346+12345.099609+1.000000=12346.112305]
 
I havn't found document about the float accuracy of this dspic family...
Have you an idea why I have a big rounded mistake like that? (12345.099609 instead of 12345.100000 and 12346.112305 instead of 12346.112346)
 
 
post edited by PEB - 2013/09/29 14:38:24
#1

5 Replies Related Threads

    davekw7x
    Entropy++
    • Total Posts : 1819
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re:XC16 and Dspic33E : Float accuracy 2013/09/25 14:46:17 (permalink)
    +1 (1)
    PEB
    ...I havn't found document about the float accuracy of this dspic family...
    You can check the XC16 User's Guide.  It spells it all out.
     
    Bottom line:
    For "normal" compiles float and double data types are both 32-bit floats.  On the other hand, long double data types are 64-bit floats.  (There is a compiler command-line switch that makes doubles 64-bits.)
     
    PEB
    Have you an idea why I have a big rounded mistake like that?

    Go back to your workstation compiler and declare all of the variables to be floats rather than doubles and see what it gives.  With GNU gcc, using floats rather than doubles, I get exactly the same output as you showed from the dsPIC. This compiler has 32-bit floats and 64-bit doubles.
     
    Using floats instead of doubles, the output indicates the expected limits of 32-bit floating point precision: about six or seven decimal digits.  Using 64-bit floats I expect about 16 significant decimal digit precision.
     
    Anyhow...
     
    With XC16 version 1.11 I tried the following on a dsPIC33EP256GP506, and no special compiler switches:
     
         

        long double valDouble1 = 0.0123456L;
        long double valDouble2 = 12345.1L;
        long double valDouble3 = 1.0;

        valDouble1 += valDouble2;
        valDouble1 += valDouble3;
        printf("sizeof(float) = %d, sizeof(double) = %d, sizeof(long double) = %d\n",
                sizeof(float), sizeof(double), sizeof(long double));
        printf("1- Long Double : [%lf+%lf+%lf=%lf]\n",
                0.0123456L, valDouble2, valDouble3, valDouble1);

     
    Output
         
    sizeof(float) = 4, sizeof(double) = 4, sizeof(long double) = 8
    1- Long Double : [0.012346+12345.100000+1.000000=12346.112346]

     
    I have previously noted that the format specification for long doubles should be "%Ld" but XC16 doesn't like that.  Printing long doubles with "%f" or "%lf" gives a warning, but results seem to show that the calculations are, at least, carried out with 64-bit floats.
     
     
    Regards,

    Dave
     
    post edited by davekw7x - 2013/09/25 14:56:15

    Sometimes I just can't help myself...
    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 17723
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re:XC16 and Dspic33E : Float accuracy 2013/09/25 15:14:28 (permalink)
    0 (2)
    There is a compiler option to make doubles 64 bits
    #3
    PEB
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2009/05/03 03:28:04
    • Location: 0
    • Status: offline
    Re:XC16 and Dspic33E : Float accuracy 2013/09/26 02:45:49 (permalink)
    0
    thank you for your replies.
     
    davekw7x 
    Go back to your workstation compiler and declare all of the variables to be floats rather than doubles and see what it gives.  With GNU gcc, using floats rather than doubles, I get exactly the same output as you showed from the dsPIC. This compiler has 32-bit floats and 64-bit doubles. 

    Using floats instead of doubles, the output indicates the expected limits of 32-bit floating point precision: about six or seven decimal digits.  Using 64-bit floats I expect about 16 significant decimal digit precision.
     
     
    Ok, but why 0.1 is rounded to 0.099609 ? 32bits (so float or double declaration) should be enought for this case?
     
     
    davekw7x 
    With XC16 version 1.11 I tried the following on a dsPIC33EP256GP506, and no special compiler switches: 

      

       long double valDouble1 = 0.0123456L; 
       long double valDouble2 = 12345.1L; 
       long double valDouble3 = 1.0; 

       valDouble1 += valDouble2; 
       valDouble1 += valDouble3; 
       printf("sizeof(float) = %d, sizeof(double) = %d, sizeof(long double) = %d\n", 
               sizeof(float), sizeof(double), sizeof(long double)); 
       printf("1- Long Double : [%lf+%lf+%lf=%lf]\n", 
               0.0123456L, valDouble2, valDouble3, valDouble1); 
     

    Output 
      
    sizeof(float) = 4, sizeof(double) = 4, sizeof(long double) = 8 
    1- Long Double : [0.012346+12345.100000+1.000000=12346.112346]
     

     
     
    I tried with long double declaration and double with the '64bits double' gcc option, it works fine on my side, like you said.
     
    #4
    dhenry
    Super Member
    • Total Posts : 4994
    • Reward points : 0
    • Joined: 2003/11/07 12:35:12
    • Location: Colorado
    • Status: offline
    Re:XC16 and Dspic33E : Float accuracy 2013/09/26 06:03:10 (permalink)
    +3 (3)
    PEB
    Ok, but why 0.1 is rounded to 0.099609 ? 32bits (so float or double declaration) should be enought for this case?

    http://docs.oracle.com/cd...3568/ncg_goldberg.html
    #5
    PEB
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2009/05/03 03:28:04
    • Location: 0
    • Status: offline
    Re:XC16 and Dspic33E : Float accuracy 2013/09/29 14:38:30 (permalink)
    0
    thank you for all.
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5