sscanf resolution

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I am using the Atmel studio 6.2 with the standard library for an xmega processor. I came across an issue with sscanf.

When I convert 8.3331547 from a string into a 32 bits floatting point value, the value is rounded to 8.333155.  I need 2 more digits resolution. Any idea how to go around this issue?

 

Thank You.

 

Charles Guggenheim

Marina,CA

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

32-bit f-p is only good for 7 significant digits.    If you want better resolution,  you need to use a Compiler that will do 64-bit double.   e.g. IAR, Rowley, Imagecraft.

 

Alternatively,  you do your maths with integers and keep track of the decimal point.

It is obvious how you use sscanf to parse 1234.56789 into two integers: 1234L and 56789L

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David has your answer but for a bit more info consider the fact that GCC (like most C compilers) uses IEEE754(R) 32 bit floating point numbers. They are so common you can even find online calculators that can encode/decode into 754 format such as this:

 

http://www.h-schmidt.net/FloatCo...

 

If you type 8.3331547 into that you will see it's immediately converted to 8.333155. What's more if you take the hex representation: 0x4105549a and try the values either side of it: 0x41055499 and 0x4105549b you get 8.333154 and 8.333156 with 8.333155 in the middle. You simply cannot get the extra ...47 resolution in that because 0x4105549a simply does not have enough bits to hold that information.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all for your replies. I did check with the online converter first but I must I have misread the results.

 

Thank you again!

 

Charles