Javascript BigInt.js How to Divide big numbers

ぃ、小莉子 提交于 2019-12-23 04:31:51

问题


http://www.leemon.com/crypto/BigInt.js

I am using the leemon bigint.js library, but I am having trouble figuring out how to divide one big number by another. Here is what I have so far:

var a = str2bigInt("100",10);
var b = int2bigInt("5", 10);
var result = [];
var r = [];
divide_(a,b,result,r)
alert(bigInt2str(result,10));

but when I alert(result) the output is 0. The result should be 20? Can anybody see what I am doing wrong?

Cheers


回答1:


Apparently, this BigInt.js library expects the result arrays to already have sufficient length to store the result; using empty arrays doesn't work.

This code however works as expected:

var a = str2bigInt("100",10);
var b = int2bigInt("5", 10);
var result = new Array(2);
var r = new Array(2);
divide_(a,b,result,r);
alert(bigInt2str(result,10));



回答2:


I suppose the line

var b = int2bigInt("5", 10);

should be

var b = str2bigInt("5", 10);

The function int2bigInt expects an integer, not a string.



来源:https://stackoverflow.com/questions/10959255/javascript-bigint-js-how-to-divide-big-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!