Created
February 10, 2021 17:40
-
-
Save 1oo7/494ec90601b4338914c78c595d79eed1 to your computer and use it in GitHub Desktop.
Big Decimal Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BigDecimal = require('js-big-decimal') | |
var array = [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255] | |
var decimal = new BigDecimal(0) | |
var _256 = new BigDecimal(256) | |
array.map((value => { | |
decimal = decimal.multiply(_256) | |
decimal = decimal.add(new BigDecimal(value)) | |
})) | |
console.log(decimal) | |
var zero = new BigDecimal(0) | |
var resultArray = [] | |
while (decimal.compareTo(zero) > 0) { | |
var modulus = decimal.modulus(_256) | |
var modulusNum = new Number(modulus.getValue()) | |
resultArray.push(modulusNum) | |
decimal = decimal | |
.subtract(modulus) | |
.divide(_256) | |
.round(0) | |
} | |
resultArray = resultArray.reverse() | |
console.log("Result array: " + resultArray.join(', ')) | |
resultArray.map((value, index) => { | |
if (value != array[index]) { | |
console.log('math is broken') | |
} else { | |
console.log('math rules') | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment