SQL 除法 precision/loss 浮点数 x 小数
SQL division precision/loss float x decimal
为什么 SQL 服务器在小数除法上失去精度?
抱歉格式问题,我是用手机发布的...
declare @base decimal(38,10) = -3062.325,
@div decimal(38,10) = 25812561468.9017,
@mult decimal(38,10) = 103177265901.524
select @base / @div * @mult
结果:0
declare @base decimal(28,10) = -3062.325,
@div decimal(28,10) = 25812561468.9017,
@mult decimal(28,10) = 103177265901.524
select @base / @div * @mult
结果:-12236.823736
declare @base float = -3062.325,
@div float = 25812561468.9017,
@mult float = 103177265901.524
select @base / @div * @mult
结果:-12240.6418744047
最奇怪的是 float returns 与 Excel 和 Wolfram alpha ...
的结果相同
Excel 结果:-12240.64187
In SQL 服务器小数精度和结果的小数位数取决于操作数的精度和小数位数
"The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, it is reduced to 38, and the corresponding scale is reduced to try to prevent the integral part of a result from being truncated. In some cases such as multiplication or division, scale factor will not be reduced in order to keep decimal precision, although the overflow error can be raised."
为什么 SQL 服务器在小数除法上失去精度?
抱歉格式问题,我是用手机发布的...
declare @base decimal(38,10) = -3062.325,
@div decimal(38,10) = 25812561468.9017,
@mult decimal(38,10) = 103177265901.524
select @base / @div * @mult
结果:0
declare @base decimal(28,10) = -3062.325,
@div decimal(28,10) = 25812561468.9017,
@mult decimal(28,10) = 103177265901.524
select @base / @div * @mult
结果:-12236.823736
declare @base float = -3062.325,
@div float = 25812561468.9017,
@mult float = 103177265901.524
select @base / @div * @mult
结果:-12240.6418744047
最奇怪的是 float returns 与 Excel 和 Wolfram alpha ...
的结果相同Excel 结果:-12240.64187
In SQL 服务器小数精度和结果的小数位数取决于操作数的精度和小数位数
"The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, it is reduced to 38, and the corresponding scale is reduced to try to prevent the integral part of a result from being truncated. In some cases such as multiplication or division, scale factor will not be reduced in order to keep decimal precision, although the overflow error can be raised."