将精度与 Float.MAX_VALUE 进行比较意味着什么?
在阅读 有关位置的 Android 开发者博客文章时, 我遇到了这段代码(从博客中剪切和粘贴):
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
bestResult = location;
bestTime = time;
}
}
}
虽然其余部分非常清楚,但这一行让我难住了:
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
“时间”必须在可接受的延迟期内&比之前的 bestTime 更新。这是有道理的。
但是 bestAccuracy 与 Max Float 值的比较意味着什么?什么时候精度会完全等于浮点数可以容纳的最大值?
While reading the Android developers blog post on location,
I came across this bit of code (cut & paste from the blog):
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
bestResult = location;
bestTime = time;
}
}
}
While the rest is pretty clear, this line has me stumped:
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
'time' has to be within the acceptable latency period & more recent than the previous bestTime. That makes sense.
But what does the comparison of bestAccuracy to Max Float value mean? When would the accuracy be exactly equal to the maximum value a float can hold?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您按照他的链接访问整个源文件,那么该特定位会更有意义。这是一个稍微大一点的代码片段:
非常简单,
Float.MAX_VALUE
是他的bestAccuracy
的默认值,他只是检查他在前面的if 中是否没有减少它
子句。That particular bit makes more sense if you follow his link to the whole source file. Here's a slightly bigger snippet:
Very simply,
Float.MAX_VALUE
is his default value forbestAccuracy
and he's just checking that he hasn't reduced it in the previousif
clause.我猜测
bestAccuracy
已初始化为 Float.MAX_VALUE。如果是这样,代码看起来可以总结为:找到精度最小(最好?)且时间大于 minTime 的提供者。如果没有大于minTime的时间,则取最接近minTime的时间。可以将其重构
为
使其更加清晰。
I'm guessing that
bestAccuracy
has been initialized to Float.MAX_VALUE. If so it looks like the code can be summarized as: find the provider with the smallest (best?) accuracy with time greater than minTime. If there is no time greater than minTime, then just take the one with the time closest to minTime.This could be refactored from
to
makes it a little bit clearer.