How do you do large number math in QtQuick Qml using JavaScriptValidate decimal numbers in JavaScript - IsNumeric()How to validate an email address in JavaScriptHow do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I check if an array includes an object in JavaScript?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Has J.J.Jameson ever found out that Peter Parker is Spider-Man?

Current in only inductive AC circuit

In MTG, was there ever a five-color deck that worked well?

How was the cosmonaut of the Soviet moon mission supposed to get back in the return vehicle?

Does WSL2 runs Linux in a virtual machine or alongside windows Kernel?

What is Albrecht Dürer's Perspective Machine drawing style?

Different answers of calculations in LuaLaTeX on local computer, lua compiler and on overleaf

Why is the Vasa Museum in Stockholm so Popular?

Can an unintentional murderer leave Ir Miklat for Shalosh Regalim?

Accurately recalling the key - can everyone do it?

Phase portrait of a system of differential equations

How do I safety check that there is no light in Darkroom / Darkbag?

Is this popular optical illusion made of a grey-scale image with coloured lines?

What is the reason behind water not falling from a bucket at the top of loop?

Why does Shift-right says it is bound to right?

Is law enforcement responsible for damages made by a search warrant?

Is it moral to remove/hide certain parts of a photo, as a photographer?

How to avoid a lengthy conversation with someone from the neighborhood I don't share interests with

How to understand "...to hide the evidence of mishandled magic, or else hidden by castle-proud house-elves" in this sentence

What is the most 'environmentally friendly' way to learn to fly?

Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?

How long should I wait to plug in my refrigerator after unplugging it?

How can I perform a deterministic physics simulation?

Is an "are" omitted in this sentence



How do you do large number math in QtQuick Qml using JavaScript


Validate decimal numbers in JavaScript - IsNumeric()How to validate an email address in JavaScriptHow do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I check if an array includes an object in JavaScript?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I want to calculate the circumstance of the Sun around the Galaxy; the Math Formula is ((241828072282107.5071453596951 * 666) * 2) * 3.14159265359, using QML JavaScript I get the answer 1011954093357316100, when the correct answer is 1011954093357316200, a 100 miles off.



galaxyRadius="241828072282107.5071453596951";

currentTrackNumber=666; // Track Number like on a Record

pIe="3.14159265359"; // not the same as Math.PI


I had to use strings because converting these sized numbers to floats truncate the precision, I am converting an old bash script, and it worked fine with bc, not so much Math.



I have tried this:



orbitDist = ((( Number.parseFloat(galaxyRadius).toPrecision(20) * currentTrackNumber) * 2) * Number.parseFloat(pIe).toPrecision(12) );


I get the same results as:



orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );


from bash:



echo "$(bc <<< "scale=13;((241828072282107.5071453596951 * 666) * 2) * 3.14159265359")"


Bash is right and JavaScript is wrong by almost a 100 miles, that is crazy, it is freaking me out, how can JavaScript Floating Point Math be so far off, and this is just one example, I have a whole app full of numbers that are close, but not even close enough, a 100 miles off is not acceptable.



I do not want to deal with exponents, I want the value as an Integer, Strings are fine, it is stored in a database as string, I just need the Math to be right.



This is a QtQuick, QML, Felgo App, using Qml and JavaScript, so it will need to run on different platforms. My next thought is C++, or a Math Library that works for this type of project, JavaScript or a QML wrapper Library for C++ would be great, figuring out how make JavaScript not so bad at Floating Point would be better, I found a few JavaScript Libraries for the Web, they do not work without a lot of work under Qml, so I am only interested in what works, and I did a lot of research and did not find what I was looking for.










share|improve this question





















  • 1





    JavaScript precision just isn't good enough for your extremely large numbers. I assumed you looked into the various big* libraries that would solve the precision issue? (github.com/MikeMcl).

    – Cᴏʀʏ
    Mar 27 at 0:57












  • I was referring to this one above, it was not written for Qml JavaScript, it would take a lot of work to get it to just have no error or warnings, I started by removing "use strict", and changing the == to === and so on, but run into problems with variables not being defined before use, and fixing some of them, lead to more problems, it can be converted, never done that before, was hoping for a library that made for Qml.

    – user83395
    Mar 27 at 1:32











  • @user83395 In SO you should not add SOLVED to the title of the question, instead you should mark the question that helped you solve your problem, if instead you found the solution then we invite you to publish a solution and mark it as correct in 2 days , for more information check the tour :-)

    – eyllanesc
    Mar 29 at 3:32


















4















I want to calculate the circumstance of the Sun around the Galaxy; the Math Formula is ((241828072282107.5071453596951 * 666) * 2) * 3.14159265359, using QML JavaScript I get the answer 1011954093357316100, when the correct answer is 1011954093357316200, a 100 miles off.



