Pages: [1]   Go Down
Author Topic: ftoa  (Read 1189 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been looking around for a ftoa function. The ones that I found seemed to fail for numbers such as 0.05, you'd get 0.5, it would loose any leading zeros in the fraction. Below is a version that should fix this.

Code:
char *ftoa(char *buffer, double d, int precision) {

long wholePart = (long) d;

// Deposit the whole part of the number.

itoa(wholePart,buffer,10);

// Now work on the faction if we need one.

if (precision > 0) {

// We do, so locate the end of the string and insert
// a decimal point.

char *endOfString = buffer;
while (*endOfString != '\0') endOfString++;
*endOfString++ = '.';

// Now work on the fraction, be sure to turn any negative
// values positive.

if (d < 0) {
d *= -1;
wholePart *= -1;
}

double fraction = d - wholePart;
while (precision > 0) {

// Multipleby ten and pull out the digit.

fraction *= 10;
wholePart = (long) fraction;
*endOfString++ = '0' + wholePart;

// Update the fraction and move on to the
// next digit.

fraction -= wholePart;
precision--;
}

// Terminate the string.

*endOfString = '\0';
}

    return buffer;
}
« Last Edit: June 11, 2011, 11:58:45 am by Martin Waller » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I developed this in Microsoft C++ Express, you will need to remove the leading _ on the call to itoa! Sorry!
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You still should be able to click modify and alter your post.

Also please use the [code ][/code ] tags
Logged

Gloucester
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Martin,
Can you give an example of how to use?

Yours Simon M.
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been looking around for a ftoa function.
The avr-libc library used with Arduino has two such conversion functions: dtostre() for scientific notation and dtstrf() for fixed point.

Quote from: Martin Waller
...Below is a version...
I would like to see rounded values in the output.

So, if I had a number, say, 0.014567 and I wanted three decimal places I think it should be 0.015.  With four decimal places I would expect to see 0.0146. Stuff like that.

Code:
void setup()
{
    Serial.begin(9600);
    float x = 0.014567;
    Serial.println("x was set to 0.014567");
    Serial.println("Here are results with ftoa, dtostrf and dtostre");
    Serial.println();

    char buffer[30]; // Could be smaller, but...
    for (int i = 0; i < 7; i++) {

        ftoa(buffer, x, i);
        Serial.print(buffer);
        Serial.print("  ");
        dtostrf(x, i+4, i, buffer); // avr-libc function for floats
        Serial.print(buffer);
        Serial.print("  ");
        dtostre(x, buffer, i, NULL); // avr-libc function for scientific notation
        Serial.println(buffer);

    }
}

void loop() {}

// Your ftoa goes here


Output:

x was set to 0.014567
Here are results with ftoa, dtostrf and dtostre

0     0  1e-02
0.0    0.0  1.5e-02
0.01    0.01  1.46e-02
0.014    0.015  1.457e-02
0.0145    0.0146  1.4567e-02
0.01456    0.01457  1.45670e-02
0.014566    0.014567  1.456700e-02


Bottom line: It's interesting, but, as a general purpose conversion routine it has a few problems other than my preference for rounding.  What happens if you give it x = 1234567.0?  That one is pretty easy to fix, I'm thinking (don't forget to test -1234567.0 also).


Regards,

Dave

« Last Edit: June 15, 2011, 10:46:46 am by davekw7x » Logged

Pages: [1]   Go Up
Jump to: