I am using Laravel's migration system to recreate an existing database structure. The tables need to match the tables in this existing database exactly.
One of the fields on a table is described as so:
`frequency` float unsigned NOT NULL DEFAULT 0,
Looking at the Laravel migration documentation I see the following options:
$table->float('amount', 8, 2);
$table->double('column', 15, 8);
Both of these column types results in creating a double
type column with a length and decimal points. But I need one that is specifically defined as a float
with no specified length and zero decimal points.
It it possible using Laravel's migration system?
Do not use (m,n)
on FLOAT
or DOUBLE
in MySQL. That feature belongs only on DECIMAL
. If you want zero decimal places, use some form of INT
. On the other hand, maybe your frequency
is in the gigahertz? At which point FLOAT
without precision or decimals, is the most appropriate.
Note that 1GHz will not fit in FLOAT(8,2)
, but it will fit in FLOAT
.
Why both?... FLOAT
takes 4 bytes; DOUBLE
takes 8; both are part of the IEEE-754 standard that virtually every computer hardware implements. In the "old" days (when 1MB of RAM was a "big" machine; the standard dates back to the early '80s), saving 4 bytes was a big deal. If Laravel is turning float into double, that is its choice; MySQL will take either.
In virtually all situations FLOAT
and DOUBLE
values and variables are interchangeable. But... A value stored in FLOAT
and the same value stored in DOUBLE
may lead to values that compare unequal. For example, float(1.234) != double(1.234) in virtually all languages, hardware, etc.
MySQL is happy with frequency float unsigned NOT NULL DEFAULT 0
.
But, if $table->float('amount', 8, 2);
in Laravel means the same as MySQL's FLOAT(8,2)
, then you are likely to have big problems.
1234567.89
will be truncated to 999999.99
. And 1.234
will be truncated to 1.23
, then rounded to binary.
Sounds like a bug report for Lavaral. (Sorry, I have never used Lavaral, so I am guessing at its details. I do know MySQL and IEEE-754.)