List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:October 28 1999 11:25pm
Subject:Re: Storing Floats (not critical)
View as plain text  
At 12:31 AM +0200 10/29/99, Oliver Artelt wrote:
>Hi,
>
>I want to store floats into mysql.
>the path:
>
>mysql> create table a1(mg float);
>mysql> show fields from a1;
>+-------+-------------+------+-----+---------+-------+
>| Field | Type        | Null | Key | Default | Extra |
>+-------+-------------+------+-----+---------+-------+
>| mg    | float(10,2) | YES  |     | NULL    |       |
>+-------+-------------+------+-----+---------+-------+
>1 row in set (0.01 sec)
>
>insert into a1 values(0.0023);
>Query OK, 1 row affected (0.06 sec)
>
>mysql> select * from a1;
>+------+
>| mg   |
>+------+
>| 0.00 |
>+------+
>1 row in set (0.00 sec)
>
>mysql> insert into a1 values(2.3e-12);
>Query OK, 1 row affected (0.00 sec)
>
>mysql> select * from a1;
>+------+
>| mg   |
>+------+
>| 0.00 |
>| 0.00 |
>+------+
>2 rows in set (0.00 sec)
>
>mysql> alter table a1 add mf float(8);
>Query OK, 2 rows affected (0.01 sec)
>Records: 2  Duplicates: 0  Warnings: 0
>
>mysql> show fields from a1;
>+-------+--------------+------+-----+---------+-------+
>| Field | Type         | Null | Key | Default | Extra |
>+-------+--------------+------+-----+---------+-------+
>| mg    | float(10,2)  | YES  |     | NULL    |       |
>| mf    | double(16,4) | YES  |     | NULL    |       |
>+-------+--------------+------+-----+---------+-------+
>2 rows in set (0.00 sec)
>
>mysql> insert into a1 values(2.3e-12, 2.3e-12);
>Query OK, 1 row affected (0.00 sec)
>
>mysql> select * from a1;
>+------+--------+
>| mg   | mf     |
>+------+--------+
>| 0.00 |   NULL |
>| 0.00 |   NULL |
>| 0.00 | 0.0000 |
>+------+--------+
>3 rows in set (0.00 sec)
>
>
>mysqladmin  Ver 7.11 Distrib 3.22.21, for pc-linux-gnu on i586
>
>Are you really sure,that this is the right behaviour to handle floats?


float(10,2) means 2 digits of precision, so, yes, it's the correct
behavior for the values that you're adding.

However, as of MySQL 3.23, float(4) and float(8) are true floating-point
types, and will store value to the precision provided by your hardware.

-- 
Paul DuBois, paul@stripped
Thread
Storing Floats (not critical)Oliver Artelt29 Oct
  • Re: Storing Floats (not critical)Paul DuBois29 Oct
  • Re: Storing Floats (not critical)Bob Kline29 Oct
  • Re: Storing Floats (not critical)Arne Mueller29 Oct
    • Re: Storing Floats (not critical)Bob Kline29 Oct
      • Re: Storing Floats (not critical)Michael Widenius8 Nov
  • Re: Storing Floats (not critical)Bogdan Paduraru29 Oct