diff options
author | unknown <monty@mysql.com> | 2005-02-22 12:51:23 +0200 |
---|---|---|
committer | unknown <monty@mysql.com> | 2005-02-22 12:51:23 +0200 |
commit | cb8d9c3ad40f00018cff05168e731ff2547d6144 (patch) | |
tree | af26a8ec65a080dd408d6df18990ff71c50577aa /strings/strtod.c | |
parent | ae8c3e130a91687839b570f82f6694f81c21dbaf (diff) | |
download | mariadb-git-cb8d9c3ad40f00018cff05168e731ff2547d6144.tar.gz |
Backport my_strntod() from 5.0
Change string->float conversion to delay division as long as possible.
This gives us more exact integer->float conversion for numbers of type '123.45E+02' (Bug #7740)
client/mysql.cc:
Fix wront usage of charset (found during review of pushed code)
include/m_string.h:
Backported my_strtod() from 5.0
mysql-test/mysql-test-run.sh:
Run also mysql_client_test with --debug
mysql-test/r/ps_1general.result:
Safety fix (if mysql_client_test.test fails)
mysql-test/r/type_float.result:
More test
mysql-test/t/mysql_client_test.test:
Comments for what to do if this test fails
mysql-test/t/ps_1general.test:
Safety fix (if mysql_client_test.test fails)
mysql-test/t/type_float.test:
More test to better test new strtod() function
Test also bug #7740 (wrong comparsion between integer and float-in-integer-range)
sql/field.cc:
Backport my_strntod() from 5.0
sql/item.cc:
Backport my_strntod() from 5.0
sql/item.h:
Backport my_strntod() from 5.0
sql/item_func.h:
Backport my_strntod() from 5.0
sql/item_strfunc.cc:
Backport my_strntod() from 5.0
sql/item_sum.cc:
Backport my_strntod() from 5.0
sql/item_sum.h:
Backport my_strntod() from 5.0
sql/procedure.h:
Backport my_strntod() from 5.0
strings/ctype-simple.c:
Backport my_strntod() from 5.0
strings/ctype-ucs2.c:
Backport my_strntod() from 5.0
strings/strtod.c:
Backport my_strntod() from 5.0
Change conversion to delay division as long as possible.
This gives us more exact integer-> float conversion for numbers of type '123.45E+02'
Diffstat (limited to 'strings/strtod.c')
-rw-r--r-- | strings/strtod.c | 167 |
1 files changed, 119 insertions, 48 deletions
diff --git a/strings/strtod.c b/strings/strtod.c index bc8105b8040..61f2c107abe 100644 --- a/strings/strtod.c +++ b/strings/strtod.c @@ -2,7 +2,7 @@ An alternative implementation of "strtod()" that is both simplier, and thread-safe. - From mit-threads as bundled with MySQL 3.23 + Original code from mit-threads as bundled with MySQL 3.23 SQL:2003 specifies a number as @@ -29,6 +29,8 @@ #include "my_base.h" /* Includes errno.h */ #include "m_ctype.h" +#define MAX_DBL_EXP 308 +#define MAX_RESULT_FOR_MAX_EXP 1.79769313486232 static double scaler10[] = { 1.0, 1e10, 1e20, 1e30, 1e40, 1e50, 1e60, 1e70, 1e80, 1e90 }; @@ -37,89 +39,157 @@ static double scaler1[] = { }; -double my_strtod(const char *str, char **end) +/* + Convert string to double (string doesn't have to be null terminated) + + SYNOPSIS + my_strtod() + str String to convert + end_ptr Pointer to pointer that points to end of string + Will be updated to point to end of double. + error Will contain error number in case of error (else 0) + + RETURN + value of str as double +*/ + +double my_strtod(const char *str, char **end_ptr, int *error) { double result= 0.0; - int negative, ndigits; - const char *old_str; + uint negative= 0, ndigits, dec_digits= 0, neg_exp= 0; + int exp= 0, digits_after_dec_point= 0; + const char *old_str, *end= *end_ptr, *start_of_number; + char next_char; my_bool overflow=0; + *error= 0; + if (str >= end) + goto done; + while (my_isspace(&my_charset_latin1, *str)) - str++; + { + if (++str == end) + goto done; + } + start_of_number= str; if ((negative= (*str == '-')) || *str=='+') - str++; + { + if (++str == end) + goto done; /* Could be changed to error */ + } + + /* Skip pre-zero for easier calculation of overflows */ + while (*str == '0') + { + if (++str == end) + goto done; + start_of_number= 0; /* Found digit */ + } old_str= str; - while (my_isdigit (&my_charset_latin1, *str)) + while ((next_char= *str) >= '0' && next_char <= '9') { - result= result*10.0 + (*str - '0'); - str++; + result= result*10.0 + (next_char - '0'); + if (++str == end) + { + next_char= 0; /* Found end of string */ + break; + } + start_of_number= 0; /* Found digit */ } - ndigits= str-old_str; + ndigits= (uint) (str-old_str); - if (*str == '.') + if (next_char == '.' && str < end-1) { - double p10=10; - str++; - old_str= str; - while (my_isdigit (&my_charset_latin1, *str)) + /* + Continue to add numbers after decimal point to the result, as if there + was no decimal point. We will later (in the exponent handling) shift + the number down with the required number of fractions. We do it this + way to be able to get maximum precision for numbers like 123.45E+02, + which are normal for some ODBC applications. + */ + old_str= ++str; + while (my_isdigit(&my_charset_latin1, (next_char= *str))) { - result+= (*str++ - '0')/p10; - p10*=10; + result= result*10.0 + (next_char - '0'); + digits_after_dec_point++; + if (++str == end) + { + next_char= 0; + break; + } } - ndigits+= str-old_str; - if (!ndigits) str--; + /* If we found just '+.' or '.' then point at first character */ + if (!(dec_digits= (uint) (str-old_str)) && start_of_number) + str= start_of_number; /* Point at '+' or '.' */ } - if (ndigits && (*str=='e' || *str=='E')) + if ((next_char == 'e' || next_char == 'E') && + dec_digits + ndigits != 0 && str < end-1) { - int exp= 0; - int neg= 0; const char *old_str= str++; - if ((neg= (*str == '-')) || *str == '+') + if ((neg_exp= (*str == '-')) || *str == '+') str++; - if (!my_isdigit (&my_charset_latin1, *str)) + if (str == end || !my_isdigit(&my_charset_latin1, *str)) str= old_str; else { - double scaler= 1.0; - while (my_isdigit (&my_charset_latin1, *str)) + do { - if (exp < 9999) /* protection against exp overflow */ - exp= exp*10 + *str - '0'; + if (exp < 9999) /* prot. against exp overfl. */ + exp= exp*10 + (*str - '0'); str++; - } - if (exp >= 1000) + } while (str < end && my_isdigit(&my_charset_latin1, *str)); + } + } + if ((exp= (neg_exp ? exp + digits_after_dec_point : + exp - digits_after_dec_point))) + { + double scaler; + if (exp < 0) + { + exp= -exp; + neg_exp= 1; /* neg_exp was 0 before */ + } + if (exp + ndigits >= MAX_DBL_EXP + 1 && result) + { + /* + This is not 100 % as we actually will give an owerflow for + 17E307 but not for 1.7E308 but lets cut some corners to make life + simpler + */ + if (exp + ndigits > MAX_DBL_EXP + 1 || + result >= MAX_RESULT_FOR_MAX_EXP) { - if (neg) - result= 0.0; - else + if (neg_exp) + result= 0.0; + else overflow= 1; goto done; } - while (exp >= 100) - { - scaler*= 1.0e100; - exp-= 100; - } - scaler*= scaler10[exp/10]*scaler1[exp%10]; - if (neg) - result/= scaler; - else - result*= scaler; } + scaler= 1.0; + while (exp >= 100) + { + scaler*= 1.0e100; + exp-= 100; + } + scaler*= scaler10[exp/10]*scaler1[exp%10]; + if (neg_exp) + result/= scaler; + else + result*= scaler; } done: - if (end) - *end = (char *)str; + *end_ptr= (char*) str; /* end of number */ if (overflow || isinf(result)) { result= DBL_MAX; - errno= EOVERFLOW; + *error= EOVERFLOW; } return negative ? -result : result; @@ -127,6 +197,7 @@ done: double my_atof(const char *nptr) { - return (my_strtod(nptr, 0)); + int error; + const char *end= nptr+65535; /* Should be enough */ + return (my_strtod(nptr, (char**) &end, &error)); } - |