# Problem with matrix multiplication

**URL:** <https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420>\
**Category:** Questions\
**Created:** [July 12, 2020, 1:36pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420 "2020-07-12T13:36:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Otto](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/otto/32/2877_2.png) [@Otto](https://forum.babylonjs.com/u/Otto)\
**Post date:** [July 12, 2020, 1:36pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/1 "2020-07-12T13:36:08Z")

</div>

According to math books, when multiplying matrices following is true:

(AB)C=A(BC)

Lets assume I have Matrices A, B and C. I multiply them together with babylonjs math:

const AB= A.multiply(B);  
const ABC=AB.multiply( C );

Because the rule “(AB)C=A(BC)” should the following result same?

const BC= B.multiply( C );  
const ABC=A.multiply(BC)

But I am not getting identical results, where is the problem?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [July 12, 2020, 2:21pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/2 "2020-07-12T14:21:38Z")

</div>

It seems ok to me:

[https://playground.babylonjs.com/#QPGDXH](https://playground.babylonjs.com/#QPGDXH)

Look at the console log.

---

<div class="post-metadata">

**Author:** ![Otto](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/otto/32/2877_2.png) [@Otto](https://forum.babylonjs.com/u/Otto)\
**Post date:** [July 13, 2020, 11:01am UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/3 "2020-07-13T11:01:31Z")

</div>

You are right.  
Because of floating numbers accuracy the results were not completely identical, and I was confused of scientific notation which makes very small numbers to look very different at first glance.

---

<div class="post-metadata">

**Author:** ![Otto](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/otto/32/2877_2.png) [@Otto](https://forum.babylonjs.com/u/Otto)\
**Post date:** [July 13, 2020, 8:37pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/4 "2020-07-13T20:37:19Z")

</div>

My problem was not fully solved so I had to dig deeper. It seems that after I invert matrices, they are no longer similar. Take a look: [https://playground.babylonjs.com/#QPGDXH#1](https://playground.babylonjs.com/#QPGDXH#1)

When inverted, the matrices are totally different, for example the node 13 is on first matrix 13744307.000 but second 24520554.000

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [July 13, 2020, 9:30pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/5 "2020-07-13T21:30:01Z")

</div>

It’s because of a float precision problem.

Matrices store their components as 32 bits float and not 64 bits.

For testing purpose, I have performed some changes on my local copy to have matrices use 64 bits float instead (so Array instead of Float32Array), and it’s much better, even if not exactly equal:

```auto
2.8306351045,0.0000001191,0.0000001154,-0.0000000000,-0.0000005523,0.4227937602,
 0.0000005524,-0.0000000000,-43670253748.5334472656,42674406020.9739761353,
 43208142962.6059951782,-0.5000044421,43670305807.5326919556,-42674456892.8302230835, 
 -43208194469.7262496948,0.5000050381

2.8306352408,-0.0000000142,-0.0000000195,0.0000000000,0.0000000046,0.4227932161,
 0.0000000015,0.0000000000,-43670199647.2177734375,42674353153.3738937378,
 43208089433.7807312012,-0.5000038226,43670251706.1525268555,-42674404025.1671066284,
 -43208140940.8371810913,0.5000044187

```

I don’t see an easy solution here, as I don’t think updating the `Matrix` class to use 64 bits floats is planned… Maybe having an option to switch matrices to 64 bits could do it, but that would mean quite a lot of work to make all code depending on matrices work in both cases…

---

<div class="post-metadata">

**Author:** ![Otto](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/otto/32/2877_2.png) [@Otto](https://forum.babylonjs.com/u/Otto)\
**Post date:** [July 14, 2020, 9:33am UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/6 "2020-07-14T09:33:55Z")

</div>

Thank you.  
I wonder could matrix class be implemented in such way that you could pipe operations without the results being stored to actual underlying buffer until the last operation is finished. Then every calculation would happen with f64 while final results is saved f32

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [July 14, 2020, 2:59pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/7 "2020-07-14T14:59:14Z")

</div>

We are currently discussing with evgeni on a plan for you:) we need to make sure performance are kept unchanged

---

<div class="post-metadata">

**Author:** ![Otto](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/otto/32/2877_2.png) [@Otto](https://forum.babylonjs.com/u/Otto)\
**Post date:** [July 14, 2020, 3:46pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/8 "2020-07-14T15:46:03Z")

</div>

If you solve this, its possible to make picking much faster.  
Currectly, when picking, the babylonjs creates picking ray for each mesh by applying view matrix to model and then applying projection matrix to that.  
If you can calculate multiplication of view and projection just once and then apply it to each mesh world matrix, one expensive matrix multiplication per mesh is saved.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [July 17, 2020, 12:47pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/9 "2020-07-17T12:47:26Z")

</div>

Note that you can now switch matrix computation to 64 bits:

[https://playground.babylonjs.com/#QPGDXH#3](https://playground.babylonjs.com/#QPGDXH#3)

You just have to set the `useHighPrecisionMatrix` parameter to true when creating the engine:

```javascript
var createEngine = () => {
    return new BABYLON.Engine(canvas, false, {
        useHighPrecisionMatrix: true
    });
};

```

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [July 17, 2020, 5:34pm UTC](https://forum.babylonjs.com/t/problem-with-matrix-multiplication/12420/10 "2020-07-17T17:34:55Z")

</div>

Thanks @Evgeni_Popov!