galaxyRadius="241828072282107.5071453596951";

currentTrackNumber=666; // Track Number like on a Record

pIe="3.14159265359"; // not the same as Math.PI


I had to use strings because converting these sized numbers to floats truncate the precision, I am converting an old bash script, and it worked fine with bc, not so much Math.



I have tried this:



orbitDist = ((( Number.parseFloat(galaxyRadius).toPrecision(20) * currentTrackNumber) * 2) * Number.parseFloat(pIe).toPrecision(12) );


I get the same results as:



orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );


from bash:



echo "$(bc <<< "scale=13;((241828072282107.5071453596951 * 666) * 2) * 3.14159265359")"


Bash is right and JavaScript is wrong by almost a 100 miles, that is crazy, it is freaking me out, how can JavaScript Floating Point Math be so far off, and this is just one example, I have a whole app full of numbers that are close, but not even close enough, a 100 miles off is not acceptable.



I do not want to deal with exponents, I want the value as an Integer, Strings are fine, it is stored in a database as string, I just need the Math to be right.



This is a QtQuick, QML, Felgo App, using Qml and JavaScript, so it will need to run on different platforms. My next thought is C++, or a Math Library that works for this type of project, JavaScript or a QML wrapper Library for C++ would be great, figuring out how make JavaScript not so bad at Floating Point would be better, I found a few JavaScript Libraries for the Web, they do not work without a lot of work under Qml, so I am only interested in what works, and I did a lot of research and did not find what I was looking for.










share|improve this question





















  • 1





    JavaScript precision just isn't good enough for your extremely large numbers. I assumed you looked into the various big* libraries that would solve the precision issue? (github.com/MikeMcl).

    – Cᴏʀʏ
    Mar 27 at 0:57












  • I was referring to this one above, it was not written for Qml JavaScript, it would take a lot of work to get it to just have no error or warnings, I started by removing "use strict", and changing the == to === and so on, but run into problems with variables not being defined before use, and fixing some of them, lead to more problems, it can be converted, never done that before, was hoping for a library that made for Qml.

    – user83395
    Mar 27 at 1:32











  • @user83395 In SO you should not add SOLVED to the title of the question, instead you should mark the question that helped you solve your problem, if instead you found the solution then we invite you to publish a solution and mark it as correct in 2 days , for more information check the tour :-)

    – eyllanesc
    Mar 29 at 3:32














4












4








4








I want to calculate the circumstance of the Sun around the Galaxy; the Math Formula is ((241828072282107.5071453596951 * 666) * 2) * 3.14159265359, using QML JavaScript I get the answer 1011954093357316100, when the correct answer is 1011954093357316200, a 100 miles off.



galaxyRadius="241828072282107.5071453596951";

currentTrackNumber=666; // Track Number like on a Record

pIe="3.14159265359"; // not the same as Math.PI


I had to use strings because converting these sized numbers to floats truncate the precision, I am converting an old bash script, and it worked fine with bc, not so much Math.



I have tried this:



orbitDist = ((( Number.parseFloat(galaxyRadius).toPrecision(20) * currentTrackNumber) * 2) * Number.parseFloat(pIe).toPrecision(12) );


I get the same results as:



orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );


from bash:



echo "$(bc <<< "scale=13;((241828072282107.5071453596951 * 666) * 2) * 3.14159265359")"


Bash is right and JavaScript is wrong by almost a 100 miles, that is crazy, it is freaking me out, how can JavaScript Floating Point Math be so far off, and this is just one example, I have a whole app full of numbers that are close, but not even close enough, a 100 miles off is not acceptable.



I do not want to deal with exponents, I want the value as an Integer, Strings are fine, it is stored in a database as string, I just need the Math to be right.



This is a QtQuick, QML, Felgo App, using Qml and JavaScript, so it will need to run on different platforms. My next thought is C++, or a Math Library that works for this type of project, JavaScript or a QML wrapper Library for C++ would be great, figuring out how make JavaScript not so bad at Floating Point would be better, I found a few JavaScript Libraries for the Web, they do not work without a lot of work under Qml, so I am only interested in what works, and I did a lot of research and did not find what I was looking for.










share|improve this question
















I want to calculate the circumstance of the Sun around the Galaxy; the Math Formula is ((241828072282107.5071453596951 * 666) * 2) * 3.14159265359, using QML JavaScript I get the answer 1011954093357316100, when the correct answer is 1011954093357316200, a 100 miles off.



galaxyRadius="241828072282107.5071453596951";

currentTrackNumber=666; // Track Number like on a Record

pIe="3.14159265359"; // not the same as Math.PI


I had to use strings because converting these sized numbers to floats truncate the precision, I am converting an old bash script, and it worked fine with bc, not so much Math.



I have tried this:



orbitDist = ((( Number.parseFloat(galaxyRadius).toPrecision(20) * currentTrackNumber) * 2) * Number.parseFloat(pIe).toPrecision(12) );


I get the same results as:



orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );


from bash:



echo "$(bc <<< "scale=13;((241828072282107.5071453596951 * 666) * 2) * 3.14159265359")"


Bash is right and JavaScript is wrong by almost a 100 miles, that is crazy, it is freaking me out, how can JavaScript Floating Point Math be so far off, and this is just one example, I have a whole app full of numbers that are close, but not even close enough, a 100 miles off is not acceptable.



I do not want to deal with exponents, I want the value as an Integer, Strings are fine, it is stored in a database as string, I just need the Math to be right.



This is a QtQuick, QML, Felgo App, using Qml and JavaScript, so it will need to run on different platforms. My next thought is C++, or a Math Library that works for this type of project, JavaScript or a QML wrapper Library for C++ would be great, figuring out how make JavaScript not so bad at Floating Point would be better, I found a few JavaScript Libraries for the Web, they do not work without a lot of work under Qml, so I am only interested in what works, and I did a lot of research and did not find what I was looking for.







javascript bash qt math qml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 3:30









eyllanesc

104k12 gold badges38 silver badges70 bronze badges




104k12 gold badges38 silver badges70 bronze badges










asked Mar 27 at 0:51









user83395user83395

481 silver badge5 bronze badges




481 silver badge5 bronze badges










  • 1





    JavaScript precision just isn't good enough for your extremely large numbers. I assumed you looked into the various big* libraries that would solve the precision issue? (github.com/MikeMcl).

    – Cᴏʀʏ
    Mar 27 at 0:57












  • I was referring to this one above, it was not written for Qml JavaScript, it would take a lot of work to get it to just have no error or warnings, I started by removing "use strict", and changing the == to === and so on, but run into problems with variables not being defined before use, and fixing some of them, lead to more problems, it can be converted, never done that before, was hoping for a library that made for Qml.

    – user83395
    Mar 27 at 1:32











  • @user83395 In SO you should not add SOLVED to the title of the question, instead you should mark the question that helped you solve your problem, if instead you found the solution then we invite you to publish a solution and mark it as correct in 2 days , for more information check the tour :-)

    – eyllanesc
    Mar 29 at 3:32













  • 1





    JavaScript precision just isn't good enough for your extremely large numbers. I assumed you looked into the various big* libraries that would solve the precision issue? (github.com/MikeMcl).

    – Cᴏʀʏ
    Mar 27 at 0:57












  • I was referring to this one above, it was not written for Qml JavaScript, it would take a lot of work to get it to just have no error or warnings, I started by removing "use strict", and changing the == to === and so on, but run into problems with variables not being defined before use, and fixing some of them, lead to more problems, it can be converted, never done that before, was hoping for a library that made for Qml.

    – user83395
    Mar 27 at 1:32











  • @user83395 In SO you should not add SOLVED to the title of the question, instead you should mark the question that helped you solve your problem, if instead you found the solution then we invite you to publish a solution and mark it as correct in 2 days , for more information check the tour :-)

    – eyllanesc
    Mar 29 at 3:32








1




1





JavaScript precision just isn't good enough for your extremely large numbers. I assumed you looked into the various big* libraries that would solve the precision issue? (github.com/MikeMcl).

– Cᴏʀʏ
Mar 27 at 0:57






JavaScript precision just isn't good enough for your extremely large numbers. I assumed you looked into the various big* libraries that would solve the precision issue? (github.com/MikeMcl).

– Cᴏʀʏ
Mar 27 at 0:57














I was referring to this one above, it was not written for Qml JavaScript, it would take a lot of work to get it to just have no error or warnings, I started by removing "use strict", and changing the == to === and so on, but run into problems with variables not being defined before use, and fixing some of them, lead to more problems, it can be converted, never done that before, was hoping for a library that made for Qml.

– user83395
Mar 27 at 1:32





I was referring to this one above, it was not written for Qml JavaScript, it would take a lot of work to get it to just have no error or warnings, I started by removing "use strict", and changing the == to === and so on, but run into problems with variables not being defined before use, and fixing some of them, lead to more problems, it can be converted, never done that before, was hoping for a library that made for Qml.

– user83395
Mar 27 at 1:32













@user83395 In SO you should not add SOLVED to the title of the question, instead you should mark the question that helped you solve your problem, if instead you found the solution then we invite you to publish a solution and mark it as correct in 2 days , for more information check the tour :-)

– eyllanesc
Mar 29 at 3:32






@user83395 In SO you should not add SOLVED to the title of the question, instead you should mark the question that helped you solve your problem, if instead you found the solution then we invite you to publish a solution and mark it as correct in 2 days , for more information check the tour :-)

