tknapstad / stringencoders

Automatically exported from code.google.com/p/stringencoders
0 stars 0 forks source link

10^0 is 1, not 0. #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
in source 
file: modp_numtoa.c
line: ln14 - ln19
------------------------------------------------------------------------
/**
 * Powers of 10
 * 10^0 to 10^9
 */
static const double pow10[] = {0, 10, 100, 1000, 10000, 100000, 1000000,
                               10000000, 100000000, 1000000000};
------------------------------------------------------------------------
The array pow10's first element is 10^0, but why the value is 0?

Original issue reported on code.google.com by superstr...@gmail.com on 24 Sep 2007 at 4:30

GoogleCodeExporter commented 9 years ago
Hi thanks for the comment.

It's really a documentation bug.  The code works fine, unless you are saying 
otherwise.

the pow10 array is for floating point precision.  So a precision of 1 will using
pow10[1] in the array (i.e. 10).

The code works by doing something like this
Given 1234.1
remove print integer part. (1234)
then with the faction, multiple by pow10[precision]
so 0.1 * 10 = 1
print that.

if the precision is 0, then the factional part is not printed.

Looking at the code, it appears that pow10[0] could be 1 and it would work just 
fine.
I'll fool around and see what is most clear.  If anything I'll update the docs

    int whole = (int) value;
    double tmp = (value - whole) * pow10[prec];
    uint32_t frac = (uint32_t)(tmp);
    if (tmp - frac > 0.5) {
        ++frac;
    }

thanks again!

--nickg

Original comment by nickgsup...@gmail.com on 28 Sep 2007 at 12:20

GoogleCodeExporter commented 9 years ago
I think this all cleared up now.
thanks,
closing

Original comment by nickgsup...@gmail.com on 7 Jan 2008 at 1:21