stringencoders icon indicating copy to clipboard operation
stringencoders copied to clipboard

dtoa & dtoa2 should be short-circuited for values of 0.0

Open GoogleCodeExporter opened this issue 10 years ago • 2 comments

A possible enhancement would be to check for a parameter of 0.0 for the value 
argument and then immediately set the str to "0" and return, similar to the NAN 
check.

e.g. for the case: 

"modp_dtoa2(0.0, str, n);"

The implementation may be better off being:

"size_t modp_dtoa2(double value, char * str, int prec) {
    if (!(value == value)) {    /* Test for NAN. */
        str[0] = 'n'; str[1] = 'a'; str[2] = 'n'; str[3] = 0;
        return (size_t) 3;
    }
    if (x = 0.0) {    /* Test for 0.0. */
        str[0] = '0'; str[1] = 0;
        return (size_t) 1;
    }

        //... Rest of implementation as-is...
}

Original issue reported on code.google.com by [email protected] on 23 Jul 2014 at 12:31

GoogleCodeExporter avatar May 24 '15 02:05 GoogleCodeExporter

Oops, added "if" condition should of course be  "if (value == 0.0)"

Original comment by [email protected] on 23 Jul 2014 at 12:41

GoogleCodeExporter avatar May 24 '15 02:05 GoogleCodeExporter

happy to accept pull requests here

client9 avatar Sep 23 '16 19:09 client9