我正在尝试使用acos
函数,但是它给我失败了。我试图使代码更容易理解,像这样。
select 10.800887 Latitude, 106.710729 Longitude into #tam
declare @latitude float = 10.800887,
@longitude float = 106.710729
select acos(xx) as result from(
select
sin((@latitude*pi()/180)) * sin((cast(Latitude as float)*pi()/180))+cos((@latitude*pi()/180)) * cos((cast(Latitude as float)*pi()/180)) * cos(((@longitude - cast(Longitude as float))*pi()/180)) as xx
from #tam
) a
drop table #tam
错误
发生无效的浮点运算。
但是当您放置
select acos(1)
该函数的结果为0。第一个代码结果中的acos(xx)有什么问题?
答案 0 :(得分:1)
float类型或隐式类型的表达式 转换为浮点数。仅从-1.00到1.00的值有效。 超出此范围的值将返回NULL,并且ASIN将报告域 错误。
答案 1 :(得分:0)
只需将DECIMAL(9,2)
的有效范围从ACOS
强制转换为-1.00 to 1.00
:
select 10.800887 Latitude, 106.710729 Longitude into #tam
declare @latitude float = 10.800887,
@longitude float = 106.710729
select acos(CAST(xx AS decimal(9,2))) as result from(
select
sin((@latitude*pi()/180)) * sin((cast(Latitude as float)*pi()/180))+cos((@latitude*pi()/180)) * cos((cast(Latitude as float)*pi()/180)) * cos(((@longitude - cast(Longitude as float))*pi()/180)) as xx
from #tam
) a
drop table #tam
即使SELECT
返回0
,数据类型为float
,引擎也可能认为它可能包含比允许的数字更多的数字。