Float

Last updated on
28 December 2023

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Floating point numbers are stored using the float data type. 

Sample Code:

$field['fieldname'] = array(
  'type' => 'float', 
  'unsigned' => TRUE, 
  'size' => 'normal', // normal / big
  'not null' => FALSE, 
  'description' => t('Field fieldname of tablename.'),
);

Size key actually accepts one of five values (tiny | small | medium | normal | big). But, as you can clearly see in the MySql mappings below, there is no difference between tiny, small medium and normal.

MySQL field mappings as in schema.inc file:
      'float:tiny'      => 'FLOAT',
      'float:small'     => 'FLOAT',
      'float:medium'    => 'FLOAT',
      'float:big'       => 'DOUBLE',
      'float:normal'    => 'FLOAT',
Floating point numbers in very simple words:

Floating point is something that confuses a lot of people. Floating point numbers are essentially numbers stored in scientific notation, almost the same thing that we learned in math class in school.

A floating number stores 300000000 as 3 x 10^8 or 3e8

Two major benefits of floating point representation are:

  1. It can store massive numbers as well as extremely tiny numbers without needing an enormous amount of space.
  2. It has been built over many many years, so its lightning fast for computers to deal with.

Now, we need to have a look at the concept of significant digits.

If you are making a 2D game and you need to know where to point something on the screen, it doesn't matter if its 0.0002 pixels out. In this case 2.6890 instead 2.6892 pixels is permissible rounding. Just first four digits are significant. We do not need to worry about numbers beyond a certain point and we can approximate them. So, in this case, Float is a very strong candidate for the data type of choice.

Floats and Doubles are used to store estimated values. The numbers they store can only be accurate to a certain precision.

They are well known for giving surprising results when doing math problems and in most of the cases, it is because they are capable of maintaining only a certain level of precision.

So, to experience it, In any content type add a field of type Float. Now add content to that content type and enter 123456789 in that float filed. Save it and have a look at the content you just created. Chances are that you will see 12345670000 instead of 123456789 in the float field!! Surprised?

MySQL uses four bytes for single-precision values and eight bytes for double-precision values.

IEEE Standards:

FLOAT is A small (single-precision) floating-point number. Permissible values are -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38 to 3.402823466E+38. 

DOUBLE is A normal-size (double-precision) floating-point number. Permissible values are -1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and 2.2250738585072014E-308 to 1.7976931348623157E+308.

Come on, cheer up!!  don't look scared, I will explain these in simple words in just a minute!!

These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system.

In Simple words, while dealing with floating point numbers, talking in terms of the number of digits after decimal and number of digits before the decimal will just confuse you more and more.

When dealing with floating point numbers we think about most significant digits and the total number of digits

Try to save these numbers as float and see the results. Try to find out the pattern for yourself.

Number Saved 12.123456789 12345.123456 123456789.12
Result 12.123500000 12345.100000 123457000.00

In most of the cases, you will find that float accepts up to 40 digits and treats the first five digits from left most significant.

123456789123456789.123456789 in float will be saved as 1.23457e17 which is equivalent to 123457000000000000. Here the digits after the 5 significant digits are 6789123456789.123456789 and they are rounded to 7000000000000.0000000

Results might be slightly different depending on your hardware or operating system. My purpose behind this article is to make you aware of the basic concept.

So, in most cases Floats store up to 40 digits and treats first five digits most significant and will approximate or round off rest of the digits.

Similarly, Double store up to 79 digits and treats first 16 digits as most significant and will approximate or round off rest of the digits.

For example, if you try to store the same number as a double:

123456789123456789.123456789

It will be stored in double as 1.2345678912345678e17 which is equivalent to 123456789123456780. Here, the digits after 16th digit i.e. 89.123456789 was rounded to 80.00000000

I hope, now you won't be surprised if you give a number 123456789 to float and it
will give you back 123457000!!

Help improve this page

Page status: No known problems

You can: