将精度与 Float.MAX_VALUE 进行比较意味着什么?

发布于 2024-11-18 02:46:56 字数 1116 浏览 4 评论 0原文

在阅读 有关位置的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-11-25 02:46:56

如果您按照他的链接访问整个源文件,那么该特定位会更有意义。这是一个稍微大一点的代码片段:

Location bestResult = null;
float bestAccuracy = Float.MAX_VALUE;
long bestTime = Long.MIN_VALUE;

// Iterate through all the providers on the system, keeping
// note of the most accurate result within the acceptable time limit.
// If no result is found within maxTime, return the newest Location.
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;
    }
  }
}

非常简单,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:

Location bestResult = null;
float bestAccuracy = Float.MAX_VALUE;
long bestTime = Long.MIN_VALUE;

// Iterate through all the providers on the system, keeping
// note of the most accurate result within the acceptable time limit.
// If no result is found within maxTime, return the newest Location.
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;
    }
  }
}

Very simply, Float.MAX_VALUE is his default value for bestAccuracy and he's just checking that he hasn't reduced it in the previous if clause.

绿光 2024-11-25 02:46:56

我猜测 bestAccuracy 已初始化为 Float.MAX_VALUE。如果是这样,代码看起来可以总结为:找到精度最小(最好?)且时间大于 minTime 的提供者。如果没有大于minTime的时间,则取最接近minTime的时间。

可以将其重构

    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;
    }

    if ((time < minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
      foundWithinTimeLimit = true;
    }
    else if (time > minTime && !foundWithinTimeLimit && time < bestTime) {
      bestResult = location;
      bestTime = time;
    }

使其更加清晰。

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

    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;
    }

to

    if ((time < minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
      foundWithinTimeLimit = true;
    }
    else if (time > minTime && !foundWithinTimeLimit && time < bestTime) {
      bestResult = location;
      bestTime = time;
    }

makes it a little bit clearer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文