Commit 51781b0b authored by jimw@mysql.com's avatar jimw@mysql.com

Fix conversion of floating point values to character fields when the

absolute value of the float is less than 1, and also fix calculation of
length for negative values. (Bug #7774)
parent b89d35fa
......@@ -179,3 +179,18 @@ f
9.999
9.999
drop table if exists t1;
create table t1 (c char(20));
insert into t1 values (5e-28);
select * from t1;
c
5e-28
drop table t1;
create table t1 (c char(6));
insert into t1 values (2e5),(2e6),(2e-4),(2e-5);
select * from t1;
c
200000
2e+06
0.0002
2e-05
drop table t1;
......@@ -179,3 +179,18 @@ f
9.999
9.999
drop table if exists t1;
create table t1 (c char(20));
insert into t1 values (5e-28);
select * from t1;
c
5e-28
drop table t1;
create table t1 (c char(6));
insert into t1 values (2e5),(2e6),(2e-4),(2e-5);
select * from t1;
c
200000
2e+06
0.0002
2e-05
drop table t1;
......@@ -103,3 +103,13 @@ create table t1 (f double(4,3));
insert into t1 values (-11.0),(-11),("-11"),(11.0),(11),("11");
select * from t1;
drop table if exists t1;
# Check conversion of floats to character field (Bug #7774)
create table t1 (c char(20));
insert into t1 values (5e-28);
select * from t1;
drop table t1;
create table t1 (c char(6));
insert into t1 values (2e5),(2e6),(2e-4),(2e-5);
select * from t1;
drop table t1;
......@@ -4301,13 +4301,20 @@ int Field_str::store(double nr)
char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
uint length;
bool use_scientific_notation= TRUE;
use_scientific_notation= TRUE;
if (field_length < 32 && fabs(nr) < log_10[field_length]-1)
/*
Check fabs(nr) against longest value that can be stored in field,
which depends on whether the value is < 1 or not, and negative or not
*/
double anr= fabs(nr);
int neg= (nr < 0.0) ? 1 : 0;
if (field_length < 32 &&
(anr < 1.0 ? anr > 1/(log_10[max(0,field_length-neg-2)]) /* -2 for "0." */
: anr < log_10[field_length-neg]-1))
use_scientific_notation= FALSE;
length= (uint) my_sprintf(buff, (buff, "%-.*g",
(use_scientific_notation ?
max(0, (int)field_length-5) :
max(0, (int)field_length-neg-5) :
field_length),
nr));
/*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment