How to safely and reasonably convert a float or double to string or char array?

@krupski

I assume you have software background considering the conversation we had so far. In firmware this is not a forgiven small issue. I have worked in a place, where things smaller than this can make your manager go crazy. This is why it takes a lot more time to write firmware than software, you may go and read on this. It take significantly higher amount of time in average to produce a line in firmware than a line in software and the cost estimation for each line also very different mainly due to this.

I had once worked on a project where I was required to test a code for 72 hours straight with very critical timing. And oh boy a mistake like this would be the last thing you want. Imagine you test your code for 72 hours to figure out when you get to 71th hour the dtostrf does something crazy because of its range. It would probably so hard to find it.

And yeah I do not even know how to compile the code again. So, if you can point me to a guide that would be awesome so at the end I would do a comparison. I have never read a line of code in java nor touched a java compiler nor know how the whole thing works so there is not much I can do unless I spend significant amount of time to figure it out.

I do realize how sad it is that I cannot use the GCC (I think it is part of AVR-lib but it is not something that matters) floating point, but since no one had a solution I am making my own dtostrf.

And btw for the sprintf part, I am doing it for code compatibility and in case the sprintf was not modified to accept formatting.

My progress on dtostrf:

there are 3 ways to do this:

  1. inefficient and in accurate:
    This method is done by mathematically cracking up the float. A code example can be found here:
    c++ - Convert a float to a string - Stack Overflow

  2. More efficient with good accuracy (if not perfect) but limited range
    In this method they use the largest integer supported on the software and then use ltoa to get the digits out. The limitation in range is due to 32bit or 64bit integer number. You can see the code here:
    ftoa() implementation | Forum for Electronics

  3. the accurate and the most efficient version:
    There are algorithm used to do dtostrf and apparently the back story is kind of interesting. The initial algorithm was Dragon 2 and then Dragon 4. However it was then improved further and was named Grisu. The Grisu 2 and Grisu 3 also exist based on some of the documents I have read. The best link regarding Grisu is here:
    Printing Floating-Point Numbers - RyanJuckett.com

A bench mark comparison between these can be found here

I either use method 3 or a hybrid version where less memory is used because Grisu needs some stuff stored in an array. At this point I am not sure what they are but I am reading on them. I also have a feeling that the arduino __floatEngine works with the same method because it appears to me that some numbers are stored however it could be method number 1. I can confirm this when I finish reading and understading Grisu