参数类型’double’无法赋值给参数类型’double’。

huangapple go评论83阅读模式
英文:

The argument type 'double' can't be assigned to the parameter type 'double'

问题

我有一个以 double 作为输入的函数,并且我传递了一个 double 值。然后我收到了错误消息:

参数类型 'double' 无法分配给参数类型 'double'

for (var i = 0; i < found.length; i++) {
  final double f = found[i];

  if (_aboveTreshold(f)) {
    ....
  }
}

而方法的签名非常简单。

bool _aboveTreshold(double value) {

...
}

真的不可能将 double 作为参数吗?还是我做错了什么?

这对我来说是一个意外的错误消息。

为什么 'double' 不应该是 'double'?

所以作为一种解决方法,我将函数定义为泛型,然后将值强制转换回 double。这是一个不太优雅的解决方法。有更好的方法吗?

bool _aboveTreshold<T>(T value) {
  final v = value as double;

  ....
}

编辑

这里有更多来自 TFLite 示例的代码,以了解 'found' 从哪里来。它是 'List'。

final interpreter = await _loadModel('assets/tf_lite_models/mobilenet_v1_1.0_128_1_default_1.tflite');
final imageMatrix = _buildImageMatrix(imageBytes, 128, 128);

final input = [imageMatrix];
final output = [List<double>.filled(1001, 0)];

interpreter.run(input, output);

var foundedLabels = _runClassification(output.first).keys.join(',');

// 方法:

Map<String, double> _runClassification<double>(List<double> found) {
    final classification = <String, double>{};

    for (int i = 0; i < found.length; i++) {

      if (_aboveTreshold(found[i])) {
        classification[_labels[i]] = found[i];
      }
    }      

    return classification;
  }
}

在开发环境中,该值也被显示为 List

英文:

I have a function that takes double as input and I'm passing a double value. I then get the error message:

The argument type 'double' can't be assigned to the parameter type 'double'

for (var i = 0; i &lt; found.length; i++) {
  final double f = found[i];

  if (_aboveTreshold(f)) {
    ....
  }
}

And the method of signature is quite simple.

bool _aboveTreshold(double value) {

...
}

Is it really not possible to have a double as a parameter or am I doing something wrong?

This is an unexpected error message for me.

Why shouldn't 'double' be 'double'?

So as a workaround I made the function generic and then cast the value back to double. This is a dirty workaround. Is there a better way?

bool _aboveTreshold&lt;T&gt;(T value) {
  final v = value as double;

  ....
}

EDIT

Here's more code from the TFLite example to understand where 'found' comes from. It's 'List<double>' that I get.

final interpreter = await _loadModel(&#39;assets/tf_lite_models/mobilenet_v1_1.0_128_1_default_1.tflite&#39;);
final imageMatrix = _buildImageMatrix(imageBytes, 128, 128);

final input = [imageMatrix];
final output = [List&lt;double&gt;.filled(1001, 0)];

interpreter.run(input, output);

var foundedLabels = _runClassification(output.first).keys.join(&#39;,&#39;);

the method:

 Map&lt;String, double&gt; _runClassification&lt;double&gt;(List&lt;double&gt; found) {
    final classification = &lt;String, double&gt;{};

    for (int i = 0; i &lt; found.length; i++) {

      if (_aboveTreshold(found[i])) {
        classification[_labels[i]] = found[i];
      }
    }      

    return classification;
  }
}

In the development environment, the value is also displayed as List<double>.

参数类型’double’无法赋值给参数类型’double’。

答案1

得分: 1

我现在有一个相当奇怪的解决方案,但它有效,所以我不再需要变通方法了。如果你也遇到这个奇怪的问题,希望它能有所帮助。

原始的if条件不起作用。我无法比较两个双精度数。然后我将if条件外包给一个函数,上面描述的错误就出现了。为了解决这个问题,我将这个函数变成了通用函数。

但经过一些来回调整后,我成功修复了所有的错误。

首先,我移除了'_aboveTreshold'函数。现在原始的错误又回来了,我无法将found[i]与一个双精度数进行比较。

if (found[i] >= 0.9)

为了解决这个问题,我再次使用了通用方法的技巧。我将_runClassification方法改为了通用方法。

Map<String, T> _runClassification<T>(List<T> found) {
    final classification = <String, T>{};

    for (int i = 0; i < found.length; i++) {

        if ((found[i] as double) >= 0.900) {
            classification[_labels[i]] = found[i];
        }
    }      

    return classification;
}

之后,原则上错误消息再次消失。然后保存并使用调试器。

最后,我也移除了第二个变通方法,现在它可以正常运行而无需任何变通方法。经过几次尝试才成功。

Map<String, double> _runClassification(List<double> found) {
    final classification = <String, double>{};

    for (int i = 0; i < found.length; i++) {
        if (found[i] >= 0.9) {
            classification[_labels[i]] = found[i];
        }
    }

    return classification;
}

我希望这对那些无缘无故遇到这个问题的人有所帮助。

英文:

I now have a solution which is pretty weird, but it worked so I don't need a workaround anymore. If you have this weird problem too, I hope it helps.

The original if condition didn't work. I couldn't compare two doubles. Then I outsourced the if condition to a function and the error described above came up. As a workaround I made this function generic.

But after a bit of tweaking back and forth, I was able to fix all the bugs.

First, I removed the '_aboveTreshold' function. Now the original error came back that I can't compare found[i] with a double.

if (found[i] &gt;= 0.9)

To fix that, I did the generic methods trick again. I turned _runClassification into a generic method.

 Map&lt;String, T&gt; _runClassification&lt;T&gt;(List&lt;T&gt; found) {
    final classification = &lt;String, T&gt;{};

    for (int i = 0; i &lt; found.length; i++) {

      if ((found[i] as double) &gt;= 0.900) {
        classification[_labels[i]] = found[i];
      }
    }      

    return classification;
  }
}

After that, the error messages were gone again in principle. Then save again and use the debugger.

In the end I also removed the second workaround and now it works without workarounds. It took a few tries.

  Map&lt;String, double&gt; _runClassification(List&lt;double&gt; found) {
    final classification = &lt;String, double&gt;{};

    for (int i = 0; i &lt; found.length; i++) {
      if (found[i] &gt;= 0.9) {
        classification[_labels[i]] = found[i];
      }
    }

    return classification;
  }

I hope this helps anyone who has this for no reason.

huangapple
  • 本文由 发表于 2023年6月5日 15:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定