在mysql中减去两个相等的值不为零 [英] Subtraction of two equal values is not zero in mysql

查看:214
本文介绍了在mysql中减去两个相等的值不为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在mysql中将两个相等的值相减不等于零?

Why Subtraction of two equal values in mysql does not equal zero?

两个字段类型均为双精度.见下图

both field type are double. See image below

推荐答案

这称为近似精度.这不是错误,浮点数据类型旨在以这种方式工作.他们不能精确地存储数据.因此,如果这很重要,则应使用定点数据类型,例如

That's known as a approximate precision . This isn't an error, floating point data types are intended to work such way. They can not store data precisely. So if that matters, you should use fixed-point data types, such as DECIMAL in MySQL.

另一方面,您始终可以使用精度增量进行浮点比较,例如:

On the other hand, you can always use precision delta for comparisons for floating point, like:

SELECT 
  `foo`,
  `bar`,
  IF(ABS(`foo`-`bar`)<1E-13, 0, `foo`-`bar`) AS zero_compared
FROM
 t

如您所见,这里的增量是1E-13(通常就足够了)

as you can see, here delta is 1E-13 (normally, that will be enough)

这篇关于在mysql中减去两个相等的值不为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