gpt4 book ai didi

c# - SQL Server EXP 到数字的精度转换结果丢失

转载 作者:行者123 更新时间:2023-11-30 22:03:56 24 4
gpt4 key购买 nike

我有这个 C# 代码。

double x = 5.8856571269513607   
double result = Math.Exp(-x * x)

结果的值为 0.00000000000000090286393216423726。

我想将其移植到 T-SQL,所以我这样做了。

declare @x numeric(17,16),
@result numeric(32,31)

set @x = 5.8856571269513607

set @result = CAST(exp(-@x * @x) AS numeric(32,31))

@result的值为0.0000000000000009000000000000000

知道为什么它在 9 之后失去了精度吗?

提前致谢。

最佳答案

这是在使用 EXP() 函数将浮点值转换回小数时发生的; SQL Server 中的最大精度限制为 17:

来自 MSDN:

Conversion of float values that use scientific notation to decimal ornumeric is restricted to values of precision 17 digits only. Any valuewith precision higher than 17 rounds to zero.

http://msdn.microsoft.com/en-gb/library/ms173773.aspx

关于c# - SQL Server EXP 到数字的精度转换结果丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com