Value NaN of a variable

What does it mean wnen the printed value of a variable is “NaN”?

NaN means “Not a Number”

println(0.0/0.0);
println(sqrt(-1));

why not show the code what produces it?

2 Likes

Lets assume you are talking about a float. In Java a float is stored in 4 bytes of memory that’s 32 bits, since each bit can be 0 or 1 that gives us 4294967296 different combinations to encode a floating point number. Not all those combinations represent a valid floating point number hence Not a Number or NaN

3 Likes

Hi, this is the code that produces NaN:

void setup()
{
size(1000,1000);
background(255);
}
void draw()
{
  float x,y,xm,xM,ym,yM,A,B,inc,Zx,Zy;
  xm = -10; xM = 10; ym = -10; yM = 10;
  inc = (xM-xm)/1000;
  x = xm; y = ym;
  A = (x - xm)/(xM-xm)*1000;
  B = (y-ym)/(yM-ym)*1000;
  println(x,y,inc);
       for (int i = 0; i < 1000; i=i+1)
         {
            for (int k = 0; k < 1000; k++)
              {                    
               for (int l = 0; l < 20 ; l++)
                { println(x,y);
                  Zx = x*x-y*y;
                 Zy = 2*x*y;
           
                 if( Zx*Zx + Zy*Zy < 10)
                  {A = (x - xm)/(xM-xm)*1000;
                   B = (y-ym)/(yM-ym)*1000;  
                   stroke(l);
                   point(A,B);
                  }
                 x = Zx;
                 y = Zy;

               }
            y = ym + k*inc;
           
          }
           x = xm + i*inc;
          }
          exit();
}

that code must be posted in the
</> Preformatted text from the editor menu

Zx = xx-yy;
Zy = 2xy;

this code can not run.

BUT
should it mean:

        Zx = x*x-y*y;
        Zy = 2*x*y;

the code runs and produce your NaN error.


try

  println( "float   min: "+Float.MIN_VALUE+  " max: "+Float.MAX_VALUE);

that tells you the range of possible FLOAT numbers
your loops do
x = Zx = x*x-y*y;
after 7 loops that value is out of range…


and the position you calculate for the points
point(A,B);
is always 0 , 0


sorry but to run that math 20 000 000 times ( points ) is questionable

1 Like

Thank you very much.

It’s obvious that I have some serious misconceptions, is there any book that explains in comprehensvly processing functioning?

please repair your above code posting first.


i asked you to try one line of code to understand the floating point number range
what was the result?
is there a number type that can cover a bigger number range? YES “double”
would it help in your case / math ? i think NOT


yes there are books,
but also

all might depend of YOUR best way of learning.


the problem is that from you code it is not clear
what you want to do?

  • -1- cover the canvas with points and find some nice way to color them?
  • -2- use 20 000 000 points to draw some structure on the canvas ?spiral?..

so for -1-:
show a easy start:

void setup() {
  size(500, 500);
  colorMode(HSB, width, 100, 100);
}

void draw() {
  many_points();
}

void many_points() {
  for ( int x = 0; x < width; x++)
    for ( int y = 0; y < height; y++ ) {
      stroke(x, 100, 100);
      point(x, y);
    }
}

2 Likes

Again, thank you very much!!!