array length problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    array length problem

    I am having problems with this piece of script. Usually the script
    works and the alert gives an array length of 9. But every so often the
    array length is 8 and naturally the output is then wrong. But seeing
    as I am using dis/8 in the loop how can the array length be 8?

    var dis = myLine.Distance ();
    var fresnelHi=[];
    var fresnelLo=[];
    var heightDiff;
    var minHeight;
    var slope=heightDif f/dis;
    var freq= document.getEle mentById("Freq1 ").value;
    wl= .3/freq ;
    for(s=0;s<=dis; s=s+(dis/8)) {
    var intervalHeight= (slope*s) + minHeight;
    var points=wl*s*(di s-s)/dis;
    fresHi=(interva lHeight + Math.sqrt(point s))/10;
    fresnelHi.push( fresHi);
    fresLo=(interva lHeight - Math.sqrt(point s))/10;
    fresnelLo.push( fresLo);
    }
    alert(fresnelLo .length)
  • Gregor Kofler

    #2
    Re: array length problem

    Steve meinte:
    I am having problems with this piece of script. Usually the script
    works and the alert gives an array length of 9. But every so often the
    array length is 8 and naturally the output is then wrong. But seeing
    as I am using dis/8 in the loop how can the array length be 8?
    >
    var dis = myLine.Distance ();
    var fresnelHi=[];
    var fresnelLo=[];
    var heightDiff;
    var minHeight;
    var slope=heightDif f/dis;
    var freq= document.getEle mentById("Freq1 ").value;
    wl= .3/freq ;
    for(s=0;s<=dis; s=s+(dis/8)) {
    var intervalHeight= (slope*s) + minHeight;
    var points=wl*s*(di s-s)/dis;
    fresHi=(interva lHeight + Math.sqrt(point s))/10;
    fresnelHi.push( fresHi);
    fresLo=(interva lHeight - Math.sqrt(point s))/10;
    fresnelLo.push( fresLo);
    }
    alert(fresnelLo .length)
    A rounding error I suppose.

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum

    Comment

    • Dr J R Stockton

      #3
      Re: array length problem

      In comp.lang.javas cript message <21d91a8b-cd30-41cb-89ee-04a9358495d5@d4
      5g2000hsc.googl egroups.com>, Fri, 1 Aug 2008 06:31:33, Steve
      <stephen.joung@ googlemail.comp osted:
      >I am having problems with this piece of script. Usually the script
      >works and the alert gives an array length of 9. But every so often the
      >array length is 8 and naturally the output is then wrong. But seeing
      >as I am using dis/8 in the loop how can the array length be 8?
      >
      >var dis = myLine.Distance ();
      >var fresnelHi=[];
      >var fresnelLo=[];
      >var heightDiff;
      >var minHeight;
      >var slope=heightDif f/dis;
      var freq= document.getEle mentById("Freq1 ").value;
      wl= .3/freq ;
      for(s=0;s<=dis; s=s+(dis/8)) {
      var intervalHeight= (slope*s) + minHeight;
      var points=wl*s*(di s-s)/dis;
      fresHi=(interva lHeight + Math.sqrt(point s))/10;
      fresnelHi.push( fresHi);
      fresLo=(interva lHeight - Math.sqrt(point s))/10;
      fresnelLo.push( fresLo);
      }
      >alert(fresnelL o.length)
      Variable dis will be represented as an IEEE Double, with 53 bit
      resolution. The value of dis/8 will be similar, but with exponent three
      smaller. When you expect s to become 3*dis/8, an extra bit of
      resolution will be wanted but not available, and s may be rounded up.
      The same possibility can occur in later additions. It will vary
      according to the exact value of dis.

      Use for (X=0 ; X<=8 ; X++) { s = X*dis/8 // or similar.

      Always expect rounding errors when using non-integers, except sometimes.

      Remove repeated calculation from loops - dis/8, Math.sqrt(point s) .

      And space your code, for legibility.

      It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

      --
      (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
      news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
      <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • Steve

        #4
        Re: array length problem

        On Aug 1, 7:50 pm, Dr J R Stockton <j...@merlyn.de mon.co.ukwrote:
        On Aug 1, 8:29 pm, Gregor Kofler <use...@gregork ofler.atwrote:

        Thank you both.

        Steve.


        Comment

        Working...