0

The Javascript engine of Google Chrome does something strange with the variable values in this prototype.

The prototype is defined with:

function matrix4def()
{
 this.m00=0;
 this.m01=0;
 this.m02=0;
 this.m03=0;
 this.m10=0;
 this.m11=0;
 this.m12=0;
 this.m13=0;
 this.m20=0;
 this.m21=0;
 this.m22=0;
 this.m23=0;
 this.m30=0;
 this.m31=0;
 this.m32=0;
 this.m33=0;
};

var value = new matrix4def();

console.log(value) shows us:

 m00: 1
 m01: 0
 m02: 0
 m03: 0
 m10: 0
 m11: 1
 m12: 0
 m13: 0
 m20: 0
 m21: 0
 m22: 1
 m23: 0
 m30: 0
 m31: 0
 m32: 0
 m33: 1

While using console.log(value.m00) will show us: -6.123031769111886e-17. Doing console.log(value); console.log(value.m00); console.log(value); shows no difference between the first and second call of console.log(value);.

Doing console.log(typeof(value.m00)) will show us: number

I've tried doing parseFloat(value.m00), but that didn't show 1 either... Storing the value of value.m00 to a temporary value shows me -6.123031769111886e-17 too.

Does anyone have a clue what's happening here?

P.S.: I cannot show you the entire code; it's part of render code that does a lot with these values.

Diamondo25
  • 769
  • 1
  • 8
  • 21

2 Answers2

2

This looks like an issue with console.log, which show you the current property values of an object instead of the ones it had when the object was logged. Do you have any code around (after) the logging that changes the values?

console.log(new matrix4def().m00)

will log 0, not something negative very close to zero.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I'm agree with you, becourse in my demo, it's running well, and dosen't have some strange result – xiaohao Sep 17 '12 at 11:12
  • The `console.log` is ran with the final values of `value`, that will be used to render the objects in WebGL. – Diamondo25 Sep 17 '12 at 11:13
  • Does the WebGL code change the values? Show it to us, create a http://jsfiddle.net for it. – Bergi Sep 17 '12 at 11:17
  • @Bergi Running `console.log(value); console.log(value.m00); console.log(value);` shows me that, internally, `value.m00` is not changed at all, but `console.log(value.m00)` still shows me the `-6.123...` – Diamondo25 Sep 17 '12 at 11:22
0

I suspect you encounter this issue when running your full script (or at least: after performing some operations on the properties of your object). And, given that you're using parseFloat, I'm assuming the values can be decimal (float) values.
If that's the case, you should take a closer look at the known issues with JS and floating point numbers:

either here
or here
maybe here, too
and last but not least: here

Don't forget to look into the articles that are referred to on the last page

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149