– eyllanesc
Mar 29 at 3:32













1 Answer
1






active

oldest

votes


















0














Because of JavaScript for now just support bignum for integer(BigInt) only. Then I convert it to BigInt before calculate. And tracking the decimal part to re-convert to float.






const galaxyRadius="241828072282107.5071453596951";

const currentTrackNumber=666; // Track Number like on a Record

const pIe="3.14159265359"; // not the same as Math.PI

const orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
console.log(orbitDist)

//// My approad

//Put all number into an array
var arrToMul = [galaxyRadius,currentTrackNumber, 2, pIe]

// Function multiple all item in array with BigInt format
function mulBigInt(arr)
return arr.reduce(function (acc, e)
return BigInt(e) * acc;
, BigInt(1));
;

// Function get length of decimal part
function decLength(str)
var dec = str.toString().split('.')[1];
return dec ? dec.length : 0;
;

// Function remove point in string to convert float to int
function rmPoint(strNum)
return strNum.toString().replace('.', "");
;

// Main function
function cal(arr)
// Get total decimal part length of all number
var pointSize = arr.reduce(function (acc, e)
return acc + decLength(e);
, 0);

// convert the all item to int (type string)
var newArr = arr.map(function (e)
return rmPoint(e);
);
// Add point to reconvert BigInt to float(string acctually)
var tmp = mulBigInt(newArr).toString().split('');
tmp.splice(tmp.length - pointSize, 0, '.');

// Return float result with string format
return tmp.join('');
;
const rs = cal(arrToMul)
console.log(rs, 'converted to BigInt:')








share|improve this answer



























  • QtCreator Qml JavaScript is choking on the arrow function expression, I will have to figure out how to convert them to a regular function call, unless you can update your answer to work in QtCreator, never used arrow function, I do see from researching them that they do not work in Qml, but no idea how to convert right now, will look into it, the answer is right, so it is just a matter of getting it work in Qml. Thanks, great work, did not know about this, was working on a C++ solution, like this better, a lot more precision. bugreports.qt.io/browse/QTBUG-47735

    – user83395
    Mar 27 at 4:53












  • @user83395 I updated the answer to convert arrow function into normal function. Hope it help.

    – bird
    Mar 27 at 5:27











  • Thanks, close, just add new in front of BigInt, some warning about using new before functions that start with capital letters, then I get a BigInt undefined error, looking at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, it seems this is built in, google did not help find a solution, Qml JavaScript is just not the same as Web, do you know if I need to import it, if so how? Thanks for all the help, I assume you got this to work using Qt Creator, assume, you know what that means.

    – user83395
    Mar 27 at 5:52











  • My efforts in C++ gave me 1011954093357316096, which is worse than JavaScript, seems 64 bits is not enough, and that was using a Double, I will have to look into Arbitrary Precision Math Libraries next, I hope I can use BigInt, google is no help, all I can find is for Web use, not Qml.

    – user83395
    Mar 27 at 6:14












  • I figure out one of my problems with using any JavaScript Solution, and that is that anytime you have to do any Math that involves a Float, or you have to convert to a float for any reason, you are going to have precision issues, BigInt that bird showed, works great, but I could not figure out how to use it in Qml, Qt states to look for JavaScript Examples, when most do not work, BigInt is one, plus there is a "this" bug to deal with, so I switched to boost math multiprecision, I have one issue with it, another thread, but it does give me the correct answer, so thanks for all the help.

    – user83395
    Mar 29 at 1:02










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368223%2fhow-do-you-do-large-number-math-in-qtquick-qml-using-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Because of JavaScript for now just support bignum for integer(BigInt) only. Then I convert it to BigInt before calculate. And tracking the decimal part to re-convert to float.






const galaxyRadius="241828072282107.5071453596951";

const currentTrackNumber=666; // Track Number like on a Record

const pIe="3.14159265359"; // not the same as Math.PI

const orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
console.log(orbitDist)

//// My approad

//Put all number into an array
var arrToMul = [galaxyRadius,currentTrackNumber, 2, pIe]

// Function multiple all item in array with BigInt format
function mulBigInt(arr)
return arr.reduce(function (acc, e)
return BigInt(e) * acc;
, BigInt(1));
;

// Function get length of decimal part
function decLength(str)
var dec = str.toString().split('.')[1];
return dec ? dec.length : 0;
;

// Function remove point in string to convert float to int
function rmPoint(strNum)
return strNum.toString().replace('.', "");
;

// Main function
function cal(arr)
// Get total decimal part length of all number
var pointSize = arr.reduce(function (acc, e)
return acc + decLength(e);
, 0);

