map(NaN)返回NaN,但我无法调试NaN

问题描述 投票:0回答:1

我提供了一个演示我的问题的摘要。基本上处理给我这个错误:

map(NaN, -3, 3, -125, 125) called, which returns NaN (not a number)

我理解此消息的方式是map函数返回NaN,由于它返回浮点数,因此我应该能够使用Float.NaN进行检查。如所示,虽然在创建检查它的if语句时我没有得到任何帮助。我试图将if语句放在带有相应变量但没有命中的map函数之前。我想知道是否有人可以向我解释这种现象并帮助我调试代码。这可能是我所监管的一些小事情,但这让我发疯。在此先感谢

摘要:

void setup() {
  size(500, 500);
}

void draw() {
  background(0);
}

class Complex {
  private float re, im, r, p;

  Complex(float real, float imag) {
    re = real;
    im = imag;
    r = sqrt(sq(re)+sq(im)); //radius 
    p = atan2(im, re);       //phase
  }

  Complex Div(Complex b) {
    Complex a = this;
    float real = (a.re*b.re+a.im*b.im)/(sq(b.re)+sq(b.im));
    float imag = (a.im*b.re-a.re*b.im)/(sq(b.re)+sq(b.im));
    return new Complex(real, imag);
  }

  Complex Ln() {
    float real = log(r);
    float imag = p;
    return new Complex(real, imag);
  }

  Complex LogBase(Complex b) {
    Complex a = this;
    return a.Ln().Div(b.Ln());
  }

  Complex Scale(float scale, int dim) {
    float real = map(re, -scale, scale, -dim, dim);
    float imag = map(im, -scale, scale, -dim, dim);
    if (real == Float.NaN || imag == Float.NaN) {
      print("\nHit!");
    }
    return new Complex(real, imag);
  }
}

void keyPressed() {
  float d = width/4;
  for (float z = -d; z<d; z++) {
    for (float x = -d; x<d; x++) {
      Complex c = new Complex(1, 5);
      c = c.LogBase(new Complex(x, z));
      c.Scale(3.0, int(d));
    }
  }
}
java processing
1个回答
0
投票

似乎误解与NaN的比较有关。与NaN进行的任何等效比较(==)将返回false。即使与自身进行比较也是如此。要检查NaN值,可以使用Float.isNaN方法。

例如,]

    System.out.println("(Float.NaN == Float.NaN) -> " + (Float.NaN == Float.NaN));
    System.out.println("(Float.isNaN(Float.NaN)) -> " + (Float.isNaN(Float.NaN)));

产生:

(Float.NaN == Float.NaN) -> false
(Float.isNaN(Float.NaN)) -> true
© www.soinside.com 2019 - 2024. All rights reserved.