Comments on: Re: MySQL 5.1 vs. MySQL 5.5: Floats, Doubles, and Scientific Notation https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation Blog by Shlomi Noach Tue, 15 Apr 2014 22:28:20 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: Anonymous https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-237168 Tue, 15 Apr 2014 22:28:20 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-237168 This bug still seems to exist 1 year later …

]]>
By: Alfie John https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-217418 Tue, 20 Aug 2013 07:57:48 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-217418 Currently testing a migration to 5.6. Ran a checksum and was smacked in the face by a wall of changes.

After doing dumps+dumps of random tables, it looks like it’s this issue.

@shlomi: thanks for a suitable work around.

]]>
By: Sheeri https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-178018 Mon, 11 Feb 2013 17:22:02 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-178018 Note that depending on float precision, this still might not work. I tried to use your workaround and it still showed differences, and I think it’s because we’re not setting the float precision (and by default there is ‘none’).

I found that with CAST, when using a float precision of 30 or less on a double field, it works, but not 31 or more:

mysql> select @@version; SELECT
ROUND(IF(double_field BETWEEN -1 AND 1, CAST(double_field AS DECIMAL(65,30)), double_field), 30) as float_p30,
ROUND(IF(double_field BETWEEN -1 AND 1, CAST(double_field AS DECIMAL(65,30)), double_field), 31) as float_p31 from my_table where id IN (val1,val2,val3);
+————+
| @@version |
+————+
| 5.1.61-log |
+————+
1 row in set (0.00 sec)

+———————————-+————-+
| float_p30 | float_p31 |
+———————————-+————-+
| 0.000075488799999999981024841056 | 7.54888e-05 |
| 0.000090369599999999989390145338 | 9.03696e-05 |
| 0.000059410600000000003402360338 | 5.94106e-05 |
+———————————-+————-+
3 rows in set (0.00 sec)

This kind of truncation also happens on MariaDB, but in a different way – it truncates to 10 decimal places.

MariaDB [addons_allizom_org]> SELECT ROUND(IF(movers BETWEEN -1 AND 1, CAST(movers AS DECIMAL(65,30)), movers), 30) as float_p30,ROUND(IF(movers BETWEEN -1 AND 1, CAST(movers AS DECIMAL(65,30)), movers), 31) as float_p31 from addons_allizom_org.personas where id in (4313,33619,66706);
+———————————-+————–+
| float_p30 | float_p31 |
+———————————-+————–+
| 0.000075488800000000000000000000 | 0.0000754888 |
| 0.000090369599999999990000000000 | 0.0000903696 |
| 0.000059410600000000000000000000 | 0.0000594106 |
+———————————-+————–+
3 rows in set (0.00 sec)

MariaDB truncates without any warnings, which is odd.

]]>
By: Data Industry news round up, Log Buffer #304 https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-167152 Fri, 25 Jan 2013 14:08:30 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-167152 […] Sheeri’s MySQL 5.1 vs. MySQL 5.5: Floats, Doubles, and Scientific Notation, Shlomi Noach was baffled at this change of floating point number […]

]]>
By: Rick James https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-166847 Thu, 24 Jan 2013 20:18:36 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-166847 FLOAT goes down to about 1e-38, so DECIMAL(x, 30) can lose precision, even lose _all_ precision. DOUBLE is much further.

What is needed is to FORMAT to some number of _significant_ digits, not some number of _decimal places_.

If the numbers are different on the two machines, they should be flagged as different. Converting to strings, then comparing the strings is the source of the problem.

Unfortunately, there is not a CAST AS BINARY to produce the internal representation. (A trick like that works to bypass character set issues.) Probably 99.99% of MySQL installations use hardware that uses IEEE 754, so I suggest that it is not reasonable to argue about differing representations.

]]>
By: Sheeri https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-166767 Thu, 24 Jan 2013 17:23:17 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-166767 Interesting – I’d tried FORMAT() and CAST (and CONVERT) by itself, not with rounding, and nothing I’d tried there worked. I appreciate that workaround!

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-166763 Thu, 24 Jan 2013 17:21:17 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-166763 Baron,

FORMAT() seems to work well for small numbers (for larger numbers, which are of no interest in this problem, it adds commas to denote orders of magnitude).
It apparently doesn’t have a limit on the number of digits after the floating point (FLOAT and DOUBLE max at 30 digits when casted to DECIMAL) — but this is just silent: when FORMATting numbers with over 30 digits, result is rounded to 30 digits no matter what.
So in conclusion I don’t see the difference between the two.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-166725 Thu, 24 Jan 2013 16:18:13 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-166725 Peter,
Well, I don’t touch large numbers as you can see. With small numbers I’m casting with up to 30 digits after the period.

Moreover, consider I’ve put this into the float-precision option, whose target in the first place is to do round ups… So yes, there’s round up, and it is intentional.

]]>
By: Peter Laursen https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-166716 Thu, 24 Jan 2013 15:59:15 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-166716 Now if you have extremely small (atomar physics) or extremely large numbers (astronomics) a CAST to DECIMAL may truncate data I think? And in that case after truncation different values may appear as identical values.

Right/wrong?

]]>
By: Baron https://shlomi-noach.github.io/blog/mysql/re-mysql-5-1-vs-mysql-5-5-floats-doubles-and-scientific-notation/comment-page-1#comment-166676 Thu, 24 Jan 2013 14:29:55 +0000 https://shlomi-noach.github.io/blog/?p=6028#comment-166676 I think it might be better to use FORMAT() instead. But that is just from my memory, I haven’t looked at the documentation to remind myself of how it works.

]]>