// convert the all item to int (type string)
var newArr = arr.map(function (e)
return rmPoint(e);
);
// Add point to reconvert BigInt to float(string acctually)
var tmp = mulBigInt(newArr).toString().split('');
tmp.splice(tmp.length - pointSize, 0, '.');

// Return float result with string format
return tmp.join('');
;
const rs = cal(arrToMul)
console.log(rs, 'converted to BigInt:')








share|improve this answer



























  • QtCreator Qml JavaScript is choking on the arrow function expression, I will have to figure out how to convert them to a regular function call, unless you can update your answer to work in QtCreator, never used arrow function, I do see from researching them that they do not work in Qml, but no idea how to convert right now, will look into it, the answer is right, so it is just a matter of getting it work in Qml. Thanks, great work, did not know about this, was working on a C++ solution, like this better, a lot more precision. bugreports.qt.io/browse/QTBUG-47735

    – user83395
    Mar 27 at 4:53












  • @user83395 I updated the answer to convert arrow function into normal function. Hope it help.

    – bird
    Mar 27 at 5:27











  • Thanks, close, just add new in front of BigInt, some warning about using new before functions that start with capital letters, then I get a BigInt undefined error, looking at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, it seems this is built in, google did not help find a solution, Qml JavaScript is just not the same as Web, do you know if I need to import it, if so how? Thanks for all the help, I assume you got this to work using Qt Creator, assume, you know what that means.

    – user83395
    Mar 27 at 5:52











  • My efforts in C++ gave me 1011954093357316096, which is worse than JavaScript, seems 64 bits is not enough, and that was using a Double, I will have to look into Arbitrary Precision Math Libraries next, I hope I can use BigInt, google is no help, all I can find is for Web use, not Qml.

    – user83395
    Mar 27 at 6:14












  • I figure out one of my problems with using any JavaScript Solution, and that is that anytime you have to do any Math that involves a Float, or you have to convert to a float for any reason, you are going to have precision issues, BigInt that bird showed, works great, but I could not figure out how to use it in Qml, Qt states to look for JavaScript Examples, when most do not work, BigInt is one, plus there is a "this" bug to deal with, so I switched to boost math multiprecision, I have one issue with it, another thread, but it does give me the correct answer, so thanks for all the help.

    – user83395
    Mar 29 at 1:02















0














Because of JavaScript for now just support bignum for integer(BigInt) only. Then I convert it to BigInt before calculate. And tracking the decimal part to re-convert to float.






const galaxyRadius="241828072282107.5071453596951";

const currentTrackNumber=666; // Track Number like on a Record

const pIe="3.14159265359"; // not the same as Math.PI

const orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
console.log(orbitDist)

//// My approad

//Put all number into an array
var arrToMul = [galaxyRadius,currentTrackNumber, 2, pIe]

// Function multiple all item in array with BigInt format
function mulBigInt(arr)
return arr.reduce(function (acc, e)
return BigInt(e) * acc;
, BigInt(1));
;

// Function get length of decimal part
function decLength(str)
var dec = str.toString().split('.')[1];
return dec ? dec.length : 0;
;

// Function remove point in string to convert float to int
function rmPoint(strNum)
return strNum.toString().replace('.', "");
;

// Main function
function cal(arr)
// Get total decimal part length of all number
var pointSize = arr.reduce(function (acc, e)
return acc + decLength(e);
, 0);

// convert the all item to int (type string)
var newArr = arr.map(function (e)
return rmPoint(e);
);
// Add point to reconvert BigInt to float(string acctually)
var tmp = mulBigInt(newArr).toString().split('');
tmp.splice(tmp.length - pointSize, 0, '.');

// Return float result with string format
return tmp.join('');
;
const rs = cal(arrToMul)
console.log(rs, 'converted to BigInt:')








share|improve this answer



























  • QtCreator Qml JavaScript is choking on the arrow function expression, I will have to figure out how to convert them to a regular function call, unless you can update your answer to work in QtCreator, never used arrow function, I do see from researching them that they do not work in Qml, but no idea how to convert right now, will look into it, the answer is right, so it is just a matter of getting it work in Qml. Thanks, great work, did not know about this, was working on a C++ solution, like this better, a lot more precision. bugreports.qt.io/browse/QTBUG-47735

    – user83395
    Mar 27 at 4:53












  • @user83395 I updated the answer to convert arrow function into normal function. Hope it help.

    – bird
    Mar 27 at 5:27











  • Thanks, close, just add new in front of BigInt, some warning about using new before functions that start with capital letters, then I get a BigInt undefined error, looking at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, it seems this is built in, google did not help find a solution, Qml JavaScript is just not the same as Web, do you know if I need to import it, if so how? Thanks for all the help, I assume you got this to work using Qt Creator, assume, you know what that means.

    – user83395
    Mar 27 at 5:52











  • My efforts in C++ gave me 1011954093357316096, which is worse than JavaScript, seems 64 bits is not enough, and that was using a Double, I will have to look into Arbitrary Precision Math Libraries next, I hope I can use BigInt, google is no help, all I can find is for Web use, not Qml.

    – user83395
    Mar 27 at 6:14












  • I figure out one of my problems with using any JavaScript Solution, and that is that anytime you have to do any Math that involves a Float, or you have to convert to a float for any reason, you are going to have precision issues, BigInt that bird showed, works great, but I could not figure out how to use it in Qml, Qt states to look for JavaScript Examples, when most do not work, BigInt is one, plus there is a "this" bug to deal with, so I switched to boost math multiprecision, I have one issue with it, another thread, but it does give me the correct answer, so thanks for all the help.

    – user83395
    Mar 29 at 1:02













