开发者

MySQL float values jumping around on insert?

开发者 https://www.devze.com 2022-12-28 04:08 出处:网络
So i have a SQL table setup as such CREATE TABLE IF NOT EXISTS `points` ( `id` int(11) NOT NULL auto_increment,

So i have a SQL table setup as such

CREATE TABLE IF NOT EXISTS `points` (
  `id` int(11) NOT NULL auto_increment,
  `lat` float(10,6) NOT NULL,
  `lng` float(10,6) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

And im inserting stuff like

INSERT INTO `points` (`lat`, `lng`) VALUES ('89.1开发者_开发问答23456','-12.123456');

Gives me a row with lat and lng being 89.123459 and -12.123455

Whats up?


In computers a "float" value is stored as a number times a number squared generally, and some numbers can't be stored exactly as entered. If you need it exact you should store it as DECIMAL(8,6)


FLOAT data types have rounding effects, because not every fractional base-10 number can be represented in the base-2 format of IEEE 754. For this reason, FLOAT and DOUBLE PRECISION should be considered inexact numeric data types.

Read: "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (http://www.validlab.com/goldberg/paper.pdf)

+1 to @MindStalker's answer -- use NUMERIC or DECIMAL if you need exact numeric data types.

0

精彩评论

暂无评论...
验证码 换一张
取 消