• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes

### Hot!Floating point opearation problem.

Page: < 12 Showing page 2 of 2
Author
moser
Super Member
• Total Posts : 335
• Reward points : 0
• Joined: 2015/06/16 02:53:47
• Location: 0
• Status: offline
Re: Floating point opearation problem. 2018/04/27 01:24:20 (permalink)
5 (1)
1and0 is right: The answer is 42.

... page 42 in the 32-Bit Language Tools Libraries Manual ( C:\Program Files (x86)\Microchip\xc32\v2.05\docs\MPLAB C32 Libraries.pdf ).

This is just wrong:
`sprintf(str,"%lf",( long double) 47.11378963);`

It means, you provide the number as 64 bit floating-point value as argument, but then you tell sprint() to use only the first 32 bit of the 64 bit value (which is a part of the 52 bit fraction) and interpreted those as 32 bit floating-point (with 1 bit sign, 8 bit exponent and 23 bit fraction). This is expected to give something completely wrong, except for 0.0 and some very rare random values.
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/27 01:33:42 (permalink)
0
moser
1and0 is right: The answer is 42.

... page 42 in the 32-Bit Language Tools Libraries Manual ( C:\Program Files (x86)\Microchip\xc32\v2.05\docs\MPLAB C32 Libraries.pdf ).

This is just wrong:
`sprintf(str,"%lf",( long double) 47.11378963);`

It means, you provide the number as 64 bit floating-point value as argument, but then you tell sprint() to use only the first 32 bit of the 64 bit value (which is a part of the 52 bit fraction) and interpreted those as 32 bit floating-point (with 1 bit sign, 8 bit exponent and 23 bit fraction). This is expected to give something completely wrong, except for 0.0 and some very rare random values.

Yes I am getting random values.What format specifier I should use.If i use %Lf then nothing happens,Lf gets copied into string.

Regards,
Akshay.
moser
Super Member
• Total Posts : 335
• Reward points : 0
• Joined: 2015/06/16 02:53:47
• Location: 0
• Status: offline
Re: Floating point opearation problem. 2018/04/27 02:12:05 (permalink)
5 (2)
%Lf is the right specifier. But you forgot to tell the basics, which you should always post for any problem:

Which compiler version are you using?
Do you use the Legacy LibC ( -legacy-libc ) or not?
And are you are using a compiler switch like -fno-short-double?

Please note, that XC32 compiler version before XC32 v1.43 does not support the L specifier for doubles.
http://ww1.microchip.com/...all-release-notes.html
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/27 03:04:27 (permalink)
0
I have been using XC32 v1.40

Regards,
Akshay.
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/27 06:55:38 (permalink)
5 (1)
AKSHAY D BHAVSAR
I have been using XC32 v1.40

... which is an old compiler version.  You would had gotten answers faster and earlier when such relevant information was provided from the beginning.
NorthGuy
Super Member
• Total Posts : 4876
• Reward points : 0
• Joined: 2014/02/23 14:23:23
• Status: offline
Re: Floating point opearation problem. 2018/04/27 07:09:08 (permalink)
5 (4)
Switch to integers. This will give you better (and more predictable) resolution:

`i = 471711439 - 470000000;`

You'll get exactly 1711439.

It'll use the same amount of memory as float (32-bit), but calculations will be faster and more precise. Floats certainly have their place, but 99% of the embedded stuff doesn't require them or benefit from them.

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/27 07:11:48 (permalink)
0
1and0
AKSHAY D BHAVSAR
I have been using XC32 v1.40

... which is an old compiler version.  You would had gotten answers faster and earlier when such relevant information was provided from the beginning.

I ll try using latest XC32 v1.44.

Regards,
Akshay.
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/27 07:17:43 (permalink)
5 (1)
AKSHAY D BHAVSAR
1and0
AKSHAY D BHAVSAR
I have been using XC32 v1.40

... which is an old compiler version.  You would had gotten answers faster and earlier when such relevant information was provided from the beginning.

I ll try using latest XC32 v1.44.