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

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

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

问题

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

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

  1. for (var i = 0; i < found.length; i++) {
  2. final double f = found[i];
  3. if (_aboveTreshold(f)) {
  4. ....
  5. }
  6. }

而方法的签名非常简单。

  1. bool _aboveTreshold(double value) {
  2. ...
  3. }

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

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

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

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

  1. bool _aboveTreshold<T>(T value) {
  2. final v = value as double;
  3. ....
  4. }

编辑

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

  1. final interpreter = await _loadModel('assets/tf_lite_models/mobilenet_v1_1.0_128_1_default_1.tflite');
  2. final imageMatrix = _buildImageMatrix(imageBytes, 128, 128);
  3. final input = [imageMatrix];
  4. final output = [List<double>.filled(1001, 0)];
  5. interpreter.run(input, output);
  6. var foundedLabels = _runClassification(output.first).keys.join(',');
  7. // 方法:
  8. Map<String, double> _runClassification<double>(List<double> found) {
  9. final classification = <String, double>{};
  10. for (int i = 0; i < found.length; i++) {
  11. if (_aboveTreshold(found[i])) {
  12. classification[_labels[i]] = found[i];
  13. }
  14. }
  15. return classification;
  16. }
  17. }

在开发环境中,该值也被显示为 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'

  1. for (var i = 0; i &lt; found.length; i++) {
  2. final double f = found[i];
  3. if (_aboveTreshold(f)) {
  4. ....
  5. }
  6. }

And the method of signature is quite simple.

  1. bool _aboveTreshold(double value) {
  2. ...
  3. }

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?

  1. bool _aboveTreshold&lt;T&gt;(T value) {
  2. final v = value as double;
  3. ....
  4. }

EDIT

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

  1. final interpreter = await _loadModel(&#39;assets/tf_lite_models/mobilenet_v1_1.0_128_1_default_1.tflite&#39;);
  2. final imageMatrix = _buildImageMatrix(imageBytes, 128, 128);
  3. final input = [imageMatrix];
  4. final output = [List&lt;double&gt;.filled(1001, 0)];
  5. interpreter.run(input, output);
  6. var foundedLabels = _runClassification(output.first).keys.join(&#39;,&#39;);

the method:

  1. Map&lt;String, double&gt; _runClassification&lt;double&gt;(List&lt;double&gt; found) {
  2. final classification = &lt;String, double&gt;{};
  3. for (int i = 0; i &lt; found.length; i++) {
  4. if (_aboveTreshold(found[i])) {
  5. classification[_labels[i]] = found[i];
  6. }
  7. }
  8. return classification;
  9. }
  10. }

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

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

答案1

得分: 1

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

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

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

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

  1. if (found[i] >= 0.9)

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

  1. Map<String, T> _runClassification<T>(List<T> found) {
  2. final classification = <String, T>{};
  3. for (int i = 0; i < found.length; i++) {
  4. if ((found[i] as double) >= 0.900) {
  5. classification[_labels[i]] = found[i];
  6. }
  7. }
  8. return classification;
  9. }

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

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

  1. Map<String, double> _runClassification(List<double> found) {
  2. final classification = <String, double>{};
  3. for (int i = 0; i < found.length; i++) {
  4. if (found[i] >= 0.9) {
  5. classification[_labels[i]] = found[i];
  6. }
  7. }
  8. return classification;
  9. }

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

英文:

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.

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

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

  1. Map&lt;String, T&gt; _runClassification&lt;T&gt;(List&lt;T&gt; found) {
  2. final classification = &lt;String, T&gt;{};
  3. for (int i = 0; i &lt; found.length; i++) {
  4. if ((found[i] as double) &gt;= 0.900) {
  5. classification[_labels[i]] = found[i];
  6. }
  7. }
  8. return classification;
  9. }
  10. }

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.

  1. Map&lt;String, double&gt; _runClassification(List&lt;double&gt; found) {
  2. final classification = &lt;String, double&gt;{};
  3. for (int i = 0; i &lt; found.length; i++) {
  4. if (found[i] &gt;= 0.9) {
  5. classification[_labels[i]] = found[i];
  6. }
  7. }
  8. return classification;
  9. }

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:

确定