enkimute / ganja.js

:triangular_ruler: Javascript Geometric Algebra Generator for Javascript, c++, c#, rust, python. (with operator overloading and algebraic literals) -
MIT License
1.52k stars 107 forks source link

1e1 * 1e2 !== 1e12 #92

Open OliverEvans96 opened 4 years ago

OliverEvans96 commented 4 years ago

Hello,

I'm just beginning to learn how to use ganja.js, and I was playing around in the browser console and noticed the following:

Algebra(2, ()=>{
    console.log('(1) ==? ', 1e1*1e2==1e12)
    console.log('(2) ===? ', 1e1*1e2===1e12)
    console.log('(3) String(1e1*1e2) =', String(1e1*1e2))
    console.log('(4) String(1e12) =', String(1e12))
    console.log('(5) `${1e1*1e2}` =', `${1e1*1e2}`)
    console.log('(6) `${1e12}` =', `${1e12}`)
});

produces:

(1) ==?  true
(2) ===?  false
(3) String(1e1*1e2) = e_12
(4) String(1e12) = e_12
(5) `${1e1*1e2}` = 0
(6) `${1e12}` = e_12

I'm surprised by (2) and (5) - these are seemingly minor details, but I wonder if anyone could explain whether they are to be expected, or are overlooked implementation details.

In @chakravala's Julia implementation, Grassmann.jl, we do, in fact, have v1*v2 === v12, which leads me to expect it here as well.

Again, thanks for the really cool software, and I hope you'll excuse my ignorance :)

Cheers, Oliver

ted-dgk commented 3 years ago

This caught my interest so I looked into it. Can't say I'm completely right but here goes. == is overloaded by ganja. === is not. Since they both do ref checks on objects, you, as stated, would expect them to be the same. But if the overload of == finds the types it is interested in, it will do an element by element comparison and if they are all the same return true. You can search ganja.js source code for eq to see the function (which is mapped to ==). Obviously ganja is a learning tool - and a great one at that - so not necc. going to cover every single base. I used the browser debugger to see all this.

Thanks to ganja creator for it and the incredible GAME2020 video. Opened my eyes more than anything else.

enkimute commented 3 years ago

Sorry for being late to the party - Ted's analysis is entirely accurate. (and thanks for appreciating the lecture!). While it would not be difficult to add support for the tripple equal operator I'm unsure what the expected behavior should be. The only case that comes to mind where it would differ from the double equal is when comparing a scalar with a multivector-containing-just-a-scalar-coefficient - am I missing something?