Bug #4172 Floating point conversion looses precision (prepared staements)
Submitted: 16 Jun 2004 21:42 Modified: 4 Nov 2004 21:48
Reporter: Konstantin Osipov (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.1, 5.0 OS:Any (All)
Assigned to: Konstantin Osipov CPU Architecture:Any

[16 Jun 2004 21:42] Konstantin Osipov
Description:
There is an error in floating point conversion somewhere in the server, client, or both.
Data is converted to string representation and back.

How to repeat:
static void test_sap8()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  const char *stmt_text;
  MYSQL_RES *res;
  MYSQL_ROW row;
  int rc;
  char *buf[100];
  long lval;

  myheader("test_sap8");

  mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  mysql_query(mysql, "CREATE TABLE t1 (a FLOAT)");
  mysql_query(mysql, "INSERT INTO t1 VALUES (112.32101234568)");

  stmt = mysql_stmt_init(mysql);
  stmt_text= "SELECT a FROM t1";

  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  check_execute(stmt, rc);
  rc= mysql_stmt_execute(stmt);
  check_execute(stmt, rc);

  bzero(bind, sizeof(bind));
  bind[0].buffer_type = MYSQL_TYPE_STRING;
  bind[0].buffer = (char *)buf;
  bind[0].buffer_length = 50;
  bind[0].length = &lval;
  lval = 50;

  mysql_stmt_bind_result(stmt, bind);

  rc= mysql_stmt_fetch(stmt);
  check_execute(stmt, rc);

  printf("Value = %s  Length = %d\n", buf, lval);

  mysql_stmt_close(stmt);

  mysql_query(mysql,"SELECT a + 0.00000000000000000 FROM t1");
  res = mysql_store_result(mysql);
  row = mysql_fetch_row(res);
  printf ("Value via query: %s\n", row[0]);
  mysql_free_result(res);
  mysql_stmt_close(stmt);
}

Suggested fix:
-
[17 Jun 2004 19:49] Konstantin Osipov
The program above prints:
#####################################
123 of (1/1): test_bug4172  
#####################################
Value = 112.321  Length = 7
Value via query: 112.3210144043
[10 Jul 2004 23:35] Konstantin Osipov
Georg,
the test case is not correct.
The reason you're getting different results is that you request different results.
When you fetch from prepared statement, you fetch a float value, and when you use floating point arithmetic in the query, result value is double.
Hence difference in precision.
However there are other bugs in this code which I'm now trying to fix.
BTW, I'd not recomend you using float, as all MySQL arithmetic is in double, it's better to use
float support is quite limited.
[19 Jul 2004 20:51] Konstantin Osipov
bk commit - 4.1 tree (konstantin:1.1966) BUG#4172
[4 Nov 2004 21:26] Konstantin Osipov
The client side fixed using sprintf in 4.1.7
[4 Nov 2004 21:48] Konstantin Osipov
Additional info: 
Subject: bk commit - 4.1 tree (konstantin:1.2090) BUG#4172

ChangeSet
  1.2090 04/11/05 00:45:41 konstantin@mysql.com +1 -0
  A test case for Bug#4172 "Floating point conversion looses precision
  (prepared staements)": adding the test case to close the bug
  (the bug was fixed along with other conversion incompatibilities
  in 4.1.7)