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

division by zero inconsistent with IEEE 754 standard #156

Open cloudcell opened 1 year ago

cloudcell commented 1 year ago

Assuming that this calculator runs on ganja, I came across this behavior: image which is correct according to IEEE 754. On the other hand, image which is incorrect.

I've done some analysis using ganja coffeeshop to help resolve this bug sooner. Here's a small reproducible example:

// Create a Clifford Algebra with 3,0,1 metric
Algebra(3,0,1,()=>{

    var A = 1e0123;
    var R = 1/(A*(~A)); // results in NaN - not according to IEEE 754.
    var S = 1/R; // the S is zero (empty) in the interface but NaN in the console.
    var T = 1/0; // works according to IEEE 754 (results in Infinity) - ok!

    // print into console
    console.log("R: ", R);
    console.log("S: ", S);
    console.log("T: ", T);

    // Produce a graph in 3D
    document.body.appendChild(this.graph(()=>{
        return [
            0xFF0000, R, "R", // our test point
            0x00FF00, S, "S",
            0x0000FF, T, "T",
        ];
    },{
        grid:true,      // draw a grid
        labels:true,    // draw axis labels
        lineWidth:3     // set the width of the lines connecting the points
    })); // end of graph
}); // end of Algebra

In short, ganja seems to follow IEEE 754 (the standard which describes, among many other things, the treatment of the division by zero) when it deals with real numbers, while it produces NaN's when it deals with zero multivectors. One thing I've noticed is that the coffeeshop interface does not display NaN values which (possibly) makes one assume that the values are all zeroes. So, PERHAPS, the reason we're getting zeroes (in the "calculator example" above) is because NaN's get interpreted as empty values and empty values get interpreted as zeroes later... just a guess. HTH.