0












0








0







Because of JavaScript for now just support bignum for integer(BigInt) only. Then I convert it to BigInt before calculate. And tracking the decimal part to re-convert to float.






const galaxyRadius="241828072282107.5071453596951";

const currentTrackNumber=666; // Track Number like on a Record

const pIe="3.14159265359"; // not the same as Math.PI

const orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
console.log(orbitDist)

//// My approad

//Put all number into an array
var arrToMul = [galaxyRadius,currentTrackNumber, 2, pIe]

// Function multiple all item in array with BigInt format
function mulBigInt(arr)
return arr.reduce(function (acc, e)
return BigInt(e) * acc;
, BigInt(1));
;

// Function get length of decimal part
function decLength(str)
var dec = str.toString().split('.')[1];
return dec ? dec.length : 0;
;

// Function remove point in string to convert float to int
function rmPoint(strNum)
return strNum.toString().replace('.', "");
;

// Main function
function cal(arr)
// Get total decimal part length of all number
var pointSize = arr.reduce(function (acc, e)
return acc + decLength(e);
, 0);

// convert the all item to int (type string)
var newArr = arr.map(function (e)
return rmPoint(e);
);
// Add point to reconvert BigInt to float(string acctually)
var tmp = mulBigInt(newArr).toString().split('');
tmp.splice(tmp.length - pointSize, 0, '.');

// Return float result with string format
return tmp.join('');
;
const rs = cal(arrToMul)
console.log(rs, 'converted to BigInt:')








share|improve this answer















Because of JavaScript for now just support bignum for integer(BigInt) only. Then I convert it to BigInt before calculate. And tracking the decimal part to re-convert to float.






const galaxyRadius="241828072282107.5071453596951";

const currentTrackNumber=666; // Track Number like on a Record

const pIe="3.14159265359"; // not the same as Math.PI

const orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
console.log(orbitDist)

//// My approad

//Put all number into an array
var arrToMul = [galaxyRadius,currentTrackNumber, 2, pIe]

// Function multiple all item in array with BigInt format
function mulBigInt(arr)
return arr.reduce(function (acc, e)
return BigInt(e) * acc;
, BigInt(1));
;

// Function get length of decimal part
function decLength(str)
var dec = str.toString().split('.')[1];
return dec ? dec.length : 0;
;

// Function remove point in string to convert float to int
function rmPoint(strNum)
return strNum.toString().replace('.', "");
;

// Main function
function cal(arr)
// Get total decimal part length of all number
var pointSize = arr.reduce(function (acc, e)
return acc + decLength(e);
, 0);

// convert the all item to int (type string)
var newArr = arr.map(function (e)
return rmPoint(e);
);
// Add point to reconvert BigInt to float(string acctually)
var tmp = mulBigInt(newArr).toString().split('');
tmp.splice(tmp.length - pointSize, 0, '.');

// Return float result with string format
return tmp.join('');
;
const rs = cal(arrToMul)
console.log(rs, 'converted to BigInt:')








const galaxyRadius="241828072282107.5071453596951";

const currentTrackNumber=666; // Track Number like on a Record

const pIe="3.14159265359"; // not the same as Math.PI

const orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
console.log(orbitDist)

//// My approad

//Put all number into an array
var arrToMul = [galaxyRadius,currentTrackNumber, 2, pIe]

// Function multiple all item in array with BigInt format
function mulBigInt(arr)
return arr.reduce(function (acc, e)
return BigInt(e) * acc;
, BigInt(1));
;

// Function get length of decimal part
function decLength(str)
var dec = str.toString().split('.')[1];
return dec ? dec.length : 0;
;

// Function remove point in string to convert float to int
function rmPoint(strNum)
return strNum.toString().replace('.', "");
;

// Main function
function cal(arr)
// Get total decimal part length of all number
var pointSize = arr.reduce(function (acc, e)
return acc + decLength(e);
, 0);

