亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

map(NaN) 返回 NaN 但我無法調試 NaN

map(NaN) 返回 NaN 但我無法調試 NaN

狐的傳說 2024-01-05 19:57:16
我已經包含了一個演示我的問題的片段?;旧咸幚斫o了我這個錯誤: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));    }  }}
查看完整描述

1 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

看來誤解是圍繞與 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


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號