Hot!Floating point opearation problem.

Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
Author
Akshayy
Super Member
  • Total Posts : 351
  • Reward points : 0
  • Joined: 2016/07/29 06:35:35
  • Location: 0
  • Status: offline
2018/04/24 05:26:56 (permalink)
0

Floating point opearation problem.

Hi,
 
       I am using PIC32MX250F256H controller in my application.I am facing some issues with floating point operations.
 
float f=0
int main()
{
f=(float)(47.1711439-47.0);
}
 
I should get f=0.1711439 instead I m getting f=0.1711425
I want accuracy upto 6th place after decimal.How do i do it?

Regards,
Akshay.
#1

27 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 2632
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: Floating point opearation problem. 2018/04/24 05:42:23 (permalink)
    5 (1)
    You need to refresh your memory about how floating point numbers work.
    Try using "double" instead of float.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #2
    rodims
    Super Member
    • Total Posts : 1292
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: Floating point opearation problem. 2018/04/24 05:42:50 (permalink)
    5 (1)
    The XC32 compiler guide contains a rather good explanation of the differences for float, double, long double.
    see  FLOATING-POINT DATA TYPES chapter.
     
    [edit: and take care of -fno-short-double compiler option
    http://www.microchip.com/forums/m683940.aspx
    ]
    post edited by rodims - 2018/04/24 05:46:58
    #3
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/24 06:32:27 (permalink)
    0
    qɥb
    You need to refresh your memory about how floating point numbers work.
    Try using "double" instead of float.


    I used Double even long double also

    Regards,
    Akshay.
    #4
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/24 06:33:29 (permalink)
    0
    qɥb
    You need to refresh your memory about how floating point numbers work.
    Try using "double" instead of float.


    How do i refresh memory?

    Regards,
    Akshay.
    #5
    DavidBLit
    Super Member
    • Total Posts : 1548
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: Floating point opearation problem. 2018/04/24 06:40:37 (permalink)
    5 (2)
    Start here.

    Yeah, "//Code and stuff".
    #6
    1and0
    Access is Denied
    • Total Posts : 8001
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Floating point opearation problem. 2018/04/24 07:33:17 (permalink)
    5 (1)
    AKSHAY D BHAVSAR
    qɥb
    You need to refresh your memory about how floating point numbers work.
    Try using "double" instead of float.

    I used Double even long double also

    Hint: the closest single-precision representation for 47.1711439 is 47.171142578125, and 47.0 has exact floating-point representation.
     
    According to the XC32 User's Guide, both float and double types are 32 bits while long double is 64 bits. Long double should give the accuracy you desired.
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 15944
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Floating point opearation problem. 2018/04/24 09:19:36 (permalink)
    5 (1)
    AKSHAY D BHAVSAR
    qɥb
    You need to refresh your memory about how floating point numbers work.
    Try using "double" instead of float.


    How do i refresh memory?


    You need to refresh YOUR memory.  floats do not work like a calculator. 
    #8
    qɥb
    Monolothic Member
    • Total Posts : 2632
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: Floating point opearation problem. 2018/04/24 15:06:59 (permalink)
    0
    AKSHAY D BHAVSAR
    ...
    I used Double even long double also

    HOW did you use it?
    I suspect you made the common mistake of thinking that the type of the destination variable counts.
    It does not.
    The calculation is done using the largest type of the SOURCE variables, and only after all calculations are complete is the value copied to the destination.
    This is a basic feature of how C does arithmetic.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #9
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/24 23:58:11 (permalink)
    0
    qɥb
    AKSHAY D BHAVSAR
    ...
    I used Double even long double also

    HOW did you use it?
    I suspect you made the common mistake of thinking that the type of the destination variable counts.
    It does not.
    The calculation is done using the largest type of the SOURCE variables, and only after all calculations are complete is the value copied to the destination.
    This is a basic feature of how C does arithmetic.


    long double f=0
    int main()
    {
    f=(long double)(47.1711439-47.0);
    }

    Regards,
    Akshay.
    #10
    qɥb
    Monolothic Member
    • Total Posts : 2632
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: Floating point opearation problem. 2018/04/25 00:01:56 (permalink)
    0
    AKSHAY D BHAVSAR
    f=(long double)(47.1711439-47.0);

    Too late.
    The calculation of 47.1711439-47.0 is done using float arithmetic, and only then do you cast the result to "long double", after you have already lost the extra digits.
    For this to work, you have to cast at least one of the operands to the larger size.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #11
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 00:08:29 (permalink)
    0
    You meant to say
    f=(long double)(47.1711439)-47.0;

    Regards,
    Akshay.
    #12
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 00:50:34 (permalink)
    0
    you can use
     
    f = 47.1711439L - 47.0L;
    #13
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 01:24:15 (permalink)
    0
    I have to convert it to string.I have "float to string"function.But in this case I need to use "long double to string" function that i don't have

    Regards,
    Akshay.
    #14
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 01:29:31 (permalink)
    0
    Can I use sprintf function?

    Regards,
    Akshay.
    #15
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 02:54:39 (permalink)
    0
    why sprintf gives wrong string if we pass double/long double.However it gives correct value if we pass float

    Regards,
    Akshay.
    #16
    qɥb
    Monolothic Member
    • Total Posts : 2632
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: Floating point opearation problem. 2018/04/25 03:02:11 (permalink)
    5 (1)
    Why do you continually ask questions without supplying relevant information?
    In this case, you have not shown HOW you called the sprintf function.
    We can't see your screen, you have to tell us.
    I suspect you have used the wrong format string for a long double.,
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #17
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 03:12:56 (permalink)
    0
    sprintf(str,"%f",( double) 47.11378963);---This works upto 5th decimal place
     
    sprintf(str,"%lf",( long double) 47.11378963);---As i want to use long double.I tried using but couldnt get expected result.
     

    Regards,
    Akshay.
    #18
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 03:17:25 (permalink)
    0
    It is irrelevant to typecast.Please ignore that.

    Regards,
    Akshay.
    #19
    1and0
    Access is Denied
    • Total Posts : 8001
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Floating point opearation problem. 2018/04/25 04:49:04 (permalink)
    4.5 (2)
    qɥb
    Why do you continually ask questions without supplying relevant information?

    +1
     
    AKSHAY D BHAVSAR
    sprintf(str,"%f",( double) 47.11378963);---This works upto 5th decimal place
     
    sprintf(str,"%lf",( long double) 47.11378963);---As i want to use long double.I tried using but couldnt get expected result.

    What result did you get? What result did you expect?
     
    For floating-point constants in XC32, e.g.:
    • 47.11378963 is a double
    • 47.11378963f or 47.11378963F is a float
    • 47.11378963l or 47.11378963L is a long double
     
    For format specifier used by printf():
    • %lf converts the value to a double
    • %Lf converts the value to a long double
     
    Anyway, you should really read the XC32 User's Guide and the 32-Bit Language Tools Libraries Manual!
    post edited by 1and0 - 2018/04/25 05:11:55
    #20
    Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
    Jump to:
    © 2018 APG vNext Trial Version 4.5