/    Sign up×
Community /Pin to ProfileBookmark

|= operator strangeness

Hello,

I’ve written a little program that solves 3 equations with 3 unknowns using Cramer’s rule. I’m perplexed that the |= operator seems to increment one of its arguments. I’m seeing this in Opera and Chrome.

Also, I thought there was a |== operator that wouldn’t try to do type coercion, similar to the === operator. However, if I replace my |= operators with |== both Opera and Chrome choke, is |== only a legend.

Here’s my entire source code:

[CODE]
<html>
<head>
<title>Solve 3 Equations with 3 Unknowns using Cramer’s Rule</title>
<script>
/*
matrix[0][0]*x+matrix[1][0]*y+matrix[2][0]=A
matrix[0][1]*x+matrix[1][1]*y+matrix[2][1]=B
matrix[0][2]*x+matrix[1][2]*y+matrix[2][2]=B
*/

var inputMatrix3x4 = [];
inputMatrix3x4[0]=[];
inputMatrix3x4[1]=[];
inputMatrix3x4[2]=[];
inputMatrix3x4[3]=[];
var xMatrix = [];
xMatrix[0]=[];
xMatrix[1]=[];
xMatrix[2]=[];
var yMatrix = [];
yMatrix[0]=[];
yMatrix[1]=[];
yMatrix[2]=[];
var zMatrix = [];
zMatrix[0]=[];
zMatrix[1]=[];
zMatrix[2]=[];
var bottom = [];
bottom[0]=[];
bottom[1]=[];
bottom[2]=[];
var x;
var y;
var z;

alert(“hello”);

/*
for (var i=0;i<=3;i++) {
for (var j=0;j<=2;j++) {
if (i<3) {
inputMatrix3x4[i][j] = prompt(“Enter the matrix for i = ” + i + ” and j = ” + j + “.”,””);
}
else {
inputMatrix3x4[i][j] = prompt(“Enter the answer for row j = ” + j + “.”,””);
}
}
}

*/

inputMatrix3x4[0][0]=2;
inputMatrix3x4[1][0]=3;inputMatrix3x4[2][0]=1;inputMatrix3x4[3][0]=10;
inputMatrix3x4[0][1]=1;inputMatrix3x4[1][1]=-1;inputMatrix3x4[2][1]=1;inputMatrix3x4[3][1]=4;
inputMatrix3x4[0][2]=4;inputMatrix3x4[1][2]=-1;inputMatrix3x4[2][2]=-5;inputMatrix3x4[3][2]=-8;

bottom = replaceWithAnswers(3,inputMatrix3x4);

//alert3x3Matrix(bottom);

xMatrix=replaceWithAnswers(0,inputMatrix3x4);

//alert3x3Matrix(xMatrix);

yMatrix=replaceWithAnswers(1,inputMatrix3x4);

//alert3x3Matrix(yMatrix);

zMatrix=replaceWithAnswers(2,inputMatrix3x4);

//alert3x3Matrix(zMatrix);

x=get3x3Determinant(xMatrix)/get3x3Determinant(bottom);
y=get3x3Determinant(yMatrix)/get3x3Determinant(bottom);
z=get3x3Determinant(zMatrix)/get3x3Determinant(bottom);

alert(“x = ” + x);
alert(“y = ” + y);
alert(“z = ” + z);

function alert3x3Matrix(my3x3Matrix) {
for (var i=0;i<=2;i++) {
for (var j=0;j<=2;j++) {
alert(“matrix (” + i + “, ” + j + ” ) = ” + my3x3Matrix[i][j]);
}
}
}

function replaceWithAnswers(col,my3x4Matrix) {
var my3x3Matrix = [];
my3x3Matrix[0]=[];
my3x3Matrix[1]=[];
my3x3Matrix[2]=[];
for (var i=0;i<=2;i++) {
for (var j=0;j<=2;j++) {
if (i===col) {
//Replace it with the answer
my3x3Matrix[i][j] = my3x4Matrix[3][j];
}
else {
//Just add it to the 3x3matrix
my3x3Matrix[i][j] = my3x4Matrix[i][j];
}
}
}
return my3x3Matrix;
}

function get3x3Determinant(matrix) {
/*
matrix[0][0] matrix[1][0] matrix[2][0]
matrix[0][1] matrix[1][1] matrix[2][1]
matrix[0][2] matrix[1][2] matrix[2][2]

| + – + |
| – + – |
| + – + |

*/
document.write(“<table>”)
document.write(“<tr>”)
document.write(“<td>” + matrix[0][0] + “</td><td>” + matrix[1][0] + “</td><td>” + matrix[2][0] + “</td>”)
document.write(“</tr>”)
document.write(“<tr>”)
document.write(“<td>” + matrix[0][1] + “</td><td>” + matrix[1][1] + “</td><td>” + matrix[2][1] + “</td>”)
document.write(“</tr>”)
document.write(“<tr>”)
document.write(“<td>” + matrix[0][2] + “</td><td>” + matrix[1][2] + “</td><td>” + matrix[2][2] + “</td>”)
document.write(“</tr>”)
document.write(“</table>”)

//Return the determinant
return matrix[0][0]*get2x2Determinant(get2x2Matrix(0,0,matrix))
-matrix[1][0]*get2x2Determinant(get2x2Matrix(1,0,matrix))
+matrix[2][0]*get2x2Determinant(get2x2Matrix(2,0,matrix));

}

function get2x2Matrix(x,y,matrix3x3) {
//Get the 2×2 matrix by “covering up” the row and column
var ii=0; //x-axis for the 2×2 matrix you pass back
var matrix2x2 = []; //2×2 matrix you pass back
matrix2x2[0]=[];
matrix2x2[1]=[];
for (var i=0;i<=2;i++) {
document.write(“before i|=x, i = ” + i + “<br/>”);
if (i|=x) {
document.write(“after i|=x, i = ” + i + “<br/>”);
//You’re not in the column you’re covering up
var jj=0; //y-axis for the 2×2 matrix you pass back
for (var j=0;j<=2;j++) {
if( j |= y) {
//You’re not in the row you’re covering up, add this one to the 2×2 matrix
matrix2x2[ii][jj]=matrix3x3[i][j];
jj++; //Increment the y-axis counter for the 2×2 matrix
}
}
ii++; //Increment the y-axis counter for the 2×2 matrix
}
}
return matrix2x2; //Return the 2×2 matrix
}

function get2x2Determinant(matrix2x2) {
/*
matrix[0][0] matrix[1][0]
matrix[0][1] matrix[1][1]
*/
//Return the determinant;
return matrix2x2[0][0]*matrix2x2[1][1]-matrix2x2[0][1]*matrix2x2[1][0];

}

</script>
</head>
<body>
<h1>Solve 3 Equations with 3 Unknowns using Cramer’s Rule</h1>
</body>
</html>
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJan 09.2011 — Are you sure that the operator in question is "|==" and is not "!==" ?

I have never encountered that particular operator and cannot locate it with google search.
Copy linkTweet thisAlerts:
@tracknutJan 09.2011 — I'm no JS coder, but to me, "if (i|=x)" means "bitwise or i and x together, assign the result into i, and then test to see if i is non-zero. I suspect that's not what you intended...

Dave
Copy linkTweet thisAlerts:
@ewgoforthauthorJan 09.2011 — Doh, put in a pipe when I meant an exclamation point.

-Eric
Copy linkTweet thisAlerts:
@JMRKERJan 09.2011 — Doh, put in a pipe when I meant an exclamation point.

-Eric[/QUOTE]


Works a lot better that way, does it not? ?
×

Success!

Help @ewgoforth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...