奇怪的SQL计算错误 [英] Strange SQL calculation error

查看:103
本文介绍了奇怪的SQL计算错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用MS SQL Server2005.请检查以下查询.

Hi
I''m using MS SQL Server 2005.Check out the below query.

declare @str float
select  @str =convert(float,isnull(30452.8885,0)) * convert(float,isnull(219.8700,0))
select convert(decimal(38,10),@str) A,@str B



如果我们使用任何计算器进行上述乘法运算,我们将获得B(6695676.594495)作为输出.
但是,当我们将两个值的乘积转换为十进制(38,10)时,sql给出6695676.5944950003作为A,这很奇怪.
将6695676.594495转换为十进制(38,10)时如何得到6695676.5944950003?

知道这种奇怪的计算是如何发生的吗?

问候,
Sunit.



If we do the above multiplication using any calculator we will get B(6695676.594495) as output.
But then when we convert the multiplication of two values to decimal(38,10), sql gives 6695676.5944950003 as A, which is strange.
How can 6695676.594495 when converted to decimal(38,10) give 6695676.5944950003?

Any idea how this strange calculation happens?

Regards,
Sunit.

推荐答案

浮点值包含十进制数字的近似值(值以二进制形式存储,使用有限的位数,请参见"IEEE 754 Wikipedia页面" [
floating point values contains approximations of decimal numbers (values are store in binary form, using a finite number of bits, see "IEEE 754 Wikipedia page"[^] for detailed info).
You may verify that the following C++ program produces the same ouput:
#include<iostream> 
#include <iomanip>
#include<cstdlib> 
using namespace std; 
int main()
{
  double a = 30452.8885;
  double b = 219.8700;
  double k = a * b;
  cout << setprecision(30) << k << endl;
}


这篇关于奇怪的SQL计算错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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