mysql轮询_MySQL轮询查询,结果错误

bd96500e110b49cbb3cd949968f18be7.png

I have a question about a query that I'm running on a MySQL Server (v5.5.50-0+deb8u1).

SELECT 12 - (SELECT qty FROM Table WHERE id = 5213) AS Amount

so Amount value is 12 - 8,5500000000000007 = 3.4499999999999993

But if I run the query:

SELECT qty FROM Table WHERE id = 5213

it returns 8.55 that is the correct number written in the record, so I was expecting that the first querty returned 3.45.

The "qty" column in the table "Table" is a DOUBLE.

How is it possibile? How can I get the right answer from the query?

thanks in advance

解决方案Floating-point numbers sometimes cause confusion because they are

approximate and not stored as exact values. A floating-point value as

written in an SQL statement may not be the same as the value

represented internally.

This statement holds true for many programming languages as well. Some numbers don't even have an exact representation. Here's something from the python manual

The problem is easier to understand at first in base 10. Consider the

fraction 1/3. You can approximate that as a base 10 fraction:

0.3 or, better,

0.33 or, better,

0.333 and so on. No matter how many digits you’re willing to write down, the result will never be exactly 1/3, but will be an

increasingly better approximation of 1/3.

In the same way, no matter how many base 2 digits you’re willing to

use, the decimal value 0.1 cannot be represented exactly as a base 2

fraction. In base 2, 1/10 is the infinitely repeating fraction

So in short generally doing is float1 = float2 type of comparison is a bad idea but everyone keeps forgetting it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值