440,630 Members | 1,313 Online Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

# array length problem

 P: n/a 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=heightDiff/dis; var freq= document.getElementById("Freq1").value; wl= .3/freq ; for(s=0;s<=dis;s=s+(dis/8)) { var intervalHeight=(slope*s) + minHeight; var points=wl*s*(dis-s)/dis; fresHi=(intervalHeight + Math.sqrt(points))/10; fresnelHi.push(fresHi); fresLo=(intervalHeight - Math.sqrt(points))/10; fresnelLo.push(fresLo); } alert(fresnelLo.length) Aug 1 '08 #1
3 Replies

 P: n/a 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=heightDiff/dis; var freq= document.getElementById("Freq1").value; wl= .3/freq ; for(s=0;s<=dis;s=s+(dis/8)) { var intervalHeight=(slope*s) + minHeight; var points=wl*s*(dis-s)/dis; fresHi=(intervalHeight + Math.sqrt(points))/10; fresnelHi.push(fresHi); fresLo=(intervalHeight - Math.sqrt(points))/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 Aug 1 '08 #2

 P: n/a In comp.lang.javascript message <21d91a8b-cd30-41cb-89ee-04a9358495d5@d4 5g2000hsc.googlegroups.com>, Fri, 1 Aug 2008 06:31:33, Steve I am having problems with this piece of script. Usually the scriptworks and the alert gives an array length of 9. But every so often thearray length is 8 and naturally the output is then wrong. But seeingas 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=heightDiff/dis; var freq= document.getElementById("Freq1").value; wl= .3/freq ; for(s=0;s<=dis;s=s+(dis/8)) { var intervalHeight=(slope*s) + minHeight; var points=wl*s*(dis-s)/dis; fresHi=(intervalHeight + Math.sqrt(points))/10; fresnelHi.push(fresHi); fresLo=(intervalHeight - Math.sqrt(points))/10; fresnelLo.push(fresLo); }alert(fresnelLo.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(points) . 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 .

 P: n/a On Aug 1, 7:50 pm, Dr J R Stockton

### This discussion thread is closed

Replies have been disabled for this discussion. 