I need to calculate the orbit of the Sun around the Galaxy. The mathematical formula I am using is ((241828072282107.5071453596951 * 666) * 2) * 3.14159265359. In QML JavaScript, I received the answer 1011954093357316100, but the correct answer is 1011954093357316200, off by 100 units.
galaxyRadius="241828072282107.5071453596951";
currentTrackNumber=666; // Track Number like on a Record
pIe="3.14159265359"; // not the same as Math.PI
I had to keep the numbers as strings because converting them to floats would lose precision. Converting an old bash script to JavaScript, I realized it worked fine with bc but not with Math.
I tried the following:
orbitDist = ((( Number.parseFloat(galaxyRadius).toPrecision(20) * currentTrackNumber) * 2) * Number.parseFloat(pIe).toPrecision(12) );
The results are the same as:
orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
Compared to the bash result:
echo "$(bc <<< "scale=13;((241828072282107.5071453596951 * 666) * 2) * 3.14159265359")"
Bash is accurate while JavaScript is inaccurate by almost 100 units. This discrepancy is concerning, especially since I have many numbers that are slightly off. A 100-unit difference is not acceptable.
I prefer to work with integers rather than exponents. The values are stored in a database as strings, so I just need the math to be precise.
This is a QtQuick, QML, Felgo App using Qml and JavaScript, designed to run on various platforms. My next step is to consider C++ or a math library that suits this project. A JavaScript or QML wrapper library for C++ would be ideal. I have researched JavaScript libraries for the web, but they require extensive modifications for Qml. I am looking for a solution that works seamlessly.