Skip to content
Permalink
Browse files

Fixing bug where scientific notations were not passed to mysql in the…

…ir original form which resulted in loss of precision
  • Loading branch information...
Jelle Henkens
Jelle Henkens committed Sep 5, 2011
1 parent 15a1741 commit 53d221c3d17ac273a0a6fdc313d8fa8c6f6ece61
@@ -672,7 +672,7 @@ function value($data, $column = null, $safe = false) {
return 'NULL';
}
if (is_float($data)) {
return sprintf('%F', $data);
return str_replace(',', '.', sprintf('%G', $data));
}
if ((is_int($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
@@ -239,14 +239,33 @@ function testLocalizedFloats() {
setlocale(LC_ALL, 'de_DE');
$result = $this->db->value(3.141593, 'float');
$this->assertEqual((string)$result, '3.141593');
$this->assertTrue(strpos((string)$result, ',') === false);
$result = $this->db->value(3.141593);
$this->assertEqual((string)$result, '3.141593');
$this->assertTrue(strpos((string)$result, ',') === false);
$result = $this->db->value(2.2E-54, 'float');
$this->assertEqual('2.2E-54', (string)$result);
$result = $this->db->value(2.2E-54);
$this->assertEqual('2.2E-54', (string)$result);
setlocale(LC_ALL, $restore);
}
/**
* test that scientific notations are working correctly
*
* @return void
*/
function testScientificNotation() {
$result = $this->db->value(2.2E-54, 'float');
$this->assertEqual('2.2E-54', (string)$result);
$result = $this->db->value(2.2E-54);
$this->assertEqual('2.2E-54', (string)$result);
}
/**
* testTinyintCasting method
*

1 comment on commit 53d221c

@default007

This comment has been minimized.

Copy link

default007 commented on 53d221c Oct 5, 2011

This fix, that seems to be good for scientific notation, actually spoiled my non-scientific floats...

If I want to store a number like 6117.2830435934, CakePHP actually rounds it off without considering the schema. That's bad. But at least before, with %F, the loss of precision was not so dramatic, as it chopped it to 6117.283044, that was good enough for my FLOAT(11,4) precision in the database.

But now, when using %G, it actually writes 6117.28, with an enormous loss of precision!

So it is a bad fix for my case, and I had to revert it after the last upgrade to 1.3.12, as my tests started to fail. Maybe everyone would be happy if you used %E instead...

Compare these outputs (in PHP 5.3.2-1ubuntu4.9):

sprintf("%F", 123456.2345678901) -> 123456.234568
sprintf("%G", 123456.2345678901) -> 123456
sprintf("%E", 123456.2345678901) -> 1.234562E+5

sprintf("%F", 1234567.2345678901) > 1234567.234568
sprintf("%E", 1234567.2345678901) -> 1.234567E+6
sprintf("%G", 1234567.2345678901) -> 1.23457E+6

If CakePHP is not going to look at the schema to find out the desired precision, then this sprintf should use by default the maximum precision possible. (?)

I wanted to report a bug, but lighthouseapp is down: http://twitter.com/#!/lighthouseapp/status/121591724710707201 I'm writing this here temporarily, to keep the information somewhere.

Please sign in to comment.
You can’t perform that action at this time.