Note that there are some explanatory texts on larger screens.

plurals
  1. POIs Javascript multiplying these values wrong?
    primarykey
    data
    text
    <p>Allright, I know what machine precision is, but this, I can't understand...</p> <p>Code:</p> <pre><code> console.log("meanX",meanX); meanX2 = meanX * meanX; //squared console.log("meanX2",meanX2); </code></pre> <p>Console output:</p> <pre><code>meanX 300.3 meanX2 28493.4400000000002 </code></pre> <p>In case you are wondering, the correct value for meanX2 would be <strong>90180.09</strong> And this is only one of the many examples visible in the screenshot..</p> <p><img src="https://i.stack.imgur.com/bgea6.png" alt="See with your own eyes"></p> <p>.toFixed(6) seems to fix this... But I have no idea why it doesn't work without it.</p> <p><strong>Edit</strong></p> <p>Ok, I don't want to post the whole program code here because in first place I'm not the only author, and second, I also wouldn't like this to be copied without our permission. But I'll gladly explain how I get this error and will post the whole method/function code here.</p> <p>This code belongs, as you may have guessed from the window title, to a lane detection algorithm. We use Three.js/webgl to run some pre processing shaders on each frame of a video and then we analyze the resulting image. The method/function you see on the screenshot is a perpendicular line fitting algorithm and is part of the whole thing. I can see the algorithm running nicely because I have the lane being drawn on top of the video, and It is well placed. Until suddenly the lane turns into an horizontal bar. This unexpected behavior happens exactly because of the phenomenon I described here, since it's from that moment that I start to see wrong math in the console.</p> <p>Also, because the video and algorithm run at slightly different fps everytime, the problem doesn't always happen in the same moment of the video, and sometimes It doesn't happen at all.</p> <p>Here is the code (it has some alterations because I was trying to isolate the issue):</p> <pre><code>this.perpendicularLineFit = function (points, slopeSign) { var count = points.length; var sumX = 0, sumY = 0; var sumX2 = 0, sumY2 = 0, sumXY = 0; var meanX, meanY; var i, lowp = {}, highp = {}; var B; var slope; var originY; for (i = 0; i &lt; count; i++) { sumX += points[i].x; sumY += points[i].y; sumX2 += points[i].x * points[i].x; sumY2 += points[i].y * points[i].y; sumXY += points[i].y * points[i].x; } meanX = sumX / count; meanY = sumY / count; //If you uncoment this, problem reappears: //var numeratorLeft = meanY * meanY; console.log("meanX",meanX); var meanX2 = meanX*meanX; console.log("meanX2",meanX2); var numerator = (sumY2 - count * (meanY * meanY)) - (sumX2 - count * meanX2); var denominator = (count * meanX * meanY - sumXY); B = 0.5 * (numerator / denominator); slope = -B + slopeSign * Math.sqrt(B * B + 1); originY = meanY - slope * meanX; slope = isNaN(slope) ? slopeSign : slope; originY = isNaN(originY) ? originY : originY; lowp.y = this.lowY; lowp.x = (this.lowY - originY) / slope; highp.y = this.highY; highp.x = (this.highY - originY) / slope; return { low: lowp, high: highp }; }; </code></pre> <p>Now, I was trying to understand what was causing this, and the most bizarre thing is that it seems that when I place a statement of this form</p> <pre><code>var x = ... meanY * meanY ...; </code></pre> <p>before the meanX2 attribution, the issue happens. Otherwise it doesn't. </p> <p>Also, I tried to catch this anomaly in the debugger but just when I enter the debugging tab, the problem disapears. And the values turn correct again.</p> <p>I certainly don't believe in black magic, and I know that you are probably skeptic to this. I would be too. But here is a link to a <strong>video</strong> showing it happening: <a href="http://www.youtube.com/watch?v=_82MAH6r4Q4" rel="nofollow noreferrer">The video</a></p> <p><strong>Edit2:</strong></p> <p>I managed to reproduce this issue in another computer.. Both having ubuntu and using firefox (versions 20 and 21). </p> <p><strong>Edit3:</strong></p> <p>I'm sorry it took so much time! Here is a zip containing the issue. Just run it in any webserver. The code mentioned is in LaneDetection.js. Search for "HERE" in the file to find it.</p> <p><a href="https://docs.google.com/file/d/0B7y9wWiGlcYnYlo1S2pBelR1cHM/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/file/d/0B7y9wWiGlcYnYlo1S2pBelR1cHM/edit?usp=sharing</a></p> <p>The problem might not happen in the first attempts. If that's the case refresh the page and try again. When the lines get horizontal you know it's there. As I said, I saw this problem happening in firefox versions 20 and 21 on ubuntu. In chrome it never happened.</p> <p>By the way, I noticed that changing javascript.options.typeinference flag in firefox seems to stop the problem... I don't know exactly what that flag does, but maybe this optimization is not correctly implemented in firefox?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload