SQL Power函数对于高数字不正确

时间:2012-10-19 07:39:25

标签: sql-server function

如果我尝试执行此查询:

SELECT POWER(CONVERT(DECIMAL(30,0), 64), 10)

我收到:

1152921504606847000

这是不正确的。正确的结果是:

1152921504606846976

我如何才能收到正确的结果?

3 个答案:

答案 0 :(得分:2)

select cast(power(convert(float(30), 64), 10) as bigint)

答案 1 :(得分:0)

这是由于计算的精确性。您可以拥有非常大的数字,但精度将会关闭,因为您只有固定数量的位来存储您的值......

This is a good explanation of the situation

答案 2 :(得分:0)

如果您想要正确的结果,可以手动计算。

declare @res bigint, @i int
select @res = 64, @i = 1

while @i<10
begin
    select @res = @res * 64, @i=@i+1    
end
select @res