convert string to double

In the below function, it muliplies "10.0 * val". Why does it use 10.0? If you remove the 10.0, the return value will still be a double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <ctype.h>

/* atof: convert string s to double */
double atof(char s[])
{
    double val, power;
    int i, sign;

    for (i = 0; isspace(s[i]); i++) /* skip white space */
        ;

    sign = (s[i] == '-') ? -1 : 1;
    if (s[i] == '+' || s[i] == '-')
        i++;
    for (val = 0.0; isdigit(s[i]); i++)
        val = 10.0 * val + (s[i] - '0');
    if (s[i] == '.')
        i++;
    for (power = 1.0; isdigit(s[i]); i++) {
        val = 10.0 * val + (s[i] - '0');
        power *= 10;
    }
    return sign * val / power;
}
I guess is it could slightly be for efficiency (though some of the other code isn't that efficient, so I don't know why). Rather than having to get an int, implicitly convert it into a double and then multiply, they just give a double straight off, removing the need for a conversion.
Topic archived. No new replies allowed.