// convert the all item to int (type string)
var newArr = arr.map(function (e)
return rmPoint(e);
);
// Add point to reconvert BigInt to float(string acctually)
var tmp = mulBigInt(newArr).toString().split('');
tmp.splice(tmp.length - pointSize, 0, '.');

// Return float result with string format
return tmp.join('');
;
const rs = cal(arrToMul)
console.log(rs, 'converted to BigInt:')





const galaxyRadius="241828072282107.5071453596951";

const currentTrackNumber=666; // Track Number like on a Record

const pIe="3.14159265359"; // not the same as Math.PI

const orbitDist = ((( galaxyRadius * currentTrackNumber) * 2) * pIe );
console.log(orbitDist)

//// My approad

//Put all number into an array
var arrToMul = [galaxyRadius,currentTrackNumber, 2, pIe]

// Function multiple all item in array with BigInt format
function mulBigInt(arr)
return arr.reduce(function (acc, e)
return BigInt(e) * acc;
, BigInt(1));
;

// Function get length of decimal part
function decLength(str)
var dec = str.toString().split('.')[1];
return dec ? dec.length : 0;
;

// Function remove point in string to convert float to int
function rmPoint(strNum)
return strNum.toString().replace('.', "");
;

// Main function
function cal(arr)
// Get total decimal part length of all number
var pointSize = arr.reduce(function (acc, e)
return acc + decLength(e);
, 0);

// convert the all item to int (type string)
var newArr = arr.map(function (e)
return rmPoint(e);
);
// Add point to reconvert BigInt to float(string acctually)
var tmp = mulBigInt(newArr).toString().split('');
tmp.splice(tmp.length - pointSize, 0, '.');

// Return float result with string format
return tmp.join('');
;
const rs = cal(arrToMul)
console.log(rs, 'converted to BigInt:')






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 5:26

























answered Mar 27 at 2:11









birdbird

1,3239 silver badges20 bronze badges




1,3239 silver badges20 bronze badges















  • QtCreator Qml JavaScript is choking on the arrow function expression, I will have to figure out how to convert them to a regular function call, unless you can update your answer to work in QtCreator, never used arrow function, I do see from researching them that they do not work in Qml, but no idea how to convert right now, will look into it, the answer is right, so it is just a matter of getting it work in Qml. Thanks, great work, did not know about this, was working on a C++ solution, like this better, a lot more precision. bugreports.qt.io/browse/QTBUG-47735

    – user83395
    Mar 27 at 4:53












  • @user83395 I updated the answer to convert arrow function into normal function. Hope it help.

    – bird
    Mar 27 at 5:27











  • Thanks, close, just add new in front of BigInt, some warning about using new before functions that start with capital letters, then I get a BigInt undefined error, looking at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, it seems this is built in, google did not help find a solution, Qml JavaScript is just not the same as Web, do you know if I need to import it, if so how? Thanks for all the help, I assume you got this to work using Qt Creator, assume, you know what that means.

    – user83395
    Mar 27 at 5:52











  • My efforts in C++ gave me 1011954093357316096, which is worse than JavaScript, seems 64 bits is not enough, and that was using a Double, I will have to look into Arbitrary Precision Math Libraries next, I hope I can use BigInt, google is no help, all I can find is for Web use, not Qml.

    – user83395
    Mar 27 at 6:14












  • I figure out one of my problems with using any JavaScript Solution, and that is that anytime you have to do any Math that involves a Float, or you have to convert to a float for any reason, you are going to have precision issues, BigInt that bird showed, works great, but I could not figure out how to use it in Qml, Qt states to look for JavaScript Examples, when most do not work, BigInt is one, plus there is a "this" bug to deal with, so I switched to boost math multiprecision, I have one issue with it, another thread, but it does give me the correct answer, so thanks for all the help.

    – user83395
    Mar 29 at 1:02

















  • QtCreator Qml JavaScript is choking on the arrow function expression, I will have to figure out how to convert them to a regular function call, unless you can update your answer to work in QtCreator, never used arrow function, I do see from researching them that they do not work in Qml, but no idea how to convert right now, will look into it, the answer is right, so it is just a matter of getting it work in Qml. Thanks, great work, did not know about this, was working on a C++ solution, like this better, a lot more precision. bugreports.qt.io/browse/QTBUG-47735

    – user83395
    Mar 27 at 4:53












  • @user83395 I updated the answer to convert arrow function into normal function. Hope it help.

    – bird
    Mar 27 at 5:27











  • Thanks, close, just add new in front of BigInt, some warning about using new before functions that start with capital letters, then I get a BigInt undefined error, looking at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, it seems this is built in, google did not help find a solution, Qml JavaScript is just not the same as Web, do you know if I need to import it, if so how? Thanks for all the help, I assume you got this to work using Qt Creator, assume, you know what that means.

    – user83395
    Mar 27 at 5:52











  • My efforts in C++ gave me 1011954093357316096, which is worse than JavaScript, seems 64 bits is not enough, and that was using a Double, I will have to look into Arbitrary Precision Math Libraries next, I hope I can use BigInt, google is no help, all I can find is for Web use, not Qml.

    – user83395
    Mar 27 at 6:14












  • I figure out one of my problems with using any JavaScript Solution, and that is that anytime you have to do any Math that involves a Float, or you have to convert to a float for any reason, you are going to have precision issues, BigInt that bird showed, works great, but I could not figure out how to use it in Qml, Qt states to look for JavaScript Examples, when most do not work, BigInt is one, plus there is a "this" bug to deal with, so I switched to boost math multiprecision, I have one issue with it, another thread, but it does give me the correct answer, so thanks for all the help.

    – user83395
    Mar 29 at 1:02
















