Why would drawPoint(x,y) give "Invalid Value"?

drawPoint(x,y) in the code below is infrequently giving error "Error: Invalid Value
Details: 'Failed invoking <symbol>'".
Any thoughts as to why?
This isn't happening very often - in fact, only once so far, on a real device.
The only thought I've had so far is perhaps x or y was NaN or Inf somehow.  I supposed A or B could possibly by NaN or Inf (shouldn't be, but could) and if so, that would descend to x & y.
Any thoughts welcome!
CODE -------------------------------------------


//type = :low or :high; swidth & sheight are the screen width & height of myDc
// xc, yc are the center points of myDc
drawFuzzyEllipse (myDc, swidth, sheight, xc, yc, A, B, type) {


      if (A> swidth && B > sheight) { return; }

      var step =  A/2.0;  

      var start = 0;

      if (type == :low) {
step = (Math.rand()%150)/100.0 + 0.5;
start = (Math.rand()%314).toFloat();
}

      if (step<25 && A > 3  && type == :high) { step = 25; }

      for (var theta = start; theta < 2 * Math.PI;theta += Math.PI * 2.0 / step) {

         var adder = 0;
         if (type == :low) {adder = Math.rand()%1000/1000.0 * Math.PI * 2;

         var ran = Math.rand()%1000;
         var addme = 0.95f + ran/10000000.0f;

         var x = xc + (A*addme) * Math.cos(theta);
         var y = yc + (B*addme) * Math.sin(theta);

       

CODE -------------------------------------------
  • And, here is some further insight:

    Actually the error is NOT happening at the line with myDc.drawPoint(x,y);

    The first iteration of the loop completed successfully - and even drew the point, though I couldn't see it because the Q! covered it immediate - because it is simply using the initial value of theta, which is zero. No problem at all for that initial step of the loop.

    Where the error happens is when it returns to the top of the loop and increments theta.  That is theta += Math.PI * 2.0 / step and step = 0 so we get an error.

    For whatever reason, the line with the loop initiator is not recognized as a line we are going to during execution of the loop, so the last line of the loop - whatever that happens to be - is the one listed in the error message.

    So that line number is misleading and the actual error is happening in this line:

    • for (var theta = start; theta < 2 * Math.PI;theta += Math.PI * 2.0 / step)

    That is why no amount of testing of x & y, or printing them out, and so on, was helpful at all.

    TL;DR is probably: If an error shows up on the last line of a loop (or similar construction, like while) also inspect the loop initiator line for possible errors.