PostgreSQL:real类型的比较

PostgreSQL的数据类型,对于小数来说,有decimal、numeric、real、double precision四种类型,在文档中,其定义为:

Name Storage Size Description Range
decimal variable user-specified precision, exact up to 131072 digits before the decimal point; up to 16383 digits after the decimal point
numeric variable user-specified precision, exact up to 131072 digits before the decimal point; up to 16383 digits after the decimal point
real 4 bytes variable-precision, inexact 6 decimal digits precision
double precision 8 bytes variable-precision, inexact 15 decimal digits precision

注意,real、double的描述为inexact,此时要用此类型进行逻辑判断的话,需要专门处理。

如下述的sql语句,其中lon、lat就定义为real类型:

select tid from taxi where lon1=lon2 and lat1=lat2

这个语句就不能正确的返回结果,需要这样处理才可以返回结果:

select tid from taxi where lon1::numeric=lon2::numeric and lat1::numeric=lat2::numeric

即需要将其转换成numeric类型进行精确判断。


已发布

分类

来自

标签:

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注