MySQL 错误? (三角学)
我正在通过预先计算表中字段的一些三角函数来优化查询,当我偶然发现:
SELECT 6371 * acos( 0.793521289617132 * 0.793521289617132 + 0.608542490648241 * 0.608542490648241 * cos( 0.235244203230056 - 0.235244203230056 ) )
返回 null
具有非预先计算值的查询:
SELECT 6371 * acos( sin( radians( 52.51581 ) ) * sin( radians( 52.51581 ) ) + cos( radians( 52.51581 ) ) * cos( radians( g.lat ) ) * cos( radians( 13.4785 ) - radians( 13.4785 ) ) )
返回 0 (这是正确的结果)
这是一个错误吗?或者是预期的?
I was optimizing a query by precalculating some trigonometry-funnctions for the fields in a table, when I stumbled on this:
SELECT 6371 * acos( 0.793521289617132 * 0.793521289617132 + 0.608542490648241 * 0.608542490648241 * cos( 0.235244203230056 - 0.235244203230056 ) )
returns null
the query with non-precalculated values:
SELECT 6371 * acos( sin( radians( 52.51581 ) ) * sin( radians( 52.51581 ) ) + cos( radians( 52.51581 ) ) * cos( radians( g.lat ) ) * cos( radians( 13.4785 ) - radians( 13.4785 ) ) )
returns 0 (which is the correct result)
Is this a bug? or is it expected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的查询中存在一些由浮点算术导致的舍入错误。
如果您尝试此查询,
您将得到
6.66133814775094e-016
。所以你想要做的显然是行不通的,因为
acos
仅在 [-1,1] 域上定义。我不知道你到底想完成什么,但你必须重新计算,例如检查 acos 的参数是否超出范围,然后相应地设置值,可能像这样:
You have some rounding errors in your query which result from the float arithmetic.
If you try this query
you'll get
6.66133814775094e-016
. So what you're trying to do iswhich obviously won't work because
acos
is only defined on [-1,1] domain.I don't know what exactly you're trying to accomplish but you have to rework you calculations, e.g. check if the parameter for acos is out of bounds and then set the value accordingly, maybe like this:
如果 X 不在其中,ACOS 返回 NULL范围-1到1
可能是在非预先计算值的情况下,mysql在应用ACOS之前做了一些简化
ACOS returns NULL if X is not in the range -1 to 1
may be in the case with non-precalculated values, mysql is doing some simplification before applying ACOS