QtCreator Qml JavaScript is choking on the arrow function expression, I will have to figure out how to convert them to a regular function call, unless you can update your answer to work in QtCreator, never used arrow function, I do see from researching them that they do not work in Qml, but no idea how to convert right now, will look into it, the answer is right, so it is just a matter of getting it work in Qml. Thanks, great work, did not know about this, was working on a C++ solution, like this better, a lot more precision. bugreports.qt.io/browse/QTBUG-47735

– user83395
Mar 27 at 4:53






QtCreator Qml JavaScript is choking on the arrow function expression, I will have to figure out how to convert them to a regular function call, unless you can update your answer to work in QtCreator, never used arrow function, I do see from researching them that they do not work in Qml, but no idea how to convert right now, will look into it, the answer is right, so it is just a matter of getting it work in Qml. Thanks, great work, did not know about this, was working on a C++ solution, like this better, a lot more precision. bugreports.qt.io/browse/QTBUG-47735

– user83395
Mar 27 at 4:53














@user83395 I updated the answer to convert arrow function into normal function. Hope it help.

– bird
Mar 27 at 5:27





@user83395 I updated the answer to convert arrow function into normal function. Hope it help.

– bird
Mar 27 at 5:27













Thanks, close, just add new in front of BigInt, some warning about using new before functions that start with capital letters, then I get a BigInt undefined error, looking at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, it seems this is built in, google did not help find a solution, Qml JavaScript is just not the same as Web, do you know if I need to import it, if so how? Thanks for all the help, I assume you got this to work using Qt Creator, assume, you know what that means.

– user83395
Mar 27 at 5:52





Thanks, close, just add new in front of BigInt, some warning about using new before functions that start with capital letters, then I get a BigInt undefined error, looking at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, it seems this is built in, google did not help find a solution, Qml JavaScript is just not the same as Web, do you know if I need to import it, if so how? Thanks for all the help, I assume you got this to work using Qt Creator, assume, you know what that means.

– user83395
Mar 27 at 5:52













My efforts in C++ gave me 1011954093357316096, which is worse than JavaScript, seems 64 bits is not enough, and that was using a Double, I will have to look into Arbitrary Precision Math Libraries next, I hope I can use BigInt, google is no help, all I can find is for Web use, not Qml.

– user83395
Mar 27 at 6:14






My efforts in C++ gave me 1011954093357316096, which is worse than JavaScript, seems 64 bits is not enough, and that was using a Double, I will have to look into Arbitrary Precision Math Libraries next, I hope I can use BigInt, google is no help, all I can find is for Web use, not Qml.

– user83395
Mar 27 at 6:14














I figure out one of my problems with using any JavaScript Solution, and that is that anytime you have to do any Math that involves a Float, or you have to convert to a float for any reason, you are going to have precision issues, BigInt that bird showed, works great, but I could not figure out how to use it in Qml, Qt states to look for JavaScript Examples, when most do not work, BigInt is one, plus there is a "this" bug to deal with, so I switched to boost math multiprecision, I have one issue with it, another thread, but it does give me the correct answer, so thanks for all the help.

– user83395
Mar 29 at 1:02





I figure out one of my problems with using any JavaScript Solution, and that is that anytime you have to do any Math that involves a Float, or you have to convert to a float for any reason, you are going to have precision issues, BigInt that bird showed, works great, but I could not figure out how to use it in Qml, Qt states to look for JavaScript Examples, when most do not work, BigInt is one, plus there is a "this" bug to deal with, so I switched to boost math multiprecision, I have one issue with it, another thread, but it does give me the correct answer, so thanks for all the help.

– user83395
Mar 29 at 1:02








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368223%2fhow-do-you-do-large-number-math-in-qtquick-qml-